array.prototype.filter

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

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
array.prototype.filter
511.0.27 months ago7 years agoMinified + gzip package size for array.prototype.filter 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.filter 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.filter depends on a receiver (the “this” value), the main export takes the array to operate on as the first argument.

Example

var filter = require('array.prototype.filter');
var assert = require('assert');

assert.deepEqual(filter([1, 2, 3], function (x) { return x >= 2; }), [2, 3]);
assert.deepEqual(filter([1, 2, 3], function (x) { return x <= 2; }), [1, 2]);

var filter = require('array.prototype.filter');
var assert = require('assert');
/* when Array#filter is not present */
delete Array.prototype.filter;
var shimmedFilter = filter.shim();
assert.equal(shimmedFilter, filter.getPolyfill());
var arr = [1, 2, 3];
var isOdd = function (x) { return x % 2 !== 0; };
assert.deepEqual(arr.filter(isOdd), filter(arr, isOdd));

var filter = require('array.prototype.filter');
var assert = require('assert');
/* when Array#filter is present */
var shimmedFilter = filter.shim();
assert.equal(shimmedFilter, Array.prototype.filter);
assert.deepEqual(arr.filter(isOdd), filter(arr, isOdd));

Tests

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