Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoSign 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
1.0.0
Version published
Weekly downloads
51
183.33%
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 forNext(accept: (this: void, element: T) => void | boolean): boolean method. The latter pushes iterated elements to accept function until there is no more elements, or accept function returns false yo stop iteration. The forNext() method returns true if there are more elements to iterate, or false otherwise. The former is possible only when iteration stopped, i.e. accept returned false.

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.

API

See the full [API Documentation].

Push Iterables Construction

Each of the following functions return a push iterable instance:

  • overArray(array) - Creates a push iterable over elements of array-like structure.
  • overElementsOf(...iterables) - Creates a push iterable over elements of other iterables.
  • overEntries(object) - Creates a push iterable over the property key/value entries of the given object.
  • overKeys(object) - Creates a push iterable over keys of the given object.
  • overMany(...values) - Creates a push iterable over many values.
  • overNone() - Returns a push iterable iterator without elements.
  • reverseArray(array) - Creates a push iterable over elements of array-like structure in reverse order.

Iterables Consumption

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

  • itsEach(iterable, action) - Performs the given action for each element of the given iterable.
  • itsEmpty(iterable): boolean - Checks whether the given iterable is empty.
  • itsEvery(iterable, test): boolean - Tests whether all elements of the given iterable pass the test implemented by the provided function.
  • itsFirst(iterable): T | undefined - Extracts the first element of the given iterable, if any.
  • itsReduction(iterable, reducer, initialValue): T - Applies a function against an accumulator and each element of the given iterable to reduce elements to a single value.
  • itsSome(iterable, test): boolean - Tests whether at least one element of the given iterable passes the test implemented by the provided function.

Iterables Transformation

Each of the following functions accept either Iterable or push iterable, and return a push iterable:

  • filterIt(source, test) - Creates a push iterable with all source iterable elements that pass the test implemented by the 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.

Keywords

iteration

FAQs

Package last updated on 01 Oct 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