1 | # safe-array-concat <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 | `Array.prototype.concat`, but made safe by ignoring Symbol.isConcatSpreadable
|
---|
11 |
|
---|
12 | ## Getting started
|
---|
13 |
|
---|
14 | ```sh
|
---|
15 | npm install --save safe-array-concat
|
---|
16 | ```
|
---|
17 |
|
---|
18 | ## Usage/Examples
|
---|
19 |
|
---|
20 | ```js
|
---|
21 | var safeConcat = require('safe-array-concat');
|
---|
22 | var assert = require('assert');
|
---|
23 |
|
---|
24 | assert.deepEqual([].concat([1, 2], 3, [[4]]), [1, 2, 3, [4]], 'arrays spread as expected with normal concat');
|
---|
25 | assert.deepEqual(safeConcat([1, 2], 3, [[4]]), [1, 2, 3, [4]], 'arrays spread as expected with safe concat');
|
---|
26 |
|
---|
27 | String.prototype[Symbol.isConcatSpreadable] = true;
|
---|
28 | assert.deepEqual([].concat('foo', Object('bar')), ['foo', 'b', 'a', 'r'], 'spreadable String objects are spread with normal concat!!!');
|
---|
29 | assert.deepEqual(safeConcat('foo', Object('bar')), ['foo', Object('bar')], 'spreadable String objects are not spread with safe concat');
|
---|
30 |
|
---|
31 | Array.prototype[Symbol.isConcatSpreadable] = false;
|
---|
32 | assert.deepEqual([].concat([1, 2], 3, [[4]]), [[], [1, 2], 3, [[4]]], 'non-concat-spreadable arrays do not spread with normal concat!!!');
|
---|
33 | assert.deepEqual(safeConcat([1, 2], 3, [[4]]), [1, 2, 3, [4]], 'non-concat-spreadable arrays still spread with safe concat');
|
---|
34 | ```
|
---|
35 |
|
---|
36 | ## Tests
|
---|
37 | Simply clone the repo, `npm install`, and run `npm test`
|
---|
38 |
|
---|
39 | [package-url]: https://npmjs.org/package/safe-array-concat
|
---|
40 | [npm-version-svg]: https://versionbadg.es/ljharb/safe-array-concat.svg
|
---|
41 | [deps-svg]: https://david-dm.org/ljharb/safe-array-concat.svg
|
---|
42 | [deps-url]: https://david-dm.org/ljharb/safe-array-concat
|
---|
43 | [dev-deps-svg]: https://david-dm.org/ljharb/safe-array-concat/dev-status.svg
|
---|
44 | [dev-deps-url]: https://david-dm.org/ljharb/safe-array-concat#info=devDependencies
|
---|
45 | [npm-badge-png]: https://nodei.co/npm/safe-array-concat.png?downloads=true&stars=true
|
---|
46 | [license-image]: https://img.shields.io/npm/l/safe-array-concat.svg
|
---|
47 | [license-url]: LICENSE
|
---|
48 | [downloads-image]: https://img.shields.io/npm/dm/safe-array-concat.svg
|
---|
49 | [downloads-url]: https://npm-stat.com/charts.html?package=safe-array-concat
|
---|
50 | [codecov-image]: https://codecov.io/gh/ljharb/safe-array-concat/branch/main/graphs/badge.svg
|
---|
51 | [codecov-url]: https://app.codecov.io/gh/ljharb/safe-array-concat/
|
---|
52 | [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/safe-array-concat
|
---|
53 | [actions-url]: https://github.com/ljharb/safe-array-concat/actions
|
---|