Security News
vlt Debuts New JavaScript Package Manager and Serverless Registry at NodeConf EU
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
array.prototype.flatmap
Advanced tools
An ES2019 spec-compliant `Array.prototype.flatMap` shim/polyfill/replacement that works as far down as ES3.
The array.prototype.flatmap package provides a polyfill for the flatMap method that was added to the Array prototype in ECMAScript 2019. This method allows you to map over an array and flatten the result by one level in a single operation. It is particularly useful for dealing with arrays of arrays or when applying a function that returns an array for each element.
Mapping and Flattening
This code demonstrates how to use flatMap to map over an array of arrays, doubling each element, and then flattening the result into a single array. The output would be [2, 4, 6, 8].
[[1, 2], [3, 4]].flatMap(x => x.map(y => y * 2))
Filtering and Mapping in One Step
This example shows how flatMap can be used to filter and map over an array simultaneously. It doubles the even numbers and removes the odd numbers, resulting in [4, 8].
[1, 2, 3, 4].flatMap(x => x % 2 === 0 ? [x * 2] : [])
Lodash's flatten method provides similar functionality to flatMap for flattening arrays, but it does not include mapping. You would need to chain it with map for similar effects, making array.prototype.flatmap more convenient for combined operations.
Underscore.js offers methods like _.flatten and _.map which can be combined to achieve similar results to flatMap. However, like lodash, this requires chaining multiple operations, whereas array.prototype.flatmap integrates both into one method.
An ES2019 spec-compliant Array.prototype.flatMap
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.flatMap
depends on a receiver (the this
value), the main export takes the array to operate on as the first argument.
npm install --save array.prototype.flatmap
var flatMap = require('array.prototype.flatmap');
var assert = require('assert');
var arr = [1, [2], [], 3];
var results = flatMap(arr, function (x, i) {
assert.equal(x, arr[i]);
return x;
});
assert.deepEqual(results, [1, 2, 3]);
var flatMap = require('array.prototype.flatmap');
var assert = require('assert');
/* when Array#flatMap is not present */
delete Array.prototype.flatMap;
var shimmedFlatMap = flatMap.shim();
var mapper = function (x) { return [x, 1]; };
assert.equal(shimmedFlatMap, flatMap.getPolyfill());
assert.deepEqual(arr.flatMap(mapper), flatMap(arr, mapper));
var flatMap = require('array.prototype.flatmap');
var assert = require('assert');
/* when Array#flatMap is present */
var shimmedIncludes = flatMap.shim();
var mapper = function (x) { return [x, 1]; };
assert.equal(shimmedIncludes, Array.prototype.flatMap);
assert.deepEqual(arr.flatMap(mapper), flatMap(arr, mapper));
Simply clone the repo, npm install
, and run npm test
v1.3.1 - 2022-11-02
npmignore
to autogenerate an npmignore file 3587a34
auto-changelog
d66bdea
define-properties
, es-abstract
d64c486
8d657d0
aud
, object-inspect
, tape
aa22741
for-each
instead of foreach
748a78d
shim
/auto
: add flatMap
to Symbol.unscopables
call-bind
, es-abstract
eslint
, @ljharb/eslint-config
, @es-shims/api
, aud
, auto-changelog
, object-inspect
, safe-publish-latest
, tape
call-bind
, es-abstract
; remove unused function-bind
prepublishOnly
, for npm 7+eslint
, @ljharb/eslint-config
, @es-shims/api
, aud
, has-strict-mode
, object-inspect
, tape
node/install
instead of node/run
; use codecov
actiones-abstract
; use call-bind
where applicableeslint
, @ljharb/eslint-config
, object-inspect
, tape
; add aud
nyc
on all testsimplementation
test; run es-shim-api
in postlint; use tape
runnerpull_request_target
eventes-abstract
(65% bundle size decrease)es-abstract
eslint
, @ljharb/eslint-config
, safe-publish-latest
, object-inspect
funding
fieldmapperFunction
, to match speces-abstract
, define-properties
eslint
, @ljharb/eslint-config
, covert
, evalmd
, object-inspect
, safe-publish-latest
, tape
node
v12.11
, v11.15
, v10.16
, v9.11
, v8.16
, v6.17
, v4.9
; use nvm install-latest-npm
npx aud
instead of nsp
or npm audit
with hoopseslint
, nsp
, tape
node
v9.6
, v6.13
eslint
, object-inspect
node
v9.4
es-abstract
eslint
, nsp
, object-inspect
node
v9.2
, v8.9
, v6.12
; pin included builds to LTSFlattenIntoArray
: add assertion that thisArg
and mapperFunction
are both passed togetherFAQs
An ES2019 spec-compliant `Array.prototype.flatMap` shim/polyfill/replacement that works as far down as ES3.
The npm package array.prototype.flatmap receives a total of 19,936,525 weekly downloads. As such, array.prototype.flatmap popularity was classified as popular.
We found that array.prototype.flatmap demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
Security News
Research
The Socket Research Team uncovered a malicious Python package typosquatting the popular 'fabric' SSH library, silently exfiltrating AWS credentials from unsuspecting developers.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.