es6-polyfill
v1.1.0
Published
A javascript polyfill library for es6 API such as findIndex, Array.from, etc.
Downloads
305
Readme
Installation
A javascript polyfill library for es6 API such as findIndex, Array.from, etc.
NPM
# latest stable
$ npm install es6-polyfill
Explanation of Different Builds
In the dist/
directory you will find different builds of cookie-box. Here's an overview of the difference between them:
| | UMD | CommonJS | ES Module | | --- | --- | --- | --- | | Full | es6-polyfill.umd.js | es6-polyfill.cjs.js | es6-polyfill.esm.js |
Quick Start
import * as _ from 'es6-polyfill';
_.from('foo'); // => ['f', 'o', 'o']
API
Object
#keys(obj)
Arguments
- {Object} obj
Returns
- {Array}
Usage
Returns an array of a given object's own enumerable properties.
import { keys } from 'es6-polyfill'; keys({ 100: 'a', 2: 'b', 7: 'c' } // => [ '2', '7', '100' ]
#assign(target, ...sources)
Arguments
- {Object} target
- {Object} ...sources
Returns
- {Object}
Usage
Return the target object that copy the values of all enumerable own properties from one or more source objects.
import { assign } from 'es6-polyfill'; assign({ a: 1 }, { b: 2 }, { c: 3 }); // => { a: 1, b: 2, c: 3 }
Function
#bind(fn, thisArg)
Arguments
- {Function} fn
- {Object} thisArg
Returns
- {Function}
Usage
Return a new function that, when called, has its
this
keyword set to the provided value.import { bind } from 'es6-polyfill'; const module = { x: 81, getX: function () { return this.x; } }; const retrieveX = module.getX; retrieveX.bind(module)()); // => 81
Array
#from(arrayLike [ , mapfn [ , thisArg ] ])
Arguments
- {Object} arrayLike
- {Function} mapFn
- {Object} thisArg
Returns
- {Array}
Usage
Return a new Array from a group of arrayLike arguments.
import { from } from 'es6-polyfill'; from('foo'); // => ['f', 'o', 'o'] from([1, 2, 3], x => x + x); // => [2, 4, 6] from({ length: 5 }, (v, i) => i); // => [0, 1, 2, 3, 4]
#isArray(value)
Arguments
- {Object} value
Returns
- {Boolean}
Usage
Return true when the value is an Array, and return false when the value is not an Array.
import { isArray } from 'es6-polyfill'; isArray('foobar'); // => false isArray({foo: 123}); // => false isArray(undefined); // => false isArray([1, 2, 3]); // => true
#find(array, predicate)
Arguments
- {Array} array
- {Function} predicate
Returns
- {Any}
Usage
Return the value of the first element in the array that satisfies the provided testing function, otherwise return undefined.
import { find } from 'es6-polyfill'; find([12, 5, 8, 130, 44], element => element >= 15); // => 130 find([{ name: 'apples', quantity: 2 }, { name: 'bananas', quantity: 0 }, { name: 'cherries', quantity: 5 }], fruit => fruit.name === 'cherries')); // => { name: 'cherries', quantity: 5 } find([12, 5, 8, 130, 44], element => element >= 150); // => undefined
#findIndex(array, predicate)
Arguments
- {Array} array
- {Function} predicate
Returns
- {Number}
Usage
Return the index of the first element in the array that satisfies the provided testing function, otherwise return -1.
import { findIndex } from 'es6-polyfill'; const isPrime = (element, index, array) => { let start = 2; while (start <= Math.sqrt(element)) { if (element % start++ < 1) return false; } return element > 1; }; findIndex([4, 6, 7, 12], isPrime); // => 2 findIndex([4, 6, 8, 12], isPrime); // => -1
#indexOf(array, vMember [ , nStartFrom ])
Arguments
- {Array} array
- {Any} vMember
- {Number} nStartFrom
Returns
- {Number}
Usage
Returns the first index at which a given element can be found in the array, otherwise return -1.
import { indexOf } from 'es6-polyfill'; indexOf([2, 9, 9], 9); // => 1 indexOf([2, 9, 9], 9, 2); // => 2 indexOf([2, 9, 9], 7); // => -1 indexOf([2, 9, 9, 1], 3); // => -1
#reverse(array)
Arguments
- {Array} array
Returns
- {Array}
Usage
Return the reversed array from the original, and the initial array will also modified.
import { reverse } from 'es6-polyfill'; reverse([8, 6, 4, 2]) // => [2, 6, 4, 8]
String
#includes(string, search, start)
Arguments
- {String} string
- {String} string
- {Number} start
Returns
- {Boolean}
Usage
Return true when search is present and return false if not.
import { includes } from 'es6-polyfill'; includes('To be, or not to be, that is the question.', 'TO BE'); // => false includes('To be, or not to be, that is the question.', 'To be', 1); // => false includes('To be, or not to be, that is the question.', 'to be'); // => true includes('To be, or not to be, that is the question.', 'question', 33); // => true
Running tests
$ npm run test # mocha test
$ npm run lint # eslint
Authors
weiting-zhang
LICENSE
MIT