Irina Glushko 2389e7160b HW1 done | 3 years ago | |
---|---|---|
.. | ||
.github | 3 years ago | |
test | 3 years ago | |
.editorconfig | 3 years ago | |
.eslintignore | 3 years ago | |
.eslintrc | 3 years ago | |
.nycrc | 3 years ago | |
CHANGELOG.md | 3 years ago | |
LICENSE | 3 years ago | |
README.md | 3 years ago | |
auto.js | 3 years ago | |
implementation.js | 3 years ago | |
index.js | 3 years ago | |
package.json | 3 years ago | |
polyfill.js | 3 years ago | |
shim.js | 3 years ago |
An ESnext spec-compliant Array.prototype.flatMap
shim/polyfill/replacement that works as far down as ES3.
This package implements the es-shim API interface. It works in an ES3-supported environment and complies with the proposed spec.
Because Array.prototype.flatMap
depends on a receiver (the this
value), the main export takes the array to operate on as the first argument.
npm install --save array.prototype.flatmap
var flatMap = require('array.prototype.flatmap');
var assert = require('assert');
var arr = [1, [2], [], 3];
var results = flatMap(arr, function (x, i) {
assert.equal(x, arr[i]);
return x;
});
assert.deepEqual(results, [1, 2, 3]);
var flatMap = require('array.prototype.flatmap');
var assert = require('assert');
/* when Array#flatMap is not present */
delete Array.prototype.flatMap;
var shimmedFlatMap = flatMap.shim();
var mapper = function (x) { return [x, 1]; };
assert.equal(shimmedFlatMap, flatMap.getPolyfill());
assert.deepEqual(arr.flatMap(mapper), flatMap(arr, mapper));
var flatMap = require('array.prototype.flatmap');
var assert = require('assert');
/* when Array#flatMap is present */
var shimmedIncludes = flatMap.shim();
var mapper = function (x) { return [x, 1]; };
assert.equal(shimmedIncludes, Array.prototype.flatMap);
assert.deepEqual(arr.flatMap(mapper), flatMap(arr, mapper));
Simply clone the repo, npm install
, and run npm test