array.prototype.push

1.0.6 • Public • Published

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

Dependencies (8)

Dev Dependencies (14)

Package Sidebar

Install

npm i array.prototype.push

Weekly Downloads

2,359

Version

1.0.6

License

MIT

Unpacked Size

25.7 kB

Total Files

18

Last publish

Collaborators

  • ljharb