1 | # array.prototype.flatmap <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.flatMap` 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 [spec](https://tc39.es/ecma262/#sec-array.prototype.flatmap).
|
---|
15 |
|
---|
16 | Because `Array.prototype.flatMap` 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.flatmap
|
---|
22 | ```
|
---|
23 |
|
---|
24 | ## Usage/Examples
|
---|
25 |
|
---|
26 | ```js
|
---|
27 | var flatMap = require('array.prototype.flatmap');
|
---|
28 | var assert = require('assert');
|
---|
29 |
|
---|
30 | var arr = [1, [2], [], 3];
|
---|
31 |
|
---|
32 | var results = flatMap(arr, function (x, i) {
|
---|
33 | assert.equal(x, arr[i]);
|
---|
34 | return x;
|
---|
35 | });
|
---|
36 |
|
---|
37 | assert.deepEqual(results, [1, 2, 3]);
|
---|
38 | ```
|
---|
39 |
|
---|
40 | ```js
|
---|
41 | var flatMap = require('array.prototype.flatmap');
|
---|
42 | var assert = require('assert');
|
---|
43 | /* when Array#flatMap is not present */
|
---|
44 | delete Array.prototype.flatMap;
|
---|
45 | var shimmedFlatMap = flatMap.shim();
|
---|
46 |
|
---|
47 | var mapper = function (x) { return [x, 1]; };
|
---|
48 |
|
---|
49 | assert.equal(shimmedFlatMap, flatMap.getPolyfill());
|
---|
50 | assert.deepEqual(arr.flatMap(mapper), flatMap(arr, mapper));
|
---|
51 | ```
|
---|
52 |
|
---|
53 | ```js
|
---|
54 | var flatMap = require('array.prototype.flatmap');
|
---|
55 | var assert = require('assert');
|
---|
56 | /* when Array#flatMap is present */
|
---|
57 | var shimmedIncludes = flatMap.shim();
|
---|
58 |
|
---|
59 | var mapper = function (x) { return [x, 1]; };
|
---|
60 |
|
---|
61 | assert.equal(shimmedIncludes, Array.prototype.flatMap);
|
---|
62 | assert.deepEqual(arr.flatMap(mapper), flatMap(arr, mapper));
|
---|
63 | ```
|
---|
64 |
|
---|
65 | ## Tests
|
---|
66 | Simply clone the repo, `npm install`, and run `npm test`
|
---|
67 |
|
---|
68 | [package-url]: https://npmjs.org/package/array.prototype.flatmap
|
---|
69 | [npm-version-svg]: https://versionbadg.es/es-shims/Array.prototype.flatMap.svg
|
---|
70 | [deps-svg]: https://david-dm.org/es-shims/Array.prototype.flatMap.svg
|
---|
71 | [deps-url]: https://david-dm.org/es-shims/Array.prototype.flatMap
|
---|
72 | [dev-deps-svg]: https://david-dm.org/es-shims/Array.prototype.flatMap/dev-status.svg
|
---|
73 | [dev-deps-url]: https://david-dm.org/es-shims/Array.prototype.flatMap#info=devDependencies
|
---|
74 | [npm-badge-png]: https://nodei.co/npm/array.prototype.flatmap.png?downloads=true&stars=true
|
---|
75 | [license-image]: https://img.shields.io/npm/l/array.prototype.flatmap.svg
|
---|
76 | [license-url]: LICENSE
|
---|
77 | [downloads-image]: https://img.shields.io/npm/dm/array.prototype.flatmap.svg
|
---|
78 | [downloads-url]: https://npm-stat.com/charts.html?package=array.prototype.flatmap
|
---|
79 | [codecov-image]: https://codecov.io/gh/es-shims/Array.prototype.flatMap/branch/main/graphs/badge.svg
|
---|
80 | [codecov-url]: https://app.codecov.io/gh/es-shims/Array.prototype.flatMap/
|
---|
81 | [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/es-shims/Array.prototype.flatMap
|
---|
82 | [actions-url]: https://github.com/es-shims/Array.prototype.flatMap/actions
|
---|