npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

iterlib

v1.4.0

Published

Iterator utilities for function-bind syntax

Downloads

16

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

  • [x] resolve
  • [x] range
  • [x] product

Original methods

  • [x] indexed
  • [x] toArray

Methods in Array

  • [x] concat
  • [x] every
  • [x] filter
  • [x] find
  • [x] map
  • [x] reduce
  • [x] some

Utilities in lodash

  • [x] chunk
  • [x] compact
  • [ ] drop
  • [ ] dropWhile
  • [x] flatten
  • [x] flattenDepth
  • [ ] head
  • [ ] take
  • [ ] takeWhile
  • [x] flatMap