1.0.0 • Published 7 years ago
concat-each v1.0.0
concat-each
Iterates nested loops and concatenates values onto an Array or Set.
Installation
Requires Node.js 8.3.0 or above.
npm i concat-eachAPI
The module exports a single function.
Parameters
base(Array, Set, WeakSet): The collection onto which new items should be concatenated. If you want to create a new collection, pass in[]ornew Set()ornew WeakSet().- Variadic:
...iterables(one or more of: iterable): Iterable collections of items to pass tocb. Specify multiple iterables if you want nested loops. cb(function): A callback which accepts an argument for each iterable initerablesand returns an array of items to concatenate ontobase.- Optional: Object argument:
arrays/sets/weakSets(arrays of classes/strings): Arrays of classes and/or string names of classes that should be treated as equivalent toArray/Set/WeakSet(respectively).loose(boolean): Whether or not to compare values loosely (as defined bylooselyEquals) for the purpose of testing uniqueness ifuniqueistrue. Defaults tofalse.looselyEquals(function): A callback that accepts two values and returnstrueif they are to be considered equivalent orfalseotherwise. This argument is only used iflooseistrue. If omitted, the default behavior will, among other things, consider arrays/objects to be equal if they have the same entries.unique(boolean): Whether or not to refrain from adding values that already exist inbase. Defaults tofalse. You can define what uniqueness means by using thelooseandlooselyEqualsarguments.
Return Value
Modifies and returns base.
Example
Explaining the module is best done by showing the code it replaces:
Before
const n = []
for (const i of [1, 2, 3]) {
for (const j of [2, 3, 4]) {
const sum = i + j
if (sum % 2 === 0) n.push(sum)
}
}
n // [4, 4, 6, 6]After
const concatEach = require('concat-each')
const n = concatEach([], [1, 2, 3], [2, 3, 4], (i, j) => (i + j) % 2 === 0 ? [i + j] : []) // [4, 4, 6, 6]Related
- neach: Nested forEach
1.0.0
7 years ago