[79a0317] | 1 | # safe-push-apply <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
|
---|
| 2 |
|
---|
| 3 | [![github actions][actions-image]][actions-url]
|
---|
| 4 | [![coverage][codecov-image]][codecov-url]
|
---|
| 5 | [![License][license-image]][license-url]
|
---|
| 6 | [![Downloads][downloads-image]][downloads-url]
|
---|
| 7 |
|
---|
| 8 | [![npm badge][npm-badge-png]][package-url]
|
---|
| 9 |
|
---|
| 10 | Push an array of items into an array, while being robust against prototype modification.
|
---|
| 11 |
|
---|
| 12 | ## Getting started
|
---|
| 13 |
|
---|
| 14 | ```sh
|
---|
| 15 | npm install --save safe-push-apply
|
---|
| 16 | ```
|
---|
| 17 |
|
---|
| 18 | ## Usage/Examples
|
---|
| 19 |
|
---|
| 20 | ```js
|
---|
| 21 | var safePushApply = require('safe-push-apply');
|
---|
| 22 | var assert = require('assert');
|
---|
| 23 |
|
---|
| 24 | var arr = [1, 2, 3];
|
---|
| 25 |
|
---|
| 26 | var orig = Array.prototype[Symbol.iterator];
|
---|
| 27 | delete Array.prototype[Symbol.iterator];
|
---|
| 28 | assert.throws(() => {
|
---|
| 29 | try {
|
---|
| 30 | arr.push(...[3, 4, 5]);
|
---|
| 31 | } finally {
|
---|
| 32 | Array.prototype[Symbol.iterator] = orig;
|
---|
| 33 | }
|
---|
| 34 | }, 'array is not iterable anymore');
|
---|
| 35 |
|
---|
| 36 | delete Array.prototype.push;
|
---|
| 37 | safePushApply(arr, [3, 4, 5]);
|
---|
| 38 |
|
---|
| 39 | assert.deepEqual(arr, [1, 2, 3, 3, 4, 5]);
|
---|
| 40 | ```
|
---|
| 41 |
|
---|
| 42 | ## Tests
|
---|
| 43 | Simply clone the repo, `npm install`, and run `npm test`
|
---|
| 44 |
|
---|
| 45 | [package-url]: https://npmjs.org/package/safe-push-apply
|
---|
| 46 | [npm-version-svg]: https://versionbadg.es/ljharb/safe-push-apply.svg
|
---|
| 47 | [deps-svg]: https://david-dm.org/ljharb/safe-push-apply.svg
|
---|
| 48 | [deps-url]: https://david-dm.org/ljharb/safe-push-apply
|
---|
| 49 | [dev-deps-svg]: https://david-dm.org/ljharb/safe-push-apply/dev-status.svg
|
---|
| 50 | [dev-deps-url]: https://david-dm.org/ljharb/safe-push-apply#info=devDependencies
|
---|
| 51 | [npm-badge-png]: https://nodei.co/npm/safe-push-apply.png?downloads=true&stars=true
|
---|
| 52 | [license-image]: https://img.shields.io/npm/l/safe-push-apply.svg
|
---|
| 53 | [license-url]: LICENSE
|
---|
| 54 | [downloads-image]: https://img.shields.io/npm/dm/safe-push-apply.svg
|
---|
| 55 | [downloads-url]: https://npm-stat.com/charts.html?package=safe-push-apply
|
---|
| 56 | [codecov-image]: https://codecov.io/gh/ljharb/safe-push-apply/branch/main/graphs/badge.svg
|
---|
| 57 | [codecov-url]: https://app.codecov.io/gh/ljharb/safe-push-apply/
|
---|
| 58 | [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/safe-push-apply
|
---|
| 59 | [actions-url]: https://github.com/ljharb/safe-push-apply/actions
|
---|