/async-iterable-fns

Functions for working with async iterable types

Primary LanguageTypeScriptMIT LicenseMIT

Async Iterable Functions

npm version GitHub issues TypeDoc docs Travis Coveralls Dev Dependencies styled with prettier

Really simple functions for working with async iterable types, inspired by F#'s collection modules design.

Features

  • Full type-safety with TypeScript
  • Zero dependency
  • Pure functions

Installation

Add package using NPM or yarn

npm i --save async-iterable-fns
yarn add async-iterable-fns

You can import the top level modules directly:

import { groupBy } from 'async-iterable-fns'

Examples

Calculating primes lazily with iterators can either be done by calling each of the basic functions:

import { count, initRaw, map, filter } from 'async-iterable-fns'

const findPrimes = async () => {
  const range = initRaw({ from: 1, to: 100 })
  const mapped = map(range, (x) => ({
    x,
    factors: filter(initRaw({ from: 1, to: x }), (y) => x % y === 0),
  }))
  const filtered = filter(mapped, async (num) => (await count(num.factors)) === 2)
  const primes = map(filtered, (num) => num.x)
  return await toArray(primes)
}

or can utilise the chainable methods:

import { init } from 'async-iterable-fns'

const findPrimesChained = async () => {
  const primes = init({ from: 1, to: 100 })
    .map((x) => ({
      x,
      factors: init({ from: 1, to: x }).filter((y) => x % y === 0),
    }))
    .filter(async (num) => (await num.factors.count()) === 2)
    .map((num) => num.x)

  for await (const prime of primes) {
    console.log(prime)
  }
}

NPM scripts

  • yarn test: Run test suite
  • yarn start: Run yarn build in watch mode
  • yarn test:watch: Run test suite in interactive watch mode
  • yarn test:prod: Run linting and generate coverage
  • yarn build: Generate bundles and typings, create docs
  • yarn lint: Lints code
  • yarn commit: Commit using conventional commit style (husky will tell you to use it if you haven't 😉)