diff options
author | Bernhard Posselt <dev@bernhard-posselt.com> | 2015-11-12 17:21:08 +0100 |
---|---|---|
committer | Bernhard Posselt <dev@bernhard-posselt.com> | 2015-11-12 17:21:08 +0100 |
commit | 104cf2afe06c7a51dee47b412a508bc9b0d83a69 (patch) | |
tree | 0221feb96a09546a3a66c23b24469ef3f91d17c2 /js/vendor/es6-shim/README.md | |
parent | ec700890c0484250edb4796b197d490df99daa42 (diff) |
update es6 shim
Diffstat (limited to 'js/vendor/es6-shim/README.md')
-rw-r--r-- | js/vendor/es6-shim/README.md | 67 |
1 files changed, 41 insertions, 26 deletions
diff --git a/js/vendor/es6-shim/README.md b/js/vendor/es6-shim/README.md index 9e1b54169..5a0955674 100644 --- a/js/vendor/es6-shim/README.md +++ b/js/vendor/es6-shim/README.md @@ -156,27 +156,43 @@ The [es6-collections](https://github.com/WebReflection/es6-collections) implemen ## Getting started ```javascript -var assert = require('assert'); require('es6-shim'); +var assert = require('assert'); + +assert.equal(true, 'abc'.startsWith('a')); +assert.equal(false, 'abc'.endsWith('a')); +assert.equal(true, 'john alice'.includes('john')); +assert.equal('123'.repeat(2), '123123'); + +assert.equal(false, NaN === NaN); +assert.equal(true, Object.is(NaN, NaN)); +assert.equal(true, -0 === 0); +assert.equal(false, Object.is(-0, 0)); -'abc'.startsWith('a'); // true -'abc'.endsWith('a'); // false -'john alice'.includes('john'); // true -'123'.repeat(2); // '123123' +var result = Object.assign({ a: 1 }, { b: 2 }); +assert.deepEqual(result, { a: 1, b: 2 }); -Object.is(NaN, NaN); // Fixes ===. 0 isnt -0, NaN is NaN -Object.assign({a: 1}, {b: 2}); // {a: 1, b: 2} +assert.equal(true, isNaN('a')); +assert.equal(false, Number.isNaN('a')); +assert.equal(true, Number.isNaN(NaN)); + +assert.equal(true, isFinite('123')); +assert.equal(false, Number.isFinite('123')); +assert.equal(false, Number.isFinite(Infinity)); -Number.isNaN('123'); // false. Global isNaN('123') will give true. -Number.isFinite('asd'); // false. Global isFinite() will give true. // Tests if value is a number, finite, // >= -9007199254740992 && <= 9007199254740992 and floor(value) === value -Number.isInteger(2.4); // false. +assert.equal(false, Number.isInteger(2.4)); + +assert.equal(1, Math.sign(400)); +assert.equal(0, Math.sign(0)); +assert.equal(-1, Math.sign(-400)); -Math.sign(400); // 1, 0 or -1 depending on sign. In this case 1. +var found = [5, 10, 15, 10].find(function (item) { return item / 2 === 5; }); +assert.equal(10, found); -[5, 10, 15, 10].find(function (item) { return item / 2 === 5; }); // 10 -[5, 10, 15, 10].findIndex(function (item) { return item / 2 === 5; }); // 1 +var foundIndex = [5, 10, 15, 10].findIndex(function (item) { return item / 2 === 5; }); +assert.equal(1, foundIndex); // Replacement for `{}` key-value storage. // Keys can be anything. @@ -184,30 +200,35 @@ var map = new Map([['Bob', 42], ['Foo', 'bar']]); map.set('John', 25); map.set('Alice', 400); map.set(['meh'], 555); -assert(map.get(['meh']) === undefined); // undefined because you need to use exactly the same object. +assert.equal(undefined, map.get(['meh'])); // undefined because you need to use exactly the same object. map.delete('Alice'); map.keys(); map.values(); -assert(map.size === 4); +assert.equal(4, map.size); // Useful for storing unique items. var set = new Set([0, 1]); set.add(2); set.add(5); -assert(set.has(0) === true); -assert(set.has(1) === true); -assert(set.has(2) === true); -assert(set.has(4) === false); +assert.equal(true, set.has(0)); +assert.equal(true, set.has(1)); +assert.equal(true, set.has(2)); +assert.equal(false, set.has(4)); +assert.equal(true, set.has(5)); set.delete(5); +assert.equal(false, set.has(5)); // Promises, see // http://www.slideshare.net/domenicdenicola/callbacks-promises-and-coroutines-oh-my-the-evolution-of-asynchronicity-in-javascript // https://github.com/petkaantonov/bluebird/#what-are-promises-and-why-should-i-use-them Promise.resolve(5).then(function (value) { - if (value) throw new Error("whoops!"); + assert.equal(value, 5); + if (value) throw new Error('whoops!'); // do some stuff return anotherPromise(); }).catch(function (e) { + assert.equal(e.message, 'whoops!'); + assert.equal(true, e instanceof Error); // any errors thrown asynchronously end up here }); ``` @@ -216,12 +237,6 @@ Promise.resolve(5).then(function (value) { - `Object.setPrototypeOf` / `Reflect.setPrototypeOf` - Note that null objects (`Object.create(null)`, eg, an object with `null` as its `[[Prototype]]`) can not have their `[[Prototype]]` changed except via a native `Object.setPrototypeOf`. - - `Number`: - - In order to support binary literals (`Number('0b1')`) and octal literals (`Number('0o7')`), the global `Number` constructor is wrapped in a shim. However, this can cause issues in the exceedingly unlikely event that you ever call `Number` as a function with a receiver (a “this” value) that is itself a number. Some problematic examples: -```js - assert(typeof Number.call(2, 3) === 'number'); // will fail when `Number` is wrapped, is "object" - assert(typeof (1).constructor(2) === 'number'); // will fail when `Number` is wrapped, is "object" -``` ## [License][license-url] |