Skip to content

Instantly share code, notes, and snippets.

View underwindfall's full-sized avatar
🐌
Making Progress

Qifan Yang underwindfall

🐌
Making Progress
View GitHub Profile
// 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']);
});
// 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);
});
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);
// 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');
// 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);
});
// 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);
});
// 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, '☢');
});
// 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);
// 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]);
});
// 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);
});