array.prototype.reduce

An ES5 spec-compliant `Array.prototype.reduce` shim/polyfill/replacement that works as far down as ES3.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
array.prototype.reduce
1.0.63 months ago4 years agoMinified + gzip package size for array.prototype.reduce in KB

Readme

!github actionsactions-imageactions-url !coveragecodecov-imagecodecov-url !dependency statusdeps-svgdeps-url !dev dependency statusdev-deps-svgdev-deps-url !Licenselicense-imagelicense-url !Downloadsdownloads-imagedownloads-url
!npm badgenpm-badge-pngpackage-url
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.

Example

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));

Tests

Simply clone the repo, npm install, and run npm test