core-js
As advertising: the author is looking for a good job :)
Modular standard library for JavaScript. Includes polyfills for ECMAScript 5, ECMAScript 6: promises, symbols, collections, iterators, typed arrays, ECMAScript 7+ proposals, setImmediate, etc. Some additional features such as dictionaries or extended partial application. You can require only needed features or use it without global namespace pollution.
Array.from(new Set([1, 2, 3, 2, 1])); // => [1, 2, 3]
'*'.repeat(10); // => '**********'
Promise.resolve(32).then(x => console.log(x)); // => 32
setImmediate(x => console.log(x), 42); // => 42
Without global namespace pollution:
var core = require('core-js/library'); // With a modular system, otherwise use global `core`
core.Array.from(new core.Set([1, 2, 3, 2, 1])); // => [1, 2, 3]
core.String.repeat('*', 10); // => '**********'
core.Promise.resolve(32).then(x => console.log(x)); // => 32
core.setImmediate(x => console.log(x), 42); // => 42
Index
- Usage
- Supported engines
- Features
- Missing polyfills
- Changelog
Usage
Basic
npm i core-js
bower install core.js
// Default
require('core-js');
// Without global namespace pollution
var core = require('core-js/library');
// Shim only
require('core-js/shim');
If you need complete build for browser, use builds from core-js/client
path:
- default: Includes all features, standard and non-standard.
- as a library: Like "default", but does not pollute the global namespace (see 2nd example at the top).
- shim only: Only includes the standard methods.
Warning: if you use core-js
with the extension of native objects, require all needed core-js
modules at the beginning of entry point of your application, otherwise, conflicts may occur.
CommonJS
You can require only needed modules.
require('core-js/fn/set');
require('core-js/fn/array/from');
require('core-js/fn/array/find-index');
Array.from(new Set([1, 2, 3, 2, 1])); // => [1, 2, 3]
[1, 2, NaN, 3, 4].findIndex(isNaN); // => 2
// or, w/o global namespace pollution:
var Set = require('core-js/library/fn/set');
var from = require('core-js/library/fn/array/from');
var findIndex = require('core-js/library/fn/array/find-index');
from(new Set([1, 2, 3, 2, 1])); // => [1, 2, 3]
findIndex([1, 2, NaN, 3, 4], isNaN); // => 2
Available entry points for methods / constructors, as above examples, and namespaces: for example, core-js/es6/array
(core-js/library/es6/array
) contains all ES6 Array
features, core-js/es6
(core-js/library/es6
) contains all ES6 features.
Caveats when using CommonJS API:
modules
path is internal API, does not inject all required dependencies and can be changed in minor or patch releases. Use it only for a custom build and / or if you know what are you doing.core-js
is extremely modular and uses a lot of very tiny modules, because of that for usage in browsers bundle upcore-js
instead of usage loader for each file, otherwise, you will have hundreds of requests.
CommonJS and prototype methods without global namespace pollution
In the library
version, we can't pollute prototypes of native constructors. Because of that, prototype methods transformed to static methods like in examples above. babel
runtime
transformer also can't transform them. But with transpilers we can use one more trick - bind operator and virtual methods. Special for that, available /virtual/
entry points. Example:
import fill from 'core-js/library/fn/array/virtual/fill';
import findIndex from 'core-js/library/fn/array/virtual/find-index';
Array(10)::fill(0).map((a, b) => b * b)::findIndex(it => it && !(it % 8)); // => 4
// or
import {fill, findIndex} from 'core-js/library/fn/array/virtual';
Array(10)::fill(0).map((a, b) => b * b)::findIndex(it => it && !(it % 8)); // => 4
Custom build (from the command-line)
npm i core-js && cd node_modules/core-js && npm i
npm run grunt build:core.dict,es6 -- --blacklist=es6.promise,es6.math --library=on --path=custom uglify
Where core.dict
and es6
are modules (namespaces) names, which will be added to the build, es6.promise
and es6.math
are modules (namespaces) names, which will be excluded from the build, --library=on
is flag for build without global namespace pollution and custom
is target file name.
Available namespaces: for example, es6.array
contains ES6 Array
features, es6
contains all modules whose names start with es6
.
Custom build (from external scripts)
core-js-builder
package exports a function that takes the same parameters as the build
target from the previous section. This will conditionally include or exclude certain parts of core-js
:
```js require('core-js-builder')({ modules: ['es6', 'core.dict'], // modules / namespaces blacklist: ['es6.reflect'], /