flat-map

1.0.0 • Public • Published

flat-map

Build Status

A flat map implementation for node streams

Installation

npm install flat-map

Usage

When the callback data is already flat passes it maps it normally:

  var es = require('event-stream');
  var flatMap = require('flat-map');
  es.readArray([1, 2, 3, 4, 5])
    .pipe(flatMap(function(data, callback) { callback(null, data.split(/\s/); }));
  // [1, 2, 3, 4, 5]    

When the callback data is an array it flattens and maps it accordingly:

  var es = require('event-stream');
  var flatMap = require('flat-map');
  es.readArray('one two', 'three', 'four five')
    .pipe(flatMap(function(data, callback) { callback(null, data.split(/\s/); });
  // ['one', 'two', 'three', 'four', 'five']    

When the callback data is a stream it flattens and maps it accordingly:

  var es = require('event-stream');
  var flatMap = require('flat-map');
  es.readArray('one two', 'three', 'four five')
    .pipe(flatMap(function(data, callback) { callback(null, es.readArray(data.split(/\s+/))) });
  // ['one', 'two', 'three', 'four', 'five']    

When the callback data is a promise it flattens and maps it accordingly:

  var es = require('event-stream');
  var flatMap = require('flat-map');
  es.readArray('one two', 'three', 'four five')
      .pipe(flatMap(function(data, callback) {
        callback(null, new Promise(function(resolve) { resolve(data); }))
      }));
  // ['one', 'two', 'three', 'four', 'five']

Package Sidebar

Install

npm i flat-map

Weekly Downloads

1,394

Version

1.0.0

License

MIT

Last publish

Collaborators

  • rdy