Skip to content

Latest commit

 

History

History
314 lines (202 loc) · 8.7 KB

File metadata and controls

314 lines (202 loc) · 8.7 KB

Transforms

Transforms convert one iterable into another iterable.

Usage

import { transforms } from 'iterablefu'
console.log([...transforms.map(x => 2 * x, [0, 1, 2])]) // prints 0 2 4

Table of Contents

arrayToObject

Converts a sequence of Arrays to a sequence of Objects by assigning the property names to each array element in turn. The input sequence doesn't have to provide arrays, it can provide any sequence of iterable objects.

If the arrays are too long, extra values are ignored.

If the arrays are too short, the remaining properties are assigned undefined.

Parameters

  • propertyNames Iterable a sequence of property names
  • iterable Iterable a sequence of arrays (or any iterable objects)

Examples

const objects = arrayToObject(['a', 'b'], [[0, 1], [2, 3, 'a'], [4]])
// objects is [{'a': 0, 'b': 1 }, {'a': 2, 'b': 3 }, {'a': 4, 'b': undefined }]

Returns Generator for the sequence of Objects

chunk

Chunk every n items into an array, and output that array in the output sequence.

Parameters

  • n number the number of items to group into each array.
  • iterable Iterable the sequence of items to group

Examples

const a = chunk(2, [0, 1, 2, 3, 4, 5, 6])
console.log([...a]) // prints [[0, 1], [2, 3], [4, 5], [6]]

Returns Generator for the chunked sequence

diff

Execute fn(previous, current) and yields the result for each pair. Would be useful for calculating time differences between timestamps.

Parameters

  • fn Function fn(previous, current), yielding return value
  • iterable Iterable the input iterable

Examples

const a = diff((n, m) => m - n, [0, 1, 2, 3, 4])
console.log([...a]) // prints [1, 1, 1, 1]

Returns Generator if input has two or more items, output sequence is one shorter than input sequence. Otherwise, no items are output.

filter

Keeps item from input sequence when fn(item) returns truthy. Remove items from input sequence when fn(item) returns !truthy.

Parameters

  • fn Function fn(item) returns truthy when item should be removed
  • iterable Iterable the sequence to filter

Examples

const isEvenNumber = x => x % 2 === 0
const a = filter(isEvenNumber, [0, 1, 2, 3, 4, 5, 6])
console.log([...a]) // prints even numbers [0, 2, 4, 6]

Returns Generator for the filtered sequence

flatten

Flattens a sequence of items one level deep. It does not flatten strings, even though they are iterable.

Parameters

  • iterable Iterable the iterable sequence to flatten

Examples

const a = flatten([[0, 1], [2, 3], [4, 5], [6]])
console.log([...a]) // prints [0, 1, 2, 3, 4, 5, 6]

Returns Generator for the flattened sequence

flattenRecursive

Flattens a sequence by recursively returning items from each iterable in the sequence. Does not flatten strings even though they are iterable.

Parameters

  • iterable Iterable the sequence to flatten

Examples

const input = [0, [1, 2, 3], [[4, 5], [[[6, 7]], [8, 9], 10]], 11, 12]
const a = flattenRecursive(input)
console.log([...a]) // prints [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Returns Generator for the flattened sequence

map

Generates a sequence of items by calling fn(item) for each item.

Parameters

  • fn Function fn(item) returns the output item
  • iterable Iterable the sequence to map

Examples

const a = map(x => 2 * x, [0, 1, 2, 3])
console.log([...a]) // prints [0, 2, 4, 6]

Returns Generator for the mapped sequence

mapWith

Map the input sequence to the output sequence with a generator that maps one iterator to another.

This method exists solely so that ChainableIterable supports chaining for an arbitrary generator function.

Parameters

  • generatorFunction Function a function that returns an iterable object, and takes an iterable as a parameter. Typically, this will be a generator function.
  • iterable Iterable the input sequence

Examples

const fn = function * (iterable) {
  for (let x of iterable) {
    yield x * x
  }
}
const a = mapWith(fn, [0, 1, 2, 3])
console.log([...a]) // prints [0, 1, 4, 9]

Returns Generator for the mapped sequence

nth

Given a sequence of Arrays, output the nth element of each array as a sequence.

Parameters

  • index number the index of the Array to output
  • iterable Iterable the iterable to process

Examples

const input = [[0, 1], [2, 3], [4, 5]]
const a = nth(1, input)
console.log([...a]) // prints [1, 3, 5]

Returns Generator for the nth elements

pluck

Given a sequence of Objects, output the specified property of each Object as a sequence.

Parameters

  • propertyname string the property to extract from each Object
  • iterable Iterable the input sequence of Objects

Examples

const input = [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 5, 'b': 6}]
const a = pluck('a', input)
console.log([...a]) // prints [1, 3, 5]

Returns Generator for the plucked items

reject

Reject items when fn(item) returns truthy.

Parameters

  • fn Function fn(item) returns truthy when item should be removed from output sequence
  • iterable Iterable input sequence

Examples

const isEvenNumber = x => x % 2 === 0
const a = reject(isEvenNumber, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
console.log([...a]) // prints [1, 3, 5, 7, 9]

Returns Generator for the non-rejected items

take

Create an output sequence that is the first n items of the input sequence.

Parameters

  • n number the number of items to take
  • iterable Iterable the input sequence to take items from

Examples

const a = take(5, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
console.log([...a]) // prints [0, 1, 2, 3, 4]

Returns Generator for the first n items

takeWhile

Output items from the input iterable until fn(item) returns !truthy.

Parameters

  • fn Function fn(item) returns truthy to put item in the output sequence
  • iterable Iterable input sequence

Examples

const a = takeWhile(x => x != 4, [0, 1, 2, 3, 4, 5, 6])
console.log([...a]) // prints [0, 1, 2, 3]

Returns Generator for the selected items

tap

Pass the input sequence to the output sequence without change, but execute fn(item) for each item in the sequence.

Parameters

  • fn Function fn(item) is called for each item in the sequence
  • iterable Iterable the input sequence

Examples

const a = tap(console.log, [1, 2, 3, 4, 5])
[...a] // prints [1, 2, 3, 4, 5]

Returns Generator that is equivalent to the input iterable