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

@timkendrick/transducers

v0.0.3

Published

Simple, high-performance transducers for JavaScript

Downloads

17

Readme

@timkendrick/transducers

npm version Stability

Simple, high-performance transducers for JavaScript

Installation

npm install @timkendrick/transducers --save

Overview

Transducers provide an elegant, high-performance abstraction for manipulating collections. They allow you to compose complex, reusable transformations that can be applied to any kind of collection, and can produce different output formats depending on the choice of underlying reducer.

import { compose, map, filter, take, array } from '@timkendrick/transducers';

// Define a source iterator
const users = getUsers(10000);

// Compose a transducer that expresses a transformation
const transducer = compose(
  filter((user) => user.admin === true)
  map((user) => `${user.firstName} ${user.lastName}`),
  take(10),
);

// Apply the transformation using the 'array' reducer (which outputs an array)
const results = transduce(transducer, array, users);

// Log an array containing the full names of the first ten admin users
console.log(results);

One of the primary advantages of transducers is their efficiency. See the performance benchmarks for more details.

Usage

compose(transducer1, transducer2, ...transducerN)

Compose a series of transducers. The transformations expressed by the transducers will be processed in order from left-to-right.

The resulting transducer can be further composed for combining with other transducers.

transduce(transducer, reducer, items, [initialValue])

Apply the transducer to an iterable collection of items, using the specified reducer function and initialValue argument to construct the output.

The reducer can optionally refer to a transducer that defines an 'init' method (e.g. the bundled array reducer), in which case the initialValue is optional. In all other cases the initialValue argument is required.

Included operators

While not an exhaustive set by any means, various operators have been bundled for convenience:

  • distinct: ignore repeated items
  • empty: ignore all items
  • filter(predicate): take only the items that conform to the predicate function
  • first: take the first item only
  • flatMap(project): for each item, map to a collection using project, and merge the results
  • flatten: flatten a higher-order collection by one dimension
  • last: take the last item only
  • map(transform): transform each item to the result of mapping it through the project function
  • mapAll(transform): transform the entire output set by mapping it through the transform function
  • scan(reducer, seed): for each item, return the result of calling the reducer function with the previous result and the current item
  • skip(count): ignore the first count items
  • skipUntil(predicate): ignore items until one conforms to the predicate function
  • skipWhile(predicate): ignore items until one does not conform to the predicate function
  • slice(start, length): ignore items until the start index, and take length items from that point
  • sort(compare): sort the entire output set using the compare comparison function
  • take(count): take only the first count items
  • takeUntil(predicate): take all items until one conforms to the predicate function
  • takeWhile(predicate): take all items until one does not conform to the predicate function

See the tests for the bundled operators for more details.

The bundled operators can all be composed using the compose() helper to produce more specific operators.

Included reducers

Transducers can be used with any type of reducer. Some of the most common ones are bundled for convenience:

array

Combines the transformed collection into a JavaScript array:

import { skip, transduce } from '@timkendrick/transducers';

transduce(skip(1), array, ['foo', 'bar', 'baz']); // ['bar', 'baz']

sum

Combines the transformed collection into a sum total:

import { skip, sum } from '@timkendrick/transducers';

transduce(skip(1), sum, [1, 2, 3, 4, 5]); // 14

log

Joins the transformed collection into a string with items separated by newline characters:

import { take, log } from '@timkendrick/transducers';

transduce(skip(1), log, ['foo', 'bar', 'baz']); // "Foo\nBar\nBaz"

Interoperability with other libraries

This package conforms to the Transducers spec, allowing for full interoperability with all packages that support transducers.

Reducing bundle size

While the main @timkendrick/transducers package comes with a set of bundled operators, you can choose to import the core functionality on its own and load any desired operators and reducers individually:

import { compose, transduce } from '@timkendrick/transducers/core';

import array from '@timkendrick/transducers/reducers/array';

import map from '@timkendrick/transducers/operators/map';
import filter from '@timkendrick/transducers/operators/filter';
import take from '@timkendrick/transducers/operators/take';

This allows you to generate a super-minimal application bundle (the core module is <1KB minified and gzipped), and can also be useful for avoiding overlap with other transducer libraries.