npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pragmatic-streams

v1.1.0

Published

Pragmatic operations on iterables

Downloads

10

Readme

ci codecov downloads node npm MIT npm bundle size Conventional Commits

pragmatic-streams

Set of pragmatic operations on iterables.

Description

pragmatic-streams provides utility functions to operate with iterable structures (like arrays and iterators). There operators are similar to native array functions (like filter, map, reduce etc). There are just two differences:

  • stream operators are lazy, which means all calculations performed only when needed.
  • stream operators supports are curried (support partial application), so they can be combined to get more complex operators.

Installation

As any other npm package pragmatic-streams can be added to your project by following command:

npm i -S pragmatic-streams

API

There are several stream operators provided by this package:

Most stream operators consumes at least 1 argument - iterable value. For instance, filter operator lets us to filter out some values, when map operator transforms each item of input iterable. Both returns new iterable:

import { filter, map } from 'pragmatic-streams';

const source = [ 1, 2, 3, 4, 5, 6 ];

const isOdd = item => (item & 1);
const add2 = item => item + 2;

const filtered = filter(isOdd, source); // [ 1, 3, 5 ]
const transformed = map(add2, source); // [ 3, 4, 5, 6, 7, 8 ];

const filteredAndTransformed = map(add2, filter(isOdd, source)); // [ 3, 5, 7 ]

As we mentioned before, all operators supports partial application:

const filterOdd = filter(item => (item & 1));
const add2 = map(item => item + 2);

const filtered = filterOdd(source); // [ 1, 3, 5 ]
const transformed = add2(source); // [ 3, 4, 5, 6, 7, 8 ];

const filteredAndTransformed = add2(filterOdd(source)); // [ 3, 5, 7 ]

As you can see, behavior is similar to native Array.prototype.filter() and Array.prototype.map(), so why we need this operators? Since stream operators supports partial application, they can be combined:

import { filter, map, pipe } from 'pragmatic-streams';

...

const filterAndTransform = pipe(
    filter(isOdd),
    map(add2),
);

const filteredAndTransformed = filterAndTransform(source);

Also stream operators are lazy, which means they can improve performance when combined. Each operator pulls data from previous one, so if last one need only certain amount of items, it will call previous operator accordingly:

const source = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];

// native example
...
const first3 = (_, index) => index < 3;

const result = source
    .filter(isOdd) // will call isOdd() function 10 times (for each item)
    .map(add2) // will call add2() function 5 times (for each filtered item)
    .filter(first3); // will call first3() function 5 times (for each filtered item)

// stream example
import { filter, map, take, pipe } from 'pragmatic-streams';

...

const complexOperator(
    filter(isOdd), // will call isOdd() function only until we get 3 items (5 times)
                   // because of take(3)
    map(add2),     // will call add2() function only 3 times
    take(3)        // tooks only 3 first items, so filter
);

const result = complexOperator(source);

All operators returns iterators, so we can use result only once:

...
const result = complexOperator(source);

for(let item of result) {
    console.log(item); // will print 3, 5 and 7
}

for(let item of result) {
    console.log(item); // won't print anything
}

So, if we need to re-use result, we should store data by, for instance, converting iterator into an array:

const result = [ ...complexOperator(source) ];

for(let item of result) {
    console.log(item); // will print 3, 5 and 7
}

for(let item of result) {
    console.log(item); // will print 3, 5 and 7 again
}

There are several helpful functions provided to make work with partially applied operators easier: