Really simple functions for working with built-in collection types, inspired by F#'s collection modules design.
Add package using NPM or yarn
npm i --save collection-fns
yarn add collection-fns
You can import the top level modules directly:
import { Iterables, Arrays, Sets, Maps, pipe } from 'collection-fns'
Additionally, you can import the specific module functions from dist/lib/[module]
:
import { groupBy } from 'collection-fns/dist/lib/iterables'
Calculating primes lazily with iterators:
import { pipe } from 'collection-fns'
import { init, map, filter, count } from 'collection-fns/dist/lib/iterables';
const primes = pipe(
init({ from: 1, to: 100 }),
map(x => ({
x,
factors: pipe(
init({ from: 1, to: x }),
filter(y => x % y === 0)
)
})),
filter(num => count(num.factors) === 2),
map(num => num.x)
)
for (const prime of primes) {
console.log(prime)
}
Grouping numbers into odd and even buckets
import { pipe, Maps } from 'collection-fns'
import { init, groupBy } from 'collection-fns/dist/lib/arrays';
const oddAndEven = pipe(
init({ from: 1, to: 25 }),
groupBy(i => i % 2 === 0 ? 'even' : 'odd'),
Maps.ofArray
)
This works by use of partial application, however all functional can also be called directly such as:
Iterables.map(
Iterables.init({ from: 1, to: 10 }),
x => x * x))
The pipe()
function is a stand-in until the pipe (|>
) operator gets implemented in ESNext.
For pipes of up to 26 steps, the multi-argument overloads can be used where the first argument is the initial value, and all following arguments are functions take the result of the step before and returning a new result. The result from the final step is then returned as the result of the pipe.
For longer pipes there is an alternative syntax:
pipe(...)
a single initial value..then(...)
step in a pipe takes a callback that is passed the value from the previous step..result
property to get the value returned from the last step.Taking the example from the proposal linked above:
function doubleSay (str) {
return str + ", " + str;
}
function capitalize (str) {
return str[0].toUpperCase() + str.substring(1);
}
function exclaim (str) {
return str + '!';
}
The following statements are equivalent:
let result = exclaim(capitalize(doubleSay("hello")));
result // Prints: "Hello, hello!"
let result = pipe(
"hello",
doubleSay,
capitalize,
exclaim
)
result // Prints: "Hello, hello!"
let result =
pipe("hello")
.then(doubleSay)
.then(capitalize)
.then(exclaim)
.result
result // Prints: "Hello, hello!"
yarn test
: Run test suiteyarn start
: Run yarn build
in watch modeyarn test:watch
: Run test suite in interactive watch modeyarn test:prod
: Run linting and generate coverageyarn build
: Generate bundles and typings, create docsyarn lint
: Lints codeyarn commit
: Commit using conventional commit style (husky will tell you to use it if you haven't :wink:)The first array.
The second array.
Wraps the two given arrays as a single concatenated array.
The second array.
The first array.
Wraps the two given iterables as a single concatenated iterable.
The first iterable.
The second iterable.
Wraps the two given iterables as a single concatenated iterable.
The second iterable.
The first iterable.
Wraps the two given sets as a single concatenated set. NOTE: Duplicate items will be ignored.
The first set.
The second set.
Wraps the two given sets as a single concatenated set. NOTE: Duplicate items will be ignored.
The second set.
The first set.
Wraps the two given maps as a single concatenated map.
The first map.
The second map.
Wraps the two given maps as a single concatenated map.
The second map.
The first map.
Returns the source, but with the type restricted to being only iterable.
A map objext to return as an iterable.
Applies the given function to each element of the array and returns a new array comprised of the results for each element where the function returns a value.
The input collection.
A function to transform items from the input collection to a new value to be included, or undefined to be excluded.
Applies the given function to each element of the array and returns a new array comprised of the results for each element where the function returns a value.
A function to transform items from the input collection to a new value to be included, or undefined to be excluded.
Applies the given function to each element of the sequence and returns a new sequence comprised of the results for each element where the function returns a value.
The input collection.
A function to transform items from the input collection to a new value to be included, or undefined to be excluded.
Applies the given function to each element of the sequence and returns a new sequence comprised of the results for each element where the function returns a value.
A function to transform items from the input collection to a new value to be included, or undefined to be excluded.
Applies the given function to each element of the set and returns a new set comprised of the results for each element where the function returns a value. NOTE: Duplicate items will be ignored.
The input collection.
A function to transform items from the input set to a new value to be included, or undefined to be excluded.
Applies the given function to each element of the set and returns a new set comprised of the results for each element where the function returns a value. NOTE: Duplicate items will be ignored.
A function to transform items from the input set to a new value to be included, or undefined to be excluded.
Applies the given function to each entry of the map and returns a new map comprised of the results for each element where the function returns a value.
The input collection.
A function to transform entries from the input map to a new value to be included, or undefined to be excluded.
Applies the given function to each entry of the map and returns a new map comprised of the results for each element where the function returns a value.
A function to transform entries from the input map to a new value to be included, or undefined to be excluded.
Applies the given function to each element of the source array and concatenates all the results.
The input collection.
A function to transform elements of the input collection into collections that are concatenated.
Applies the given function to each element of the source array and concatenates all the results.
A function to transform elements of the input collection into collections that are concatenated.
Applies the given function to each element of the source iterable and concatenates all the results.
The input collection.
A function to transform elements of the input collection into collections that are concatenated.
Applies the given function to each element of the source iterable and concatenates all the results.
A function to transform elements of the input collection into collections that are concatenated.
Applies the given function to each element of the source set and concatenates all the results. NOTE: Duplicate items will be ignored.
The input collection.
A function to transform elements of the input set into collections that are concatenated.
Applies the given function to each element of the source set and concatenates all the results. NOTE: Duplicate items will be ignored.
A function to transform elements of the input set into collections that are concatenated.
Combines the given collection-of-arrays as a single concatenated array.
The input collection.
Evaluates to true if the given item is in the source set.
The input collection.
The item to look for.
Evaluates to true if the given item is in the source set.
The item to look for.
The input collection.
Evaluates to true if the given key is in the source map.
The input collection.
The key to look for.
Evaluates to true if the given key is in the source map.
The key to look for.
The input collection.
Returns the number of items in the array.
The input collection.
Returns an array that contains no duplicate entries according to the equality comparisons on the elements. If an element occurs multiple times in the sequence then the later occurrences are discarded.
The input collection.
Returns an array that contains no duplicate entries according to the equality comparisons on the keys returned by the given key-generating function. If an element occurs multiple times in the sequence then the later occurrences are discarded.
The input collection.
A function that transforms the array items into comparable keys.
Returns an array that contains no duplicate entries according to the equality comparisons on the keys returned by the given key-generating function. If an element occurs multiple times in the sequence then the later occurrences are discarded.
A function that transforms the array items into comparable keys.
The input collection.
Returns a iterable that contains no duplicate entries according to the equality comparisons on the keys returned by the given key-generating function. If an element occurs multiple times in the sequence then the later occurrences are discarded.
The input collection.
A function that transforms the collection items into comparable keys.
Returns a iterable that contains no duplicate entries according to the equality comparisons on the keys returned by the given key-generating function. If an element occurs multiple times in the sequence then the later occurrences are discarded.
A function that transforms the collection items into comparable keys.
The input collection.
Tests if every element of the array satisfies the given predicate.
The input collection.
A function to test against each item of the input collection.
Tests if every element of the array satisfies the given predicate.
A function to test against each item of the input collection.
The input collection.
Tests if every element of the collection satisfies the given predicate.
The input collection.
A function to test against each item of the input collection.
Tests if every element of the collection satisfies the given predicate.
A function to test against each item of the input collection.
The input collection.
Tests if any element of the set satisfies the given predicate.
The input collection.
A function to test each item of the input collection.
Tests if any element of the set satisfies the given predicate.
A function to test each item of the input collection.
The input collection.
Tests if every element of the map satisfies the given predicate.
The input collection.
A function to test each item of the input collection.
Tests if every element of the map satisfies the given predicate.
A function to test each item of the input collection.
The input collection.
Tests if any element of the array satisfies the given predicate.
The input collection.
A function to test each item of the input collection.
Tests if any element of the array satisfies the given predicate.
A function to test each item of the input collection.
The input collection.
Tests if any element of the collection satisfies the given predicate.
The input collection.
A function to test each item of the input collection.
Tests if any element of the collection satisfies the given predicate.
A function to test each item of the input collection.
The input collection.
Tests if any element of the set satisfies the given predicate.
The input collection.
A function to test each item of the input collection.
Tests if any element of the set satisfies the given predicate.
A function to test each item of the input collection.
The input collection.
Tests if any element of the map satisfies the given predicate.
The input collection.
A function to test each item of the input collection.
Tests if any element of the map satisfies the given predicate.
A function to test each item of the input collection.
The input collection.
Returns a new array containing only the elements of the collection for which the given predicate returns true.
The input collection.
A function to test whether each item in the input collection should be included in the output.
Returns a new array containing only the elements of the collection for which the given predicate returns true.
A function to test whether each item in the input collection should be included in the output.
Returns a new iterable containing only the elements of the collection for which the given predicate returns true.
The input collection.
A function to test whether each item in the input collection should be included in the output.
Returns a new iterable containing only the elements of the collection for which the given predicate returns true.
A function to test whether each item in the input collection should be included in the output.
Returns a new set containing only the elements of the source set for which the given predicate returns true.
The input collection.
A function to test whether each item in the input set should be included in the output set.
Returns a new set containing only the elements of the source set for which the given predicate returns true.
A function to test whether each item in the input set should be included in the output set.
Returns a new map containing only the elements of the map for which the given predicate returns true.
The input collection.
A function to test whether each item in the input map should be included in the output map.
Returns a new map containing only the elements of the map for which the given predicate returns true.
A function to test whether each item in the input map should be included in the output map.
Returns the first element for which the given function returns true, otherwise undefined.
The input collection.
A function to test whether an item in the collection should be returned.
Returns the first element for which the given function returns true, otherwise undefined.
A function to test whether an item in the collection should be returned.
The input collection.
Returns the first element for which the given function returns true, otherwise undefined.
The input collection.
A function to test whether an item in the collection should be returned.
Returns the first element for which the given function returns true, otherwise undefined.
A function to test whether an item in the collection should be returned.
The input collection.
Returns the first element for which the given function returns true, otherwise undefined.
The input collection.
A function to test whether an item in the collection should be returned.
Returns the first element for which the given function returns true, otherwise undefined.
A function to test whether an item in the collection should be returned.
The input collection.
Returns the value for the given key, or undefined if not found.
The input collection.
The key to lookup in the map.
Returns the value for the given key, or undefined if not found.
The key to lookup in the map.
The input collection.
Returns the first element for which the given function returns true or throws if not found.
If you don't want exceptions, use find
instead.
The input collection.
A function to test whether an item in the collection should be returned.
Returns the first element for which the given function returns true.
If you don't want exceptions, use find
instead.
A function to test whether an item in the collection should be returned.
The input collection.
Returns the first element for which the given function returns true.
The input collection.
A function to test whether an item in the collection should be returned.
Returns the first element for which the given function returns true.
A function to test whether an item in the collection should be returned.
The input collection.
Returns the first element for which the given function returns true.
The input collection.
A function to test whether an item in the collection should be returned.
Returns the first element for which the given function returns true.
A function to test whether an item in the collection should be returned.
The input collection.
Returns the value for the given key.
The input collection.
The key to lookup in the map.
Returns the value for the given key.
The key to lookup in the map.
The input collection.
Applies a key-generating function to each element of an array and yields an array of unique keys and an array of all elements that have each key.
The input collection.
A function that transforms an element of the collection into a comparable key.
Applies a key-generating function to each element of an array and yields an array of unique keys and an array of all elements that have each key.
A function that transforms an element of the collection into a comparable key.
The input collection.
Applies a key-generating function to each element of a collection and yields a iterable of unique keys and an array of all elements that have each key.
The input collection.
A function that transforms an element of the collection into a comparable key.
Applies a key-generating function to each element of a collection and yields a iterable of unique keys and an array of all elements that have each key.
A function that transforms an element of the collection into a comparable key.
The input collection.
Generates a new array containing the specified number sequence.
Generates a new array containing the specified number sequence.
The number of sequential numbers to generate.
Generates a new array containing elements generated by the initializer funciton.
The sequence of numbers to generate.
A function that generates an item in the array from a given number.
Generates a new array containing elements generated by the initializer funciton.
The number of sequential numbers to generate.
A function that generates an item in the array from a given number.
Generates a new iterable which, when iterated, will return the specified number sequence.
The sequence of numbers to generate.
Returns the number of items in the array.
The input collection.
Creates a new array whose elements are the results of applying the specified mapping to each of the elements of the source collection.
The input collection.
A function to transform items from the input collection.
Creates a new array whose elements are the results of applying the specified mapping to each of the elements of the source collection.
A function to transform items from the input collection.
Creates a new iterable whose elements are the results of applying the specified mapping to each of the elements of the source collection.
The input collection.
A function to transform items from the input collection.
Creates a new iterable whose elements are the results of applying the specified mapping to each of the elements of the source collection.
A function to transform items from the input collection.
Creates a new set whose elements are the results of applying the specified mapping to each of the elements of the source set. NOTE: Duplicate items will be ignored.
The input collection.
A function to transform items from the input collection.
Creates a new set whose elements are the results of applying the specified mapping to each of the elements of the source set. NOTE: Duplicate items will be ignored.
A function to transform items from the input collection.
Creates a new map whose values are the results of applying the specified mapping to each of the values of the source map.
The input collection.
A function to transform entries from the input collection into new values.
Creates a new map whose values are the results of applying the specified mapping to each of the values of the source map.
A function to transform entries from the input collection into new values.
Returns the maximum of the values in the collection.
The input collection.
Returns the maximum of the values returned by the selector for each element in the array.
The input collection.
A function to transform each element into a comparable value.
Returns the maximum of the values returned by the selector for each element in the array.
A function to transform each element into a comparable value.
The input collection.
Returns the maximum of the values returned by the selector for each element in the collection.
The input collection.
A function to transform each element into a comparable value.
Returns the maximum of the values returned by the selector for each element in the collection.
A function to transform each element into a comparable value.
The input collection.
Returns the mean (average) of the values in the collection.
The input collection.
Returns the mean (average) of the values returned by the selector for each element in the array.
The input collection.
A function to transform each element into a summable value.
Returns the mean (average) of the values returned by the selector for each element in the array.
A function to transform each element into a summable value.
The input collection.
Returns the mean (average) of the values returned by the selector for each element in the collection.
The input collection.
A function to transform each element into a summable value.
Returns the mean (average) of the values returned by the selector for each element in the collection.
A function to transform each element into a summable value.
The input collection.
Returns the minimum of the values in the collection.
The input collection.
Returns the minimum of the values returned by the selector for each element in the array.
The input collection.
A function to transform each element into a comparable value.
Returns the minimum of the values returned by the selector for each element in the array.
A function to transform each element into a comparable value.
The input collection.
Returns the minimum of the values returned by the selector for each element in the collection.
The input collection.
A function to transform each element into a comparable value.
Returns the minimum of the values returned by the selector for each element in the collection.
A function to transform each element into a comparable value.
The input collection.
Creates a set from the source array object. NOTE: Duplicate items will be ignored.
An array of tuples to convert to a map.
Creates an array from the source iterable object.
An iterable objext to convert to a set.
Creates a map from the source set object.
A set objext to convert to a map.
Returns an array of each element in the input collection and its predecessor, with the exception of the first element which is only returned as the predecessor of the second element.
The input collection
Starts a new pipe with the given input as an initial value.
This returns a pipe object to use for executing a series of functions.
To get the final result, use the result
property.
The initial value to start the pipe.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Calls each successive function with the result of the previous function.
The initial value to pass to the next function.
The result of the last function provided.
Returns a new array with the order of elements reversed.
The input collection.
Returns the elements of the iterable after a specified count.
The input collection.
The number of items to skip.
Returns the elements of the iterable after a specified count.
The number of items to skip.
The input collection.
Returns a new array ordered by the selected key. If no selector is specified, the elements will be compared directly.
The input collection.
An optional function to transform items of the input sequence into comparable keys.
Returns a new array ordered by the selected key. If no selector is specified, the elements will be compared directly.
An optional function to transform items of the input sequence into comparable keys.
The input collection.
Yields an iterable ordered by the selected key. If no selector is specified, the elements will be compared directly.
The input collection.
An optional function to transform items of the input sequence into comparable keys.
Yields an iterable ordered by the selected key. If no selector is specified, the elements will be compared directly.
An optional function to transform items of the input sequence into comparable keys.
The input collection.
Applies a key-generating function to each element of the array and returns a new array ordered by the keys.
The input collection.
A function to transform items of the input sequence into comparable keys.
Applies a key-generating function to each element of the array and returns a new array ordered by the keys.
A function to transform items of the input sequence into comparable keys.
The input collection.
Applies a key-generating function to each element of the collection and yields an iterable ordered by keys.
A function to transform items of the input sequence into comparable keys.
The input collection.
Applies a key-generating function to each element of the collection and yields an iterable ordered by keys.
The input collection.
A function to transform items of the input sequence into comparable keys.
Applies a key-generating function to each element of the array and returns a new array ordered by the keys, descending.
The input collection.
A function to transform items of the input sequence into comparable keys.
Applies a key-generating function to each element of the array and returns a new array ordered by the keys, descending.
A function to transform items of the input sequence into comparable keys.
The input collection.
Applies a key-generating function to each element of the collection and yields an iterable ordered by keys, descending.
The input collection.
A function to transform items of the input sequence into comparable keys.
Applies a key-generating function to each element of the collection and yields an iterable ordered by keys, descending.
A function to transform items of the input sequence into comparable keys.
The input collection.
Yields an iterable ordered by the selected key descending. If no selector is specified, the elements will be compared directly.
The input collection.
An optional function to transform items of the input sequence into comparable keys.
Yields an iterable ordered by the selected key, descending. If no selector is specified, the elements will be compared directly.
An optional function to transform items of the input sequence into comparable keys.
The input collection.
Yields an iterable ordered by the selected key descending. If no selector is specified, the elements will be compared directly.
The input collection.
An optional function to transform items of the input sequence into comparable keys.
Yields an iterable ordered by the selected key, descending. If no selector is specified, the elements will be compared directly.
An optional function to transform items of the input sequence into comparable keys.
The input collection.
Returns the sum of the values in the collection.
The input collection.
Returns the sum of the values returned by the selector for each element in the array.
The input collection.
A function to transform each element into a summable value.
Returns the sum of the values returned by the selector for each element in the array.
A function to transform each element into a summable value.
The input collection.
Returns the sum of the values returned by the selector for each element in the collection.
The input collection.
A function to transform each element into a summable value.
Returns the sum of the values returned by the selector for each element in the collection.
A function to transform each element into a summable value.
The input collection.
Returns the elements of the iterable up to a specified count.
The input collection.
The number of items to take.
Returns the elements of the iterable up to a specified count.
The number of items to take.
The input collection.
Creates an array from the source iterable object.
A set objext to convert to an array.
Generated using TypeDoc
Wraps the two given arrays as a single concatenated array.
Arrays.append([1], [2]) // [1, 2] // or using pipe pipe([1], Arrays.append([2])) // [1, 2]