Array#map and similars for all iterables, with function-bind syntax
Some utility functions that returns iterator
Normalize given argument to iterable
All methods below are call it to normalize this-arg
-
resolve(null)
/resolve(undefined)
-> return empty iterable -
resolve(string)
-> return single element iterable -
resolve(iterable)
-> return itself -
resolve(global)
-> return empty iterable -
resolve(else)
-> return single element iterable
import {resolve} from 'iterlib'
const data = getStringOrArrayOfString()
for (let str of resolve(data)) {
handle(str)
}
Iterate from start to end number, just like python3 range()
-
range(num)
-> iterate from0
tonum - 1
-
range(start, end)
-> iterate fromstart
toend - 1
-
range(start, end, step)
-> iterate fromstart
toend
with intervalstep
import {range} from 'iterlib'
const arr1 = [...range(3)] // [0, 1, 2]
const arr2 = [...range(2, 5)] // [2, 3, 4]
const arr3 = [...range(1, 7, 2)] // [1, 3, 5]
const arr4 = [...range(4, 1, -1)] // [4, 3, 2]
product(...iterables)
-> iterate all possible sets of given iterables' elements
import {product, range} from 'iterlib'
for (let [x, y, z] of product(range(1), range(2), range(3))) {
console.log(`(${x}, ${y}, ${z})`)
}
// result:
// (0, 0, 0)
// (0, 0, 1)
// (0, 0, 2)
// (0, 1, 0)
// (0, 1, 1)
// (0, 1, 2)
Virtual methods that generate another iterator
Iterate groups of elements with given size
iterable::chunk(size)
import {chunk} from 'iterlib'
const posData = [0, 0, 0, 1, 1, 0, 0, 2, 1, 1, 2, 0, 0, 3, 1, 2, 2, 1, 3, 0]
for (let [x, y] of posData::chunk(2)) {
console.log(`(${x}, ${y})`)
}
// result: (0, 0) (0, 1) (1, 0) (0, 2) (1, 1) ...
Strip falsey values
iterable::compact()
import {compact} from 'iterlib'
const array = [3, 0, false, 'foo', '', null]
for (let elem of array::compact()) {
console.log(elem)
}
// result: 3 foo
Just like Array#concat
iterable::concat(...otherIterables)
import {concat} from 'iterlib'
console.log([...new Set([3, 4])::concat([5, 6], [7, 8])])
// result: [3, 4, 5, 6, 7, 8]
Just like Array#filter
iterable::filter(element => shouldPass)
import {filter} from 'iterlib'
function onlyString () {
return arguments::filter(arg => typeof arg === 'string')
}
[...onlyString(3, 'a', true, 'b')] // ['a', 'b']
Allow multiple replacement to ::map(), same as ::map()::flatten()
iterable::flatMap(element => iterable)
import {flatMap} from 'iterlib'
const list = [1, 3, 6, 2, 4]
function mapper (num) {
if (num < 1 || num > 3) {
return null
}
switch (num) {
case 1: return [1]
case 2: return [1, 2]
case 3: return [1, 2, 3]
}
}
console.log([...list.flatMap(mapper)])
// result: [1, 1, 2, 3, 1, 2]
Flatten nested iterable
iterable::flatten()
import {flatten} from 'iterlib'
const list = [
['a', 'b', 'c'],
42,
null,
[true, false]
]
console.log([...list::flatten()])
// result: ['a', 'b', 'c', 42, true, false]
Iterate elements with index number
iterable::indexed()
import {indexed} from 'iterlib'
function withIndex () {
return arguments::indexed()
}
for (let {index, value} of withIndex('foo', 'bar', 'baz')) {
console.log(`${index} - ${value}`)
}
// result:
// 0 - foo
// 1 - bar
// 2 - baz
Just like Array#map
iterable::map(element => newElement)
import {map} from 'iterlib'
function duplicate () {
return arguments::map(arg => 2 * arg)
}
[...duplicate(3, 4, 5)] // [6, 8, 10]
Virtual methods that reduce iterable to value
Just like Array#every
iterable::every(element => isOk)
-> true if every elements are passed the callback, otherwise false
import {every} from 'iterlib'
function allFOO () {
return arguments::every(arg => arg === 'FOO')
}
allFOO('FOO', 'FOO', 'FOO') // true
allFOO('FOO', 'Foo', 'foo') // false
Just like Array#find
iterable::find(element => isOk)
import {find} from 'iterlib'
const names = new Set(['Andrew', 'Anthony', 'Ada'])
console.log(names::find(name => /w/.test(name)))
// result: Andrew
Just like Array#reduce
iterable::reduce((previous, current) => next[, initValue])
import {reduce} from 'iterlib'
const names = new Set(['Andrew', 'Anthony', 'Ada'])
console.log(names::reduce((prev, next) => `${prev}, ${next}`))
// result: Andrew, Anthony, Ada
Just like Array#some
iterable::some(element => isOk)
-> false if every elements are failed the callback, otherwise true
import {some} from 'iterlib'
function hasFOO () {
return arguments::some(arg => arg === 'FOO')
}
hasFOO('FOO', 'Foo', 'foo') // true
hasFOO('FOo', 'FoO', 'fOO') // false
Just like [...itr]
, but as a method form
iterable::toArray()
-> collect all iterated elements to array
import {toArray} from 'iterlib'
function wrapArray () {
return arguments::toArray()
}
wrapArray(3, 4, 5) // [3, 4, 5]
- resolve
- range
- product
- indexed
- toArray
- concat
- every
- filter
- find
- map
- reduce
- some
- chunk
- compact
- drop
- dropWhile
- flatten
- flattenDepth
- head
- take
- takeWhile
- flatMap