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

transduce-x

v0.2.0

Published

Experimental JavaScript transducers extensions

Downloads

3

Readme

Transduce-X

Build Status

Experimental extensions to transduce.

Install and Usage

$ npm install transduce-x

If using async package, also install a Promise library of your choice. Works with any-promise library (a pollyfill, es6-promise, promise, native-promise-only, bluebird, rsvp, when, q). Just install the Promise library preference and it will be auto detected and used.

Browser

API

Supports the following functions:

async {
  defer: function()
  delay: function(wait)
  compose: function(/*args*/)
  into: function(init, t?, coll?)
  transduce: function(t, xf, init?, coll)
  reduce: function(xf, init?, coll)
  when: function(promiseOrValue, t)
  promiseTransform: function(t)
  emitInto: function(to, t, from)
  callback: function(t, init?, continuation?)
}
gen: function(stepGenerator, resultFn?)

Async

Use Transducers with async iterators and observables by supporting Promises in iterators and transformers. Inspired by this talk from Erik Meijer.

Mixed into transduce.async or available by explictly requiring from transduce/async. The following are equivalent:

  • require('transduce').async.defer
  • require('transduce/async').defer
async.defer()

Create a deferred transducer that allows wrapped transformer to step or result a Promise in addition to a value. All items will be queued and processed in order. The wrapped transformer is called with value of resolved Promise.

async.delay(wait)

Create a deferred transducer that delays step of wrapped transformer by wait milliseconds. All items will be queued and delayed and step will return a promise that will resolve after wait milliseconds for each item.

async.compose(/*fns*/)

Like a normal compose, except all arguments are interleaved with defer. This allows any transducer in composed pipeline to step or result a Promise in addition to a value. The wrapped transformer is called with value of resolved Promise.

async.transduce(t, xf, init?, coll)

Like a normal transduce, except init and coll can be a Promise and xf can be a deferred transducer. The value of coll can be anything that can be converted to an iterator. The return value is a Promise for the result of the transformation.

async.reduce(xf, init?, coll)

Like a normal reduce, except init and coll can be a Promise and xf can be a deferred transducer. The value of coll can be anything that can be converted to an iterator. The return value is a Promise for the result of the reduction.

async.into(init, t?, coll?)

Async version of into. Returns a Promise for a new collection appending all items into init by passing all items from source collection coll through the optional transducer t. Chooses transformer, xf from type of init. Can be array, object, string or have @@transformer. coll is converted to an iterator.

The function is automatically curried. If coll is not provided, returns a curried function using transformer from init and the same transformation can be used for multiple collections.

async.when(promiseOrValue, t)

Resolves promise or value as a promise, then transforms promise result with the transducer t as a single item.

async.promiseTransform(t)

Creates a callback useable for promise sucess that transforms the result with the transducer tas a single item.

async.emitInto(to, t, from)

Registers listener for events on from emitter and emits events on to emitter after transforming data events through the transducer, t. Assumes both to and from support the EventEmitter API from Node.js.

Supports following events:

  • emit('data', item): Listened and emitted for each item in transformation.
  • emit('error', error): Listened and emitted on error in transformation
  • emit('end'): Emitted when transformer completes, regardless of error or result.
async.callback(t, init?, continuation?)

Creates an async callback that starts a transducer process and accepts parameter cb(err, item) as a new item in the process. The returned callback and the optional continuation follow Node.js conventions with fn(err, item). Each item advances the state of the transducer, if the continuation is provided, it will be called on completion or error. An error will terminate the transducer and be propagated to the continuation.

If the transducer exhausts due to early termination, all subsequent calls to the callback will no-op and return the computed result. If the callback is called with no item, the transducer terminates, and all subsequent calls will no-op and return the computed result. The callback returns undefined until completion. Once completed, the final result is always returned.

Like into, chooses transformer, xf, based on the type of init using transformer. If init is not defined, maintains last value and does not buffer results. This can be used with tap or other methods to process items incrementally instead of waiting and buffering results.

Gen

Create Transducers using ES6 Generators. Generator is created with a step function and each item in transformation is yielded through the generator. Calling step function will step value to next transformer in the pipeline. An optional completing function can be provided that will be called on transformer result.

Works with [transduce][5], [transducers-js][3] or [transducers.js][4].

Requires support for ES6 generators. If using Node.js run with node --harmony and version 0.11 or better.

Mapping transducer loops infinitely and transforms each yieled item with mapping function.

var gen = require('transduce-gen');
function map(f){
  return gen(function*(step){
    while(true){
      step(f(yield 0));
    }
  });
}

var doubled = map(function(num){ return num * 2; });
tr.into([], doubled, [1,2,3]);
// [2,4,6]

Filter only calls step if it passes a predicate.

var gen = require('transduce-gen');
function filter(p){
  return gen(function*(step){
    while(true){
      var val = yield 0;
      if(p(val)){
        step(val);
      }
    }
  });
}
function isEven(x){
  return x%2 === 0;
}
var evenArray = [1, 2, 3, 4, 5, 6];
tr.into([], filter(isEven), evenArray);
// [2,4,6]

Early termination (reduced) can be signalled by completing generator function.

var gen = require('transduce-gen');
function take(n){
  return gen(function*(step){
    var i = 0;
    for(; i < n; i++){
      step(yield 0);
    }
  });
}

tr.into([], take(2), [1, 2, 3]))
// [1, 2]

Provide optional completing function to be called on result.

var gen = require('transduce-gen');
function push(){
  var args = slice.call(arguments);
  return gen(function*(step){
    while(true){
        step(yield 0);
    }
  },
  function(step){
    args.forEach(step);
  });
}
tr.into([], comp(push(4), push(5,6)), [1,2,3]))
// [1,2,3,4,5,6]

License

MIT