Transforms convert one iterable into another iterable.
import { transforms } from 'iterablefu'
console.log([...transforms.map(x => 2 * x, [0, 1, 2])]) // prints 0 2 4- arrayToObject
- chunk
- diff
- filter
- flatten
- flattenRecursive
- map
- mapWith
- nth
- pluck
- reject
- take
- takeWhile
- tap
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.
propertyNamesIterable a sequence of property namesiterableIterable a sequence of arrays (or any iterable objects)
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 every n items into an array, and output that array in the output sequence.
nnumber the number of items to group into each array.iterableIterable the sequence of items to group
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
Execute fn(previous, current) and yields the result for each pair. Would be useful for calculating time differences between timestamps.
fnFunction fn(previous, current), yielding return valueiterableIterable the input iterable
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.
Keeps item from input sequence when fn(item) returns truthy. Remove items from input sequence when fn(item) returns !truthy.
fnFunction fn(item) returns truthy when item should be removediterableIterable the sequence to filter
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
Flattens a sequence of items one level deep. It does not flatten strings, even though they are iterable.
iterableIterable the iterable sequence to flatten
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
Flattens a sequence by recursively returning items from each iterable in the sequence. Does not flatten strings even though they are iterable.
iterableIterable the sequence to flatten
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
Generates a sequence of items by calling fn(item) for each item.
fnFunction fn(item) returns the output itemiterableIterable the sequence to map
const a = map(x => 2 * x, [0, 1, 2, 3])
console.log([...a]) // prints [0, 2, 4, 6]Returns Generator for the mapped sequence
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.
generatorFunctionFunction a function that returns an iterable object, and takes an iterable as a parameter. Typically, this will be a generator function.iterableIterable the input sequence
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
Given a sequence of Arrays, output the nth element of each array as a sequence.
indexnumber the index of the Array to outputiterableIterable the iterable to process
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
Given a sequence of Objects, output the specified property of each Object as a sequence.
propertynamestring the property to extract from each ObjectiterableIterable the input sequence of Objects
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 items when fn(item) returns truthy.
fnFunction fn(item) returns truthy when item should be removed from output sequenceiterableIterable input sequence
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
Create an output sequence that is the first n items of the input sequence.
nnumber the number of items to takeiterableIterable the input sequence to take items from
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
Output items from the input iterable until fn(item) returns !truthy.
fnFunction fn(item) returns truthy to put item in the output sequenceiterableIterable input sequence
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
Pass the input sequence to the output sequence without change, but execute fn(item) for each
item in the sequence.
fnFunctionfn(item)is called for each item in the sequenceiterableIterable the input sequence
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