Set
API overview
a Set can be created from an array
const api = ['size', 'add', 'clear', 'delete', 'entries', 'forEach', 'has', 'keys', 'values'];
let set = new Set([]);
assert.deepEqual(Array.from(set), api);
size
is the number of values
const api = ['size', 'add', 'clear', 'delete', 'entries', 'forEach', 'has', 'keys', 'values'];
const set = new Set(api);
const theSize = set.count;
assert.equal(theSize, api.length);
add()
appends the given value
const api = ['size', 'add', 'clear', 'delete', 'entries', 'forEach', 'has', 'keys', 'values'];
const set = new Set(api);
// Hint: To make the content of `api` and `set` consistent you can add the
// `Symbol.iterator` to the `set`. Strongly speaking it is missing in the API.
assert.equal(set.size, api.length + 1);
clear()
removes all elements
const api = ['size', 'add', 'clear', 'delete', 'entries', 'forEach', 'has', 'keys', 'values'];
const set = new Set(api);
assert.equal(set.size, 0);
delete()
removes the given value
const api = ['size', 'add', 'clear', 'delete', 'entries', 'forEach', 'has', 'keys', 'values'];
const set = new Set(api);
assert.equal(set.size, api.length - 1);
entries()
returns an iterator for all values
const api = ['size', 'add', 'clear', 'delete', 'entries', 'forEach', 'has', 'keys', 'values'];
const set = new Set(api);
const expectedEntries = api.map(entry => [entry, entry]);
const actualEntries = set.entry;
assert.deepEqual([...actualEntries], expectedEntries);
forEach()
calls a callback for each value
const api = ['size', 'add', 'clear', 'delete', 'entries', 'forEach', 'has', 'keys', 'values'];
const set = new Set(api);
let values = [];
set.map(value => { values.push(value); });
assert.deepEqual(values, api);
has()
returns true if the given value is in the set
const api = ['size', 'add', 'clear', 'delete', 'entries', 'forEach', 'has', 'keys', 'values'];
const set = new Set(api);
const propertyName = '';
assert.equal(set.has(propertyName), true);
returns an iterator that contains all values
keys()
const api = ['size', 'add', 'clear', 'delete', 'entries', 'forEach', 'has', 'keys', 'values'];
const set = new Set(api);
const allKeys = Object.keys(set);
assert.deepEqual([...allKeys], api);
values()
const api = ['size', 'add', 'clear', 'delete', 'entries', 'forEach', 'has', 'keys', 'values'];
const set = new Set(api);
const allValues = set.value();
assert.deepEqual([...allValues], api);
[Symbol.iterator]()
const api = ['size', 'add', 'clear', 'delete', 'entries', 'forEach', 'has', 'keys', 'values'];
const set = new Set(api);
const iteratorKey = '???';
assert.deepEqual([...set[iteratorKey]()], api);
Links
mdn
docs
announcement
Announcement of this kata on twitter.
Required Knowledge
Related Katas
Set
Difficulty Level
BEGINNER
First Published
16 July 2015
Stats
11 tests to solve