Options
All
  • Public
  • Public/Protected
  • All
Menu

fluent-streams

The project is deprecated

Now, with JS Array getting more and more powerful, especially with upcoming groupBy() and at() methods, projects like this become unneeded. Please use standard APIs.

Below is the original README.

Fluent Streams

Travis CI NPM version Read the docs

Fluent Streams is a JavaScript and TypeScript library providing rich API for lazy iterables (arrays, sets, etc) processing. The main construct is a Stream which is a thin stateless wrapper over an iterable; there is also an Optional modeling existing or lacking value.

Getting started

Install

npm i fluent-streams

Add polyfills

Fluent Streams lib is distributed compiled to ES5, so no additional transpilation needed, however it needs the following polyfills in older environments:

  • Array.isArray()
  • Array.prototype.forEach()
  • Array.prototype[@@iterator]()
  • Map
  • Object.keys()
  • Set
  • Symbol
  • Symbol.iterator

Optionally:

Use

To create or generate a stream or an optional use one of factory functions; the most generic is stream() which creates a stream from any iterable.

Fluent Streams exports all functions and interfaces by name from index files, so you import everything with import { ... } from 'fluent-streams'.

import { stream } from 'fluent-streams';

stream(['Aspire', 'to', 'inspire', 'before', 'we', 'expire'])
    .flatMap(word => word)
    .groupBy(char => char)
    .map(([char, chars]) => ([char, chars.length]))
    .sortBy(([_, count]) => -count)
    .take(3)
    .toArray()     // => [['e', 7], ['i', 4], ['r', 4]]

Learn

All exported members are documented with TSDoc. Docs are also available online. The concept of stream and its main properties are described in Stream interface documentation.

Why Fluent Streams

The lib is not the first to provide such a functionality. However, there are things specific to the lib:

  • Quite small: 18.6 kB minified, 4.1 kB gzipped as per Bundlephobia
  • There is Optional. While some people assume it just to be a boilerplate, it provides the means to get back to stream with flatMapToStream() without breaking a fluent pipeline.
  • Stream and Optional seamlessly interoperate with ES6 iterables — they are iterables themselves, and may be iterated over.
  • Stream uses similar names and, where possible, signatures of ES Array methods — it's easy to embed Fluent Streams into existing code as well as get rid of the lib.
  • The lib is optimized for arrays — much of operations work faster (and produce less garbage) if an input is an array.
  • The lib is very heavily tested.

Benchmarks

Fluent Streams is compared to the native JS Array and two other popular very similar libs. Because each lib is specific, there is no the only winner, and in general all libs show reasonable performance. Results are here, and benchmarks are here.

Inspirations and acknowledgements

The following great libs influenced Fluent Streams the most:

License

The library is provided under the ISC license.

Generated using TypeDoc