1 | # array.prototype.flat <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
|
---|
2 |
|
---|
3 | [![github actions][actions-image]][actions-url]
|
---|
4 | [![coverage][codecov-image]][codecov-url]
|
---|
5 | [![dependency status][deps-svg]][deps-url]
|
---|
6 | [![dev dependency status][dev-deps-svg]][dev-deps-url]
|
---|
7 | [![License][license-image]][license-url]
|
---|
8 | [![Downloads][downloads-image]][downloads-url]
|
---|
9 |
|
---|
10 | [![npm badge][npm-badge-png]][package-url]
|
---|
11 |
|
---|
12 | An ES2019 spec-compliant `Array.prototype.flat` shim/polyfill/replacement that works as far down as ES3.
|
---|
13 |
|
---|
14 | This package implements the [es-shim API](https://github.com/es-shims/api) interface. It works in an ES3-supported environment and complies with the proposed [spec](https://tc39.github.io/proposal-flatMap/).
|
---|
15 |
|
---|
16 | Because `Array.prototype.flat` depends on a receiver (the `this` value), the main export takes the array to operate on as the first argument.
|
---|
17 |
|
---|
18 | ## Getting started
|
---|
19 |
|
---|
20 | ```sh
|
---|
21 | npm install --save array.prototype.flat
|
---|
22 | ```
|
---|
23 |
|
---|
24 | ## Usage/Examples
|
---|
25 |
|
---|
26 | ```js
|
---|
27 | var flat = require('array.prototype.flat');
|
---|
28 | var assert = require('assert');
|
---|
29 |
|
---|
30 | var arr = [1, [2], [], 3, [[4]]];
|
---|
31 |
|
---|
32 | assert.deepEqual(flat(arr, 1), [1, 2, 3, [4]]);
|
---|
33 | ```
|
---|
34 |
|
---|
35 | ```js
|
---|
36 | var flat = require('array.prototype.flat');
|
---|
37 | var assert = require('assert');
|
---|
38 | /* when Array#flat is not present */
|
---|
39 | delete Array.prototype.flat;
|
---|
40 | var shimmedFlat = flat.shim();
|
---|
41 |
|
---|
42 | assert.equal(shimmedFlat, flat.getPolyfill());
|
---|
43 | assert.deepEqual(arr.flat(), flat(arr));
|
---|
44 | ```
|
---|
45 |
|
---|
46 | ```js
|
---|
47 | var flat = require('array.prototype.flat');
|
---|
48 | var assert = require('assert');
|
---|
49 | /* when Array#flat is present */
|
---|
50 | var shimmedIncludes = flat.shim();
|
---|
51 |
|
---|
52 | var mapper = function (x) { return [x, 1]; };
|
---|
53 |
|
---|
54 | assert.equal(shimmedIncludes, Array.prototype.flat);
|
---|
55 | assert.deepEqual(arr.flat(mapper), flat(arr, mapper));
|
---|
56 | ```
|
---|
57 |
|
---|
58 | ## Tests
|
---|
59 | Simply clone the repo, `npm install`, and run `npm test`
|
---|
60 |
|
---|
61 | [package-url]: https://npmjs.org/package/array.prototype.flat
|
---|
62 | [npm-version-svg]: https://versionbadg.es/es-shims/Array.prototype.flat.svg
|
---|
63 | [deps-svg]: https://david-dm.org/es-shims/Array.prototype.flat.svg
|
---|
64 | [deps-url]: https://david-dm.org/es-shims/Array.prototype.flat
|
---|
65 | [dev-deps-svg]: https://david-dm.org/es-shims/Array.prototype.flat/dev-status.svg
|
---|
66 | [dev-deps-url]: https://david-dm.org/es-shims/Array.prototype.flat#info=devDependencies
|
---|
67 | [npm-badge-png]: https://nodei.co/npm/array.prototype.flat.png?downloads=true&stars=true
|
---|
68 | [license-image]: https://img.shields.io/npm/l/array.prototype.flat.svg
|
---|
69 | [license-url]: LICENSE
|
---|
70 | [downloads-image]: https://img.shields.io/npm/dm/array.prototype.flat.svg
|
---|
71 | [downloads-url]: https://npm-stat.com/charts.html?package=array.prototype.flat
|
---|
72 | [codecov-image]: https://codecov.io/gh/es-shims/Array.prototype.flat/branch/main/graphs/badge.svg
|
---|
73 | [codecov-url]: https://app.codecov.io/gh/es-shims/Array.prototype.flat/
|
---|
74 | [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/es-shims/Array.prototype.flat
|
---|
75 | [actions-url]: https://github.com/es-shims/Array.prototype.flat/actions
|
---|