ite

iterlib

Iterator utilities for function-bind syntax

Showing:

Popularity

Downloads/wk

8

GitHub Stars

16

Maintenance

Last Commit

5yrs ago

Contributors

1

Package

Dependencies

0

Size (min+gzip)

2.3KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

iterlib

Array#map and similars for all iterables, with function-bind syntax

  • Build Status codecov - master

  • Build Status codecov - dev

Reference

Static functions

Some utility functions that returns iterator

resolve()

Normalize given argument to iterable

All methods below are call it to normalize this-arg

Rule

  • 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

Usage

import {resolve} from 'iterlib'

const data = getStringOrArrayOfString()

for (let str of resolve(data)) {
  handle(str)
}

range()

Iterate from start to end number, just like python3 range()

Rule

  • range(num) -> iterate from 0 to num - 1

  • range(start, end) -> iterate from start to end - 1

  • range(start, end, step) -> iterate from start to end with interval step

Usage

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

Rule

  • product(...iterables) -> iterate all possible sets of given iterables' elements

Usage

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)

Pipeline virtual methods

Virtual methods that generate another iterator

::chunk()

Iterate groups of elements with given size

Rule

  • iterable::chunk(size)

Usage

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

::compact()

Strip falsey values

Rule

  • iterable::compact()

Usage

import {compact} from 'iterlib'

const array = [3, 0, false, 'foo', '', null]

for (let elem of array::compact()) {
  console.log(elem)
}

// result: 3 foo

::concat()

Just like Array#concat

Rule

  • iterable::concat(...otherIterables)

Usage

import {concat} from 'iterlib'

console.log([...new Set([3, 4])::concat([5, 6], [7, 8])])

// result: [3, 4, 5, 6, 7, 8]

::filter()

Just like Array#filter

Rule

  • iterable::filter(element => shouldPass)

Usage

import {filter} from 'iterlib'

function onlyString () {
  return arguments::filter(arg => typeof arg === 'string')
}

[...onlyString(3, 'a', true, 'b')] // ['a', 'b']

::flatMap()

Allow multiple replacement to ::map(), same as ::map()::flatten()

Rule

  • iterable::flatMap(element => iterable)

Usage

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

Flatten nested iterable

Rule

  • iterable::flatten()

Usage

import {flatten} from 'iterlib'

const list = [
  ['a', 'b', 'c'],
  42,
  null,
  [true, false]
]

console.log([...list::flatten()])

// result: ['a', 'b', 'c', 42, true, false]

::indexed()

Iterate elements with index number

Rule

  • iterable::indexed()

Usage

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

::map()

Just like Array#map

Rule

  • iterable::map(element => newElement)

Usage

import {map} from 'iterlib'

function duplicate () {
  return arguments::map(arg => 2 * arg)
}

[...duplicate(3, 4, 5)] // [6, 8, 10]

Reducer virtual method

Virtual methods that reduce iterable to value

::every()

Just like Array#every

Rule

  • iterable::every(element => isOk) -> true if every elements are passed the callback, otherwise false

Usage

import {every} from 'iterlib'

function allFOO () {
  return arguments::every(arg => arg === 'FOO')
}

allFOO('FOO', 'FOO', 'FOO') // true
allFOO('FOO', 'Foo', 'foo') // false

::find()

Just like Array#find

Rule

  • iterable::find(element => isOk)

Usage

import {find} from 'iterlib'

const names = new Set(['Andrew', 'Anthony', 'Ada'])

console.log(names::find(name => /w/.test(name)))

// result: Andrew

::reduce()

Just like Array#reduce

Rule

  • iterable::reduce((previous, current) => next[, initValue])

Usage

import {reduce} from 'iterlib'

const names = new Set(['Andrew', 'Anthony', 'Ada'])

console.log(names::reduce((prev, next) => `${prev}, ${next}`))

// result: Andrew, Anthony, Ada

::some()

Just like Array#some

Rule

  • iterable::some(element => isOk) -> false if every elements are failed the callback, otherwise true

Usage

import {some} from 'iterlib'

function hasFOO () {
  return arguments::some(arg => arg === 'FOO')
}

hasFOO('FOO', 'Foo', 'foo') // true
hasFOO('FOo', 'FoO', 'fOO') // false

::toArray()

Just like [...itr], but as a method form

Rule

  • iterable::toArray() -> collect all iterated elements to array

Usage

import {toArray} from 'iterlib'

function wrapArray () {
  return arguments::toArray()
}

wrapArray(3, 4, 5) // [3, 4, 5]

Milestone

Original helper functions

  • resolve
  • range
  • product

Original methods

  • indexed
  • toArray

Methods in Array

  • concat
  • every
  • filter
  • find
  • map
  • reduce
  • some

Utilities in lodash

  • chunk
  • compact
  • drop
  • dropWhile
  • flatten
  • flattenDepth
  • head
  • take
  • takeWhile
  • flatMap

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100