Options
All
  • Public
  • Public/Protected
  • All
Menu

Iterable Functions

npm version GitHub issues TypeDoc docs Travis Coveralls Dev Dependencies styled with prettier

Really simple functions for working with native array types, inspired by F#'s Array module design.

Features

  • Full type-safety with TypeScript
  • Zero dependency
  • Pure functions
  • Non-primative poluting
  • Chainable functions

Installation

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'

Examples

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()

NPM scripts

  • yarn test: Run test suite
  • yarn start: Run yarn build in watch mode
  • yarn test:watch: Run test suite in interactive watch mode
  • yarn test:prod: Run linting and generate coverage
  • yarn build: Generate bundles and typings, create docs
  • yarn lint: Lints code
  • yarn commit: Commit using conventional commit style (husky will tell you to use it if you haven't :wink:)

Index

Functions

append

  • append<T>(first: ReadonlyArray<T>, second: ReadonlyArray<T>): T[]
  • Wraps the two given arrays as a single concatenated array.

    example

    append([1], [2]) // [1, 2]

    Type parameters

    • T

    Parameters

    • first: ReadonlyArray<T>

      The first array.

    • second: ReadonlyArray<T>

      The second array.

    Returns T[]

chain

  • 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().

    Type parameters

    • T

    Parameters

    • source: T[]

      Initial array of values to act on

    Returns ChainableArray<T>

choose

  • choose<T, U>(source: ReadonlyArray<T>, chooser: (item: T, index: number) => U | undefined): U[]
  • 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.

    example

    choose( [1, 2, 3], x => (x % 2 === 1 ? x * 2 : undefined) ) // [2, 6]

    Type parameters

    • T

    • U

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    • chooser: (item: T, index: number) => U | undefined

      A function to transform items from the input collection to a new value to be included, or undefined to be excluded.

        • (item: T, index: number): U | undefined
        • Parameters

          • item: T
          • index: number

          Returns U | undefined

    Returns U[]

collect

  • collect<T, U>(source: ReadonlyArray<T>, mapping: (item: T, index: number) => Iterable<U>): U[]
  • Applies the given function to each element of the source array and concatenates all the results.

    example

    collect([1, 2], x => [x, x]) // [1, 1, 2, 2]

    Type parameters

    • T

    • U

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    • mapping: (item: T, index: number) => Iterable<U>

      A function to transform elements of the input collection into collections that are concatenated.

        • (item: T, index: number): Iterable<U>
        • Parameters

          • item: T
          • index: number

          Returns Iterable<U>

    Returns U[]

concat

  • concat<T>(sources: Iterable<ReadonlyArray<T>>): T[]
  • Combines the given collection-of-arrays as a single concatenated array.

    example

    concat([[1, 2], [3, 4], [5]]) // [1, 2, 3, 4, 5]

    Type parameters

    • T

    Parameters

    • sources: Iterable<ReadonlyArray<T>>

      The input collection.

    Returns T[]

count

  • count<T>(source: ReadonlyArray<T>): number
  • Returns the number of items in the array.

    example

    count([1, 2, 3, 4, 5] // 5

    Type parameters

    • T

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    Returns number

distinct

  • distinct<T>(source: ReadonlyArray<T>): T[]
  • 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.

    example

    distinct(['amy', 'bob', 'bob', 'cat']) // ['amy', 'bob', 'cat']

    Type parameters

    • T

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    Returns T[]

distinctBy

  • distinctBy<T, Key>(source: ReadonlyArray<T>, selector: (item: T, index: number) => Key): T[]
  • 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.

    example

    distinctBy([ { name: 'amy', id: 1 }, { name: 'bob', id: 2 }, { name: 'bob', id: 3 }, { name: 'cat', id: 3 } ], x => x.name ) // [{ name: 'amy', id: 1 }, { name: 'bob', id: 2 }, { name: 'cat', id: 3 }]

    Type parameters

    • T

    • Key

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    • selector: (item: T, index: number) => Key

      A function that transforms the array items into comparable keys.

        • (item: T, index: number): Key
        • Parameters

          • item: T
          • index: number

          Returns Key

    Returns T[]

every

  • every<T>(source: ReadonlyArray<T>, predicate: (item: T, index: number) => boolean): boolean
  • Tests if every element of the array satisfies the given predicate.

    example

    every([1, 2], x => x === 1) // false

    Type parameters

    • T

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    • predicate: (item: T, index: number) => boolean

      A function to test against each item of the input collection.

        • (item: T, index: number): boolean
        • Parameters

          • item: T
          • index: number

          Returns boolean

    Returns boolean

exists

  • exists<T>(source: ReadonlyArray<T>, predicate: (item: T, index: number) => boolean): boolean
  • Tests if any element of the array satisfies the given predicate.

    example

    exists([1, 2], x => x === 1) // true

    Type parameters

    • T

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    • predicate: (item: T, index: number) => boolean

      A function to test each item of the input collection.

        • (item: T, index: number): boolean
        • Parameters

          • item: T
          • index: number

          Returns boolean

    Returns boolean

filter

  • filter<T>(source: ReadonlyArray<T>, predicate: (item: T, index: number) => boolean): T[]
  • Returns a new array containing only the elements of the collection for which the given predicate returns true.

    example

    filter([1, 2, 3, 4], x => x % 2 === 0) // [2, 4]

    Type parameters

    • T

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    • predicate: (item: T, index: number) => boolean

      A function to test whether each item in the input collection should be included in the output.

        • (item: T, index: number): boolean
        • Parameters

          • item: T
          • index: number

          Returns boolean

    Returns T[]

find

  • find<T>(source: ReadonlyArray<T>, predicate: (item: T, index: number) => boolean): T | undefined
  • Returns the first element for which the given function returns true, otherwise undefined.

    example

    find([{ name: 'amy', id: 1 }, { name: 'bob', id: 2 }], x => x.name === 'bob') // { name: 'bob', id: 2 }

    Type parameters

    • T

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    • predicate: (item: T, index: number) => boolean

      A function to test whether an item in the collection should be returned.

        • (item: T, index: number): boolean
        • Parameters

          • item: T
          • index: number

          Returns boolean

    Returns T | undefined

get

  • get<T>(source: ReadonlyArray<T>, predicate: (item: T, index: number) => boolean): T
  • 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.

    throws

    If no item is found matching the criteria of the predicate.

    example

    get([{ name: 'amy', id: 1 }, { name: 'bob', id: 2 }], x => x.name === 'bob') // { name: 'bob', id: 2 }

    Type parameters

    • T

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    • predicate: (item: T, index: number) => boolean

      A function to test whether an item in the collection should be returned.

        • (item: T, index: number): boolean
        • Parameters

          • item: T
          • index: number

          Returns boolean

    Returns T

groupBy

  • groupBy<T, Key>(source: ReadonlyArray<T>, selector: (item: T, index: number) => Key): [Key, T[]][]
  • 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.

    example

    groupBy( [{ name: 'amy', age: 1 }, { name: 'bob', age: 2 }, { name: 'cat', age: 2 }], x => x.age ) // [[1, [{ name: 'amy', age: 1 }]], [2, [{ name: 'bob', age: 2 }, { name: 'cat', age: 2 }]]]

    Type parameters

    • T

    • Key

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    • selector: (item: T, index: number) => Key

      A function that transforms an element of the collection into a comparable key.

        • (item: T, index: number): Key
        • Parameters

          • item: T
          • index: number

          Returns Key

    Returns [Key, T[]][]

init

  • init(options: InitRange | InitCount): number[]
  • init(count: number): number[]
  • init<T>(options: InitRange | InitCount, initializer: (index: number) => T): T[]
  • init<T>(count: number, initializer: (index: number) => T): T[]
  • Generates a new array containing the specified number sequence.

    throws

    When the options would result in an iterable that would not complete. If this is the desired behaviour, use initInfinite.

    example

    init(3) // [0, 1, 2] init(3, x => x * x) // [0, 1, 4] init({ from: 1, to: 3 }) // [1, 2, 3] init({ from: 1, to: 2, increment: 0.5 }) // [1, 1.5, 2] init({ from: 1, to: -1 }) // [1, 0, -1] init({ start: 3, count: 3 }) // [3, 4, 5] init({ count: 3, increment: 2 }) // [0, 2, 4] init({ start: 3, count: 5, increment: 2 }) // [3, 5, 7, 9, 11]

    Parameters

    Returns number[]

  • Generates a new array containing the specified number sequence.

    throws

    When the options would result in an iterable that would not complete. If this is the desired behaviour, use initInfinite.

    example

    init(3) // [0, 1, 2] init(3, x => x * x) // [0, 1, 4] init({ from: 1, to: 3 }) // [1, 2, 3] init({ from: 1, to: 2, increment: 0.5 }) // [1, 1.5, 2] init({ from: 1, to: -1 }) // [1, 0, -1] init({ start: 3, count: 3 }) // [3, 4, 5] init({ count: 3, increment: 2 }) // [0, 2, 4] init({ start: 3, count: 5, increment: 2 }) // [3, 5, 7, 9, 11]

    Parameters

    • count: number

      The number of sequential numbers to generate.

    Returns number[]

  • Generates a new array containing elements generated by the initializer funciton.

    throws

    When the options would result in an array of infinite size.

    example

    init(3) // [0, 1, 2] init(3, x => x * x) // [0, 1, 4] init({ from: 1, to: 3 }) // [1, 2, 3] init({ from: 1, to: 2, increment: 0.5 }) // [1, 1.5, 2] init({ from: 1, to: -1 }) // [1, 0, -1] init({ start: 3, count: 3 }) // [3, 4, 5] init({ count: 3, increment: 2 }) // [0, 2, 4] init({ start: 3, count: 5, increment: 2 }) // [3, 5, 7, 9, 11]

    Type parameters

    • T

    Parameters

    • options: InitRange | InitCount

      The sequence of numbers to generate.

    • initializer: (index: number) => T

      A function that generates an item in the array from a given number.

        • (index: number): T
        • Parameters

          • index: number

          Returns T

    Returns T[]

  • Generates a new array containing elements generated by the initializer funciton.

    throws

    When the options would result in an array of infinite size.

    example

    init(3) // [0, 1, 2] init(3, x => x * x) // [0, 1, 4] init({ from: 1, to: 3 }) // [1, 2, 3] init({ from: 1, to: 2, increment: 0.5 }) // [1, 1.5, 2] init({ from: 1, to: -1 }) // [1, 0, -1] init({ start: 3, count: 3 }) // [3, 4, 5] init({ count: 3, increment: 2 }) // [0, 2, 4] init({ start: 3, count: 5, increment: 2 }) // [3, 5, 7, 9, 11]

    Type parameters

    • T

    Parameters

    • count: number

      The number of sequential numbers to generate.

    • initializer: (index: number) => T

      A function that generates an item in the array from a given number.

        • (index: number): T
        • Parameters

          • index: number

          Returns T

    Returns T[]

initChain

  • Generates a new chainable array containing the specified number sequence.

    throws

    When the options would result in an array of infinite length.

    example

    init(3) // [0, 1, 2] init(3, x => x * x) // [0, 1, 4] init({ from: 1, to: 3 }) // [1, 2, 3] init({ from: 1, to: 2, increment: 0.5 }) // [1, 1.5, 2] init({ from: 1, to: -1 }) // [1, 0, -1] init({ start: 3, count: 3 }) // [3, 4, 5] init({ count: 3, increment: 2 }) // [0, 2, 4] init({ start: 3, count: 5, increment: 2 }) // [3, 5, 7, 9, 11]

    Parameters

    Returns ChainableArray<number>

  • Generates a new chainable array containing the specified number sequence.

    throws

    When the options would result in an array of infinite length.

    example

    init(3) // [0, 1, 2] init(3, x => x * x) // [0, 1, 4] init({ from: 1, to: 3 }) // [1, 2, 3] init({ from: 1, to: 2, increment: 0.5 }) // [1, 1.5, 2] init({ from: 1, to: -1 }) // [1, 0, -1] init({ start: 3, count: 3 }) // [3, 4, 5] init({ count: 3, increment: 2 }) // [0, 2, 4] init({ start: 3, count: 5, increment: 2 }) // [3, 5, 7, 9, 11]

    Parameters

    • count: number

      The number of sequential numbers to generate.

    Returns ChainableArray<number>

  • Generates a new chainable array containing the specified number sequence.

    throws

    When the options would result in an array of infinite length.

    example

    init(3) // [0, 1, 2] init(3, x => x * x) // [0, 1, 4] init({ from: 1, to: 3 }) // [1, 2, 3] init({ from: 1, to: 2, increment: 0.5 }) // [1, 1.5, 2] init({ from: 1, to: -1 }) // [1, 0, -1] init({ start: 3, count: 3 }) // [3, 4, 5] init({ count: 3, increment: 2 }) // [0, 2, 4] init({ start: 3, count: 5, increment: 2 }) // [3, 5, 7, 9, 11]

    Type parameters

    • T

    Parameters

    • options: InitRange | InitCount

      The sequence of numbers to generate.

    • initializer: (index: number) => T

      A function that generates an item in the array from a given number.

        • (index: number): T
        • Parameters

          • index: number

          Returns T

    Returns ChainableArray<T>

length

  • length<T>(source: ReadonlyArray<T>): number
  • Returns the number of items in the array.

    example

    length([1, 2, 3, 4, 5] // 5

    Type parameters

    • T

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    Returns number

map

  • map<T, U>(source: ReadonlyArray<T>, mapping: (item: T, index: number) => U): U[]
  • Creates a new array whose elements are the results of applying the specified mapping to each of the elements of the source collection.

    example

    map([1, 2], x => x * 2) // [2, 4]

    Type parameters

    • T

    • U

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    • mapping: (item: T, index: number) => U

      A function to transform items from the input collection.

        • (item: T, index: number): U
        • Parameters

          • item: T
          • index: number

          Returns U

    Returns U[]

max

  • max(source: ReadonlyArray<number>): number
  • Returns the maximum of the values in the collection.

    throws

    If the collection is empty.

    example

    max([21, 2, 18]) // 21

    Parameters

    • source: ReadonlyArray<number>

      The input collection.

    Returns number

maxBy

  • maxBy<T>(source: ReadonlyArray<T>, selector: (item: T) => number): number
  • Returns the maximum of the values returned by the selector for each element in the array.

    throws

    If the collection is empty.

    example

    maxBy( [{ name: 'amy', age: 21 }, { name: 'bob', age: 2 }, { name: 'cat', age: 18 }], x => x.age ) // 21

    Type parameters

    • T

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    • selector: (item: T) => number

      A function to transform each element into a comparable value.

        • (item: T): number
        • Parameters

          • item: T

          Returns number

    Returns number

mean

  • mean(source: ReadonlyArray<number>): number
  • Returns the mean (average) of the values in the collection.

    throws

    If the collection is empty.

    example

    mean([21, 2, 18, 39]) // 20

    Parameters

    • source: ReadonlyArray<number>

      The input collection.

    Returns number

meanBy

  • meanBy<T>(source: ReadonlyArray<T>, selector: (item: T) => number): number
  • Returns the mean (average) of the values returned by the selector for each element in the array.

    throws

    If the collection is empty.

    example

    meanBy( [{ name: 'amy', age: 21 }, { name: 'bob', age: 3 }, { name: 'cat', age: 18 }], x => x.age ) // 14

    Type parameters

    • T

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    • selector: (item: T) => number

      A function to transform each element into a summable value.

        • (item: T): number
        • Parameters

          • item: T

          Returns number

    Returns number

min

  • min(source: ReadonlyArray<number>): number
  • Returns the minimum of the values in the collection.

    throws

    If the collection is empty.

    example

    min([21, 2, 18]) // 2

    Parameters

    • source: ReadonlyArray<number>

      The input collection.

    Returns number

minBy

  • minBy<T>(source: ReadonlyArray<T>, selector: (item: T) => number): number
  • Returns the minimum of the values returned by the selector for each element in the array.

    throws

    If the collection is empty.

    example

    minBy( [{ name: 'amy', age: 21 }, { name: 'bob', age: 2 }, { name: 'cat', age: 18 }], x => x.age ) // 2

    Type parameters

    • T

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    • selector: (item: T) => number

      A function to transform each element into a comparable value.

        • (item: T): number
        • Parameters

          • item: T

          Returns number

    Returns number

ofIterable

  • ofIterable<T>(source: Iterable<T>): T[]
  • Creates an array from the source iterable object.

    example

    ofIterable((function*() { yield 1 yield 2 })()) // [1, 2]

    Type parameters

    • T

    Parameters

    • source: Iterable<T>

      An Iterable objext to convert to an array.

    Returns T[]

pairwise

  • pairwise<T>(source: ReadonlyArray<T>): [T, T][]
  • 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.

    example

    pairwise([1, 2, 3]) // [[1, 2], [2, 3]]

    Type parameters

    • T

    Parameters

    • source: ReadonlyArray<T>

      The input collection

    Returns [T, T][]

reverse

  • reverse<T>(source: ReadonlyArray<T>): T[]
  • Returns a new array with the order of elements reversed.

    example

    reverse([8, 3, 5]) // [5, 3, 8]

    Type parameters

    • T

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    Returns T[]

sort

  • sort<T, Key>(source: ReadonlyArray<T>, selector?: undefined | ((item: T) => Key)): T[]
  • Returns a new array ordered by the selected key. If no selector is specified, the elements will be compared directly.

    example

    sort([21, 2, 18]) // [2, 18, 21]

    // with selector sort( [{ name: 'amy', age: 21 }, { name: 'bob', age: 2 }, { name: 'cat', age: 18 }], x => x.age ) // [{ name: 'bob', age: 2 }, { name: 'cat', age: 18 }, { name: 'amy', age: 21 }]

    Type parameters

    • T

    • Key

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    • Optional selector: undefined | ((item: T) => Key)

      An optional function to transform items of the input sequence into comparable keys.

    Returns T[]

sortBy

  • sortBy<T, Key>(source: ReadonlyArray<T>, selector: (item: T) => Key): T[]
  • Applies a key-generating function to each element of the array and returns a new array ordered by the keys.

    example

    sortBy( [{ name: 'amy', age: 21 }, { name: 'bob', age: 2 }, { name: 'cat', age: 18 }], x => x.age ) // [{ name: 'bob', age: 2 }, { name: 'cat', age: 18 }, { name: 'amy', age: 21 }]

    Type parameters

    • T

    • Key

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    • selector: (item: T) => Key

      A function to transform items of the input sequence into comparable keys.

        • (item: T): Key
        • Parameters

          • item: T

          Returns Key

    Returns T[]

sortByDescending

  • sortByDescending<T, Key>(source: ReadonlyArray<T>, selector: (item: T) => Key): T[]
  • Applies a key-generating function to each element of the array and returns a new array ordered by the keys, descending.

    example

    // with selector sortByDescending( [{ name: 'amy', age: 21 }, { name: 'bob', age: 2 }, { name: 'cat', age: 18 }], x => x.age ) // [{ name: 'amy', age: 21 }, { name: 'cat', age: 18 }, { name: 'bob', age: 2 }]

    Type parameters

    • T

    • Key

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    • selector: (item: T) => Key

      A function to transform items of the input sequence into comparable keys.

        • (item: T): Key
        • Parameters

          • item: T

          Returns Key

    Returns T[]

sortDescending

  • sortDescending<T, Key>(source: ReadonlyArray<T>, selector?: undefined | ((item: T) => Key)): T[]
  • Yields an iterable ordered by the selected key descending. If no selector is specified, the elements will be compared directly.

    example

    sortDescending([21, 2, 18]) // [21, 18, 2]

    // with selector sortDescending( [{ name: 'amy', age: 21 }, { name: 'bob', age: 2 }, { name: 'cat', age: 18 }], x => x.age ) // [{ name: 'amy', age: 21 }, { name: 'cat', age: 18 }, { name: 'bob', age: 2 }]

    Type parameters

    • T

    • Key

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    • Optional selector: undefined | ((item: T) => Key)

      An optional function to transform items of the input sequence into comparable keys.

    Returns T[]

sum

  • sum(source: ReadonlyArray<number>): number
  • Returns the sum of the values in the collection.

    example

    sum([21, 2, 18]) // 41

    Parameters

    • source: ReadonlyArray<number>

      The input collection.

    Returns number

sumBy

  • sumBy<T>(source: ReadonlyArray<T>, selector: (item: T) => number): number
  • Returns the sum of the values returned by the selector for each element in the array.

    example

    sumBy( [{ name: 'amy', age: 21 }, { name: 'bob', age: 2 }, { name: 'cat', age: 18 }], x => x.age ) // 41

    Type parameters

    • T

    Parameters

    • source: ReadonlyArray<T>

      The input collection.

    • selector: (item: T) => number

      A function to transform each element into a summable value.

        • (item: T): number
        • Parameters

          • item: T

          Returns number

    Returns number

Legend

  • Constructor
  • Method
  • Property
  • Private property

Generated using TypeDoc