Bakhtina Sofya b84cb877be 1st comm | 2 weeks ago | |
---|---|---|
.. | ||
test | 2 weeks ago | |
.editorconfig | 2 weeks ago | |
.eslintrc | 2 weeks ago | |
.nycrc | 2 weeks ago | |
CHANGELOG.md | 2 weeks ago | |
LICENSE | 2 weeks ago | |
README.md | 2 weeks ago | |
auto.js | 2 weeks ago | |
implementation.js | 2 weeks ago | |
index.js | 2 weeks ago | |
package.json | 2 weeks ago | |
polyfill.js | 2 weeks ago | |
shim.js | 2 weeks ago |
An ES5 spec-compliant Array.prototype.reduce
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 spec.
Because Array.prototype.reduce
depends on a receiver (the “this” value), the main export takes the array to operate on as the first argument.
var reduce = require('array.prototype.reduce');
var assert = require('assert');
assert.equal(reduce([1, 2, 3], function (prev, x) { return prev + x; }), 6);
assert.equal(reduce([1, 2, 3], function (prev, x) { return prev + x; }, 1), 7);
var reduce = require('array.prototype.reduce');
var assert = require('assert');
/* when Array#reduce is not present */
delete Array.prototype.reduce;
var shimmed = reduce.shim();
assert.equal(shimmed, reduce.getPolyfill());
var arr = [1, 2, 3];
var sum = function (a, b) { return a + b; };
assert.equal(arr.reduce(sum), reduce(arr, sum));
assert.equal(arr.reduce(sum), 6);
assert.equal(arr.reduce(sum, 1), 7);
var reduce = require('array.prototype.reduce');
var assert = require('assert');
/* when Array#reduce is present */
var shimmed = reduce.shim();
assert.equal(shimmed, Array.prototype.reduce);
assert.equal(arr.reduce(sum), reduce(arr, sum));
Simply clone the repo, npm install
, and run npm test