• Stars
    star
    136
  • Rank 267,670 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 3 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Basic operations on iterables

iter-ops

Build Status

About

Basic operations on synchronous + asynchronous iterables, strictly for JavaScript native types.

image

We do not use any synthetic types / wrappers here, like Observable in RXJS, etc. It is strictly an iterable on the input, and an iterable on the output, for maximum performance, simplicity and compatibility (see Rationale).

Related repo: iter-ops-extras - addition to the main API.

Installation

$ npm i iter-ops

Usage

  • Synchronous pipeline:
import {pipe, filter, map} from 'iter-ops';

const input = [1, 2, 3, 4, 5];

const i = pipe(
    input,
    filter((a) => a % 2 === 0), // find even numbers
    map((value) => ({value})) // remap into objects
);

console.log(...i); //=> {value: 2}, {value: 4}
  • Asynchronous pipeline:
import {pipe, toAsync, distinct, delay} from 'iter-ops';

const input = [1, 2, 2, 3, 3, 4];

const i = pipe(
    toAsync(input), // make asynchronous
    distinct(), // emit unique numbers
    delay(1000) // delay each value by 1s
);
// or you can replace `pipe` + `toAsync` with just `pipeAsync`

(async function () {
    for await (const a of i) {
        console.log(a); //=> 1, 2, 3, 4 (with 1s delay)
    }
})();

See also...

API

Function pipe takes any iterable, applies all specified operators to it, and returns an extended iterable. For strict type of iterables, there are also pipeSync and pipeAsync.

Standard Operators:

All standard operators implement the same logic as Array does:

  • concat - merges current iterable with multiple values, iterators or iterables.
  • every - checks if all elements pass the predicate test.
  • filter - standard filter processor, filtering by predicate.
  • flat - flattens/expands sub-iterable elements.
  • flatMap - remaps + flattens sub-iterable elements.
  • map - standard mapping processor, remapping by predicate.
  • reduce - standard reduce processor.
  • some - checks if any element passes the predicate test.

Extended Operators:

  • aggregate - executes an aggregate on accumulated values - see Aggregates.
  • catchError - catches iteration errors - see Error Handling.
  • count - counts values, and produces a one-value iterable.
  • defaultEmpty - adds default to an empty iterable.
  • distinct - emits unique values, with optional key selector.
  • drain - drains the iterable, and then ends it.
  • empty - produces an empty iterable.
  • first - produces a one-value iterable, with the first emitted value.
  • indexBy - emits indexed values that pass the predicate test.
  • isEmpty - produces a one-value iterable, indicating if the source is empty.
  • last - produces a one-value iterable, with the last emitted value.
  • onEnd - notifies of the end of a successful iteration.
  • page - splits values into pages of fixed size (last page can be smaller).
  • repeat - repeats iterable values.
  • skip - starts emitting values after certain count.
  • skipUntil - skips until the predicate returns a truthy value.
  • skipWhile - skips while the predicate returns a truthy value.
  • split - splits values into separate lists - see Split.
  • spread - spreads iterable values.
  • take - emits up to certain number of values.
  • takeLast - emits up to certain number of the last values.
  • takeUntil - emits until the predicate returns a truthy value.
  • takeWhile - emits while the predicate returns a truthy value.
  • tap - taps into each value, without changing the output.
  • timeout - ends iteration after N milliseconds.
  • timing - measures timings for each value.
  • toArray - accumulates values into an array.
  • zip - zips values together, into an array.

Custom Operators:

See iter-ops-extras - a collection of custom operators (ones based on existing operators).

Resources:

More Repositories

1

pg-promise

PostgreSQL interface for Node.js
JavaScript
3,356
star
2

pg-promise-demo

๐Ÿ‘“ Advanced example of using pg-promise
TypeScript
278
star
3

pg-monitor

๐Ÿ’ป Events monitor for pg-promise
JavaScript
145
star
4

connection-string

Advanced URL Connection String
TypeScript
73
star
5

decomment

๐Ÿ”จ Removes comments from JSON, JavaScript, CSS, HTML, etc.
JavaScript
68
star
6

excellent

Basic DOM Component Framework
JavaScript
56
star
7

spex

๐Ÿ’ก Specialized Promise Extensions
JavaScript
51
star
8

pg-minify

Minifies PostgreSQL scripts.
JavaScript
45
star
9

sub-events

Lightweight, strongly-typed events, with monitored subscriptions.
TypeScript
36
star
10

manakin

๐Ÿฆ Prime colors for your Node.js console โ€” quick & safe.
JavaScript
29
star
11

path-value

Property path-to-value resolver, in TypeScript.
TypeScript
12
star
12

gulp-decomment

๐Ÿธ Removes comments from JSON, JavaScript, CSS, HTML, etc.
HTML
12
star
13

prime-lib

Prime number library.
TypeScript
10
star
14

pg-tuple

Tuples parser for PostgreSQL
JavaScript
7
star
15

pg-iterator

TypeScript
6
star
16

iter-ops-extras

Custom operators for iter-ops
TypeScript
5
star
17

assert-options

Generic options parameter handling
TypeScript
4
star
18

flex-value

Strongly-typed, value-resolution handler.
TypeScript
4
star
19

pg-core

PostgreSQL Core Driver for NodeJS.
TypeScript
3
star
20

pg-listener

Reliable notification listener for PostgreSQL
JavaScript
3
star
21

subcount

Observable events with monitored subscriptions.
TypeScript
3
star
22

docref

Code documentation by reference
JavaScript
2
star
23

nitrous

Performance Promise Library
JavaScript
2
star
24

connection-string-demo

Advanced use-case for connection-string
JavaScript
2
star
25

grunt-file-wrap

๐Ÿท Inject a header + footer into your text files automatically.
JavaScript
2
star
26

vparse

The smallest version parser, for client + server.
JavaScript
2
star
27

db-types

Universal database types mapper
JavaScript
2
star
28

grunt-decomment

๐Ÿท Removes comments from JSON, JavaScript, CSS, HTML, etc.
JavaScript
2
star
29

signal-ext

Angular Signal Extension
TypeScript
1
star
30

what

1
star
31

text-wrap

Fast and safe way to wrap your text with a header + footer.
JavaScript
1
star