Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

@proc7ts/push-iterator

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@proc7ts/push-iterator

Push iteration protocol

Source
npmnpm
Version
2.4.0
Version published
Weekly downloads
48
45.45%
Maintainers
1
Weekly downloads
 
Created
Source

Push Iteration Protocol

NPM Build Status codecov GitHub Project API Documentation

Push iteration protocol is a faster alternative to traditional JavaScript iteration protocol.

It extends Iterator interface with special method [PushIterator__symbol](accept?), where PushIterator__symbol is a special symbol. This method pushes iterated elements to accept callback, until there is no more elements, or accept function returns true (to suspend iteration) or false (to stop it).

The method returns a push iterator instance to continue iteration with. If accept returned false then further iteration won't be possible with returned iterator.

When called without accept parameter it just returns an iterator.

Another method it extends Iterator with is isOver(), that checks whether iteration is over.

Rationale

Performance!

Traditional iteration protocol implies a call to next() method and creation of IteratorResult object on each iteration step. The push iteration protocol avoids that.

See benchmarking results for performance comparison.

JavaScript engines optimize native iteration heavily in some situations, especially for arrays. Still, in non-trivial cases the push iteration protocol demonstrates better performance, especially when it deals with push iterators rather with raw ones.

Design Goals

  • Performance.

    Push iterators are faster. Still, a lot of the code base relies on raw iterators and arrays. The library contains performance optimizations to deal with it.

  • Compatibility.

    • Push iterator implements Iterator interface.
    • Each function in this library handles JavaScript Iterator/Iterable objects in addition to push iterator/push iterable ones.
  • Tree shaking support.

    The library API represented by functions. When tree-shaken the unused ones removed from bundles.

Instant Iteration

It is quite common to just iterate over Iterable instantly rather constructing its Iterator. The library supports this. For that, a [PushIterator__symbol] method may be defined for Iterable in addition to [Symbol.iterator] one. When the library function encounters such method, it calls it to iterate over elements instead of constructing a new iterator.

API

See the full API documentation.

Push Iterable Construction

Each of the following functions returns a push iterable instance:

Iterable Consumption

Each of the following functions accepts either Iterable or push iterable:

Iterable Transformation

Each of the following functions accepts either Iterable or push iterable, and returns a push iterable:

  • filterIt(source, test) - Creates a push iterable with all source iterable elements that pass the test implemented by provided function.
  • flatMapIt(source, convert?) - First maps each element of the source iterable using a mapping function, then flattens the result into new push iterable.
  • mapIt(source, convert) - Creates a push iterable with the results of calling a provided function on every element of the source iterable.

Array Transformation

Each of the following functions accepts an array-like instance, and returns a push iterable:

  • filterArray(array, test) - Creates a push iterable with all array elements that pass the test implemented by provided function.
  • flatMapArray(array, convert?) - First maps each element of the source array using a mapping function, then flattens the result into new push iterable.
  • mapArray(array, convert) - Creates a push iterable with the results of calling a provided function on every element of the given array.

Indexed List Transformation

An indexed list of items is an object with two properties:

  • length contains the length of the list,
  • item(index: number): T | null | undefined returns the item value under the given index.

Each of the following functions accepts an indexed list of items, and returns a push iterable:

  • filterIndexed(indexed, test) - Creates a push iterable with all items of the given indexed list that pass the test implemented by the provided function.
  • flatMapIndexed(indexed, convert?) - First maps each item of the source indexed list using a mapping function, then flattens the result into new push iterable.
  • mapIndexed(array, convert) - Creates a push iterable with the results of calling a provided function on every item of the given indexed list.

Utilities

Keywords

iteration

FAQs

Package last updated on 20 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts