array.prototype.push
An ES spec-compliant `Array.prototype.push` shim/polyfill/replacement that works as far down as ES3.
Last updated 3 years ago by ljharb .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install array.prototype.push 
SYNC missed versions from official npm registry.

array.prototype.push Version Badge

github actions coverage dependency status dev dependency status License Downloads

npm badge

An ES spec-compliant Array.prototype.push 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.push depends on a receiver (the “this” value), the main export takes the array to operate on as the first argument.

Engines where this is needed

Note: this list is not exhaustive.

  • in node/chrome (including node 18-19): push fails to throw on a nonwritable length property
  • IE 8 and below, and pre-ES6 engines: deleteCount isn't defaulted to length - start until ES6
  • Safari 5.0: sometimes it returns undefined
  • Safari 7/8: sparse arrays of size 1e5 or greater break
  • Opera 12.15: breaks on small sparse arrays

Example

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

var a = [1, 1, 1];
assert.equal(push(a, 1, 2), 5);
assert.deepEqual(a, [1, 1, 1, 1, 2]);
var push = require('array.prototype.push');
var assert = require('assert');
/* when Array#push is not present */
delete Array.prototype.push;
var shimmed = push.shim();
assert.equal(shimmed, push.getPolyfill());
assert.equal(shimmed, Array.prototype.push);
var a = [1, 2, 3];
var b = [1, 2, 3];
assert.equal(a.push(1, 2, 3), push(b, 1, 2, 3));
assert.deepEqual(a, b);
var push = require('array.prototype.push');
var assert = require('assert');
/* when Array#push is present */
var shimmed = push.shim();
assert.equal(shimmed, Array.prototype.push);
assert.deepEqual([1, 2, 3].push(1, 2, 3), push([1, 2, 3], 1, 2, 3));

Tests

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

Current Tags

  • 1.0.7                                ...           latest (2 years ago)

8 Versions

  • 1.0.7                                ...           2 years ago
  • 1.0.6                                ...           3 years ago
  • 1.0.5                                ...           3 years ago
  • 1.0.4                                ...           3 years ago
  • 1.0.3                                ...           4 years ago
  • 1.0.2                                ...           5 years ago
  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Total 39
Today 0
This Week 0
This Month 39
Last Day 0
Last Week 8
Last Month 0
Dependencies (6)
Dev Dependencies (14)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE