Skip to content

Instantly share code, notes, and snippets.

@hanmd82
Last active January 29, 2018 17:25
Show Gist options
  • Save hanmd82/cc573fa8a8ba6ada5af301fe329e1801 to your computer and use it in GitHub Desktop.
Save hanmd82/cc573fa8a8ba6ada5af301fe329e1801 to your computer and use it in GitHub Desktop.
ES6 katas for Arrays - http://es6katas.org/
// 29: array - `Array.from` static method
describe('`Array.from` converts an array-like object or list into an Array', () => {
const arrayLike = {0: 'one', 1: 'two', length: 2};
it('call `Array.from` with an array-like object', function() {
const arr = Array.from(arrayLike);
assert.deepEqual(arr, ['one', 'two']);
});
it('a DOM node`s classList object can be converted', function() {
document.body.classList.add('some');
document.body.classList.add('other');
const classList = Array.from(document.body.classList);
assert.equal(''+classList, ''+['some', 'other']);
});
it('convert a NodeList to an Array and `filter()` works on it', function() {
const nodeList = Array.from(document.querySelectorAll('body'));
const bodies = nodeList.filter((node) => node === document.body);
assert.deepEqual(bodies, [document.body]);
});
describe('custom conversion using a map function as second param', () => {
it('we can modify the value before putting it in the array', function() {
const arr = Array.from(arrayLike, (value) => value.toUpperCase());
assert.deepEqual(arr, ['ONE', 'TWO']);
});
it('and we also get the object`s key as second parameter', function() {
const arr = Array.from(arrayLike, (value, key) => `${key}=${value}`);
assert.deepEqual(arr, ['0=one', '1=two']);
});
});
});
// 30: array - `Array.of` static method
describe('`Array.of` creates an array with the given arguments as elements', () => {
it('dont mix it up with `Array(10)`, where the argument is the array length', () => {
const arr = Array.of(10);
assert.deepEqual(arr, [10]);
});
it('puts all arguments into array elements', () => {
const arr = Array.of(1, 2);
assert.deepEqual(arr, [1, 2]);
});
it('takes any kind and number of arguments', () => {
const starter = [[1, 2]];
const end = [3, '4'];
const arr = Array.of(...starter, ...end);
assert.deepEqual(arr, [[1, 2], 3, '4']);
});
});
// 31: array - `Array.prototype.fill` method
describe('`Array.prototype.fill` can fill up an array with one value', () => {
it('`fill(0)` will populate `0` into each array element', function() {
const arr = new Array(3).fill(0);
assert.deepEqual(arr, [0, 0, 0]);
});
it('fill only changes content, adds no new elements', function() {
const arr = [].fill(0);
assert.deepEqual(arr, []);
});
it('second parameter to `fill()` is the position where to start filling', function() {
const fillPosition = 2;
const arr = [1,2,3].fill(42, fillPosition);
assert.deepEqual(arr, [1, 2, 42]);
});
it('third parameter is the position where filling stops', function() {
const fillStartAt = 1;
const fillEndAt = 2;
const arr = [1,2,3].fill(42, fillStartAt, fillEndAt);
assert.deepEqual(arr, [1, 42, 3]);
});
});
// 32: array - `Array.prototype.find`
describe('`Array.prototype.find` makes finding items in arrays easier', () => {
it('takes a compare function', function() {
const found = [false, true].find(v => v === true );
assert.equal(found, true);
});
it('returns the first value found', function() {
const found = [0, 1, 2].find(item => item > 1);
assert.equal(found, 2);
});
it('returns `undefined` when nothing was found', function() {
const found = [1, 2, 3].find(item => item === 4);
assert.equal(found, void 0);
});
it('combined with destructuring complex compares become short', function() {
const bob = {name: 'Bob'};
const alice = {name: 'Alice'};
const found = [bob, alice].find(({name}) => name === "Alice");
assert.equal(found, alice);
});
});
// 33: array - `Array.prototype.findIndex`
describe('`Array.prototype.findIndex` makes finding items in arrays easier', () => {
it('takes a compare function, returns the index where it returned true', function() {
const foundAt = [false, true].findIndex(item => item === true);
assert.equal(foundAt, 1);
});
it('returns the first position it was found at', function() {
const foundAt = [0, 1, 1, 1].findIndex(item => item === 1);
assert.equal(foundAt, 1);
});
it('returns `-1` when nothing was found', function() {
const foundAt = [1, 0, -1].findIndex(item => item > 1);
assert.equal(foundAt, -1);
});
it('the findIndex callback gets the item, index and array as arguments', function() {
const three = 3;
const containsThree = arr => arr.indexOf(three) > -1;
function theSecondThree(item, index, arr) {
const preceedingItems = arr.slice(0, index);
return containsThree(preceedingItems);
}
const foundAt = [1, 1, 2, 3, 3, 3].findIndex(theSecondThree);
assert.equal(foundAt, 4);
});
it('combined with destructuring complex compares become short', function() {
const bob = {name: 'Bob'};
const alice = {name: 'Alice'};
const foundAt = [bob, alice].findIndex(({name:{length}}) => length > 3);
assert.equal(foundAt, 1);
});
});
// 41: array - entries
describe('`[].entries()` returns an iterator object with all entries', function() {
it('returns key+value for each element', function() {
const arr = ['a', 'b', 'c'];
const entriesAsArray = Array.from(arr.entries());
assert.deepEqual(entriesAsArray, [[0,"a"], [1,"b"], [2,"c"]]);
});
it('empty elements contain the value `undefined`', function() {
const arr = ['one'];
arr[2] = 'three';
const secondValue = Array.from(arr.entries())[1];
assert.deepEqual(secondValue, [1, void 0]);
});
describe('returns an iterable', function() {
it('has `next()` to iterate', function() {
const arr = ['one'];
const entries = arr.entries()
const value = entries.next().value;
assert.deepEqual(value, [0, 'one']);
});
});
});
// 42: array - `Array.prototype.keys`
describe('`Array.prototype.keys` returns an iterator for all keys in the array', () => {
it('`keys()` returns an iterator', function() {
const arr = ['a'];
const iterator = arr.keys();
assert.deepEqual(iterator.next(), {value: 0, done: false});
assert.deepEqual(iterator.next(), {value: void 0, done: true});
});
it('gets all keys', function() {
const arr = [0, 1, 2];
const keys = Array.from(arr.keys());
assert.deepEqual(keys, [0, 1, 2]);
});
it('empty array contains no keys', function() {
const arr = [];
const keys = [...arr.keys()];
assert.equal(keys.length, 0);
});
it('a sparse array without real values has keys though', function() {
const arr = [,,];
const keys = [...arr.keys()];
assert.deepEqual(keys, [0, 1]);
});
it('also includes holes in sparse arrays', function() {
const arr = ['a', , 'c'];
const keys = [...arr.keys()];
assert.deepEqual(keys, [0, 1, 2]);
});
});
@nccharles
Copy link

cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment