Really simple functions for working with native array types, inspired by F#'s Array module design.
Add package using NPM or yarn
npm i --save array-fns
yarn add array-fns
You can import the top level modules directly:
import { groupBy } from 'array-fns'
Calculating primes lazily with iterators can either be done by calling each of the basic functions:
import { count, init, map, filter } from '../src/array-fns'
const range = init({ from: 1, to: 100 })
const mapped = map(range, (x) => ({
x,
factors: filter(init({ from: 1, to: x }), (y) => x % y === 0),
}))
const filtered = filter(mapped, (num) => count(num.factors) === 2)
const primes = map(filtered, (num) => num.x)
You can also utilise the chain
methods to unwrap nested function calls:
import { initChain } from 'array-fns'
const primes = initChain({ from: 1, to: 100 })
.map((x) => ({
x,
factors: initChain({ from: 1, to: x }).filter((y) => x % y === 0),
}))
.filter((num) => num.factors.count() === 2)
.map((num) => num.x)
.toArray()
for (const prime of primes) {
console.log(prime)
}
Grouping numbers into odd and even buckets
import { initChain } from 'array-fns'
const oddAndEven = initChain({ from: 1, to: 25 })
.groupBy((i) => (i % 2 === 0 ? 'even' : 'odd'))
.toArray()
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.
Creates a new chainable sequence of operations on an array.
This can be helpful when doing a number of sequential operations on an array.
The final result can be accessed by calling .toArray()
.
Initial array of values to act on
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 source array and concatenates all the results.
The input collection.
A function to transform elements of the input collection into collections that are concatenated.
Combines the given collection-of-arrays as a single concatenated array.
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.
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 any element of the array satisfies the given predicate.
The input collection.
A function to test each item of 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 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 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.
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.
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 chainable array containing the specified number sequence.
Generates a new chainable array containing the specified number sequence.
The number of sequential numbers to generate.
Generates a new chainable array containing the specified number sequence.
The sequence of numbers to generate.
A function that generates an item in the array from a given number.
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.
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 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 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.
Creates an array from the source iterable object.
An Iterable objext to convert to an array.
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
Returns a new array with the order of elements reversed.
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.
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, descending.
The input collection.
A 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.
The input collection.
An optional function to transform items of the input sequence into comparable keys.
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.
Generated using TypeDoc
Wraps the two given arrays as a single concatenated array.
append([1], [2]) // [1, 2]