This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 11: destructuring - string | |
// To do: make all tests pass, leave the assert lines unchanged! | |
describe('destructuring also works on strings', () => { | |
it('destructure every character', () => { | |
let [a, b, c] = 'abc'; | |
assert.deepEqual([a, b, c], ['a', 'b', 'c']); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 12: destructuring - object | |
// To do: make all tests pass, leave the assert lines unchanged! | |
describe('destructuring objects', () => { | |
it('is simple', () => { | |
const {x} = {x: 1}; | |
assert.equal(x, 1); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('destructuring can also have default values', () => { | |
it('for an empty array', () => { | |
const [a=1] = []; | |
assert.equal(a, 1); | |
}); | |
it('for a missing value', () => { | |
const [,b=2,] = [1,,3]; | |
assert.equal(b, 2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 14: destructuring - parameters | |
// To do: make all tests pass, leave the assert lines unchanged! | |
describe('destructuring function parameters', () => { | |
describe('destruct parameters', () => { | |
it('multiple params from object', () => { | |
const fn = ({id,name}) => { | |
assert.equal(id, 42); | |
assert.equal(name, 'Wolfram'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 15: destructuring - assign | |
// To do: make all tests pass, leave the assert lines unchanged! | |
describe('assign object property values to new variables while destructuring', () => { | |
describe('for simple objects', function() { | |
it('use a colon after the property name, like so `propertyName: newName`', () => { | |
const {x: newName} = {x: 1}; | |
assert.equal(newName, 1); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 16: object-literal - computed properties | |
// To do: make all tests pass, leave the assert lines unchanged! | |
describe('Object literal properties may be computed values', () => { | |
it('a computed property `x` needs to be surrounded by `[]`', () => { | |
const propertyName = 'x'; | |
const obj = {[propertyName]: 1}; | |
assert.equal(obj.x, 1); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 17: unicode - in strings | |
// To do: make all tests pass, leave the assert lines unchanged! | |
describe('unicode strings', () => { | |
it('are \\u prefixed', () => { | |
const nuclear = '\u2622'; | |
assert.equal(nuclear, '☢'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 18: rest - as-parameter | |
// To do: make all tests pass, leave the assert lines unchanged! | |
describe('rest in function params', () => { | |
it('must be the last parameter', () => { | |
const fn = (...rest) => { | |
assert.deepEqual([1, 2], rest); | |
}; | |
fn(1, 2); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 19: rest - with-destructuring | |
// To do: make all tests pass, leave the assert lines unchanged! | |
describe('rest with destructuring', () => { | |
it('rest parameter must be last', () => { | |
const [...all] = [1, 2, 3, 4]; | |
assert.deepEqual(all, [1, 2, 3, 4]); | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 20: spread - with-arrays | |
// To do: make all tests pass, leave the assert lines unchanged! | |
describe('spread with arrays', () => { | |
it('extracts each array item', function() { | |
const [a, b] = [...[1, 2]]; | |
assert.equal(a, 1); | |
assert.equal(b, 2); | |
}); |