UNPKG

funstream

Version:

Funstream gives you iteratorish methods on your streams.

81 lines (76 loc) 1.97 kB
'use strict' let FunStream const FunTransform = require('./fun-transform.js') module.exports = MapStream const MAPS = Symbol('maps') function MapStream (mapWith, opts) { if (!FunStream) FunStream = require('./fun-stream.js') if (FunStream.isAsync(mapWith, 1, opts)) { return new MapStreamAsync(mapWith, opts) } else { return new MapStreamSync(mapWith, opts) } } MapStream.MAPS = MAPS class MapStreamAsync extends FunTransform { constructor (mapWith, opts) { super(opts) this[MAPS] = [mapWith] } _transform (data, encoding, next) { this._runMaps(data, 0, next) } _runMaps (data, nextMap, next) { try { if (nextMap >= this[MAPS].length) { this.push(data) return next() } const handleResult = (err, value) => { if (err) { return next(err) } else { this._runMaps(value, nextMap + 1, next) } } const result = this[MAPS][nextMap](data, handleResult) if (result && result.then) return result.then(keep => handleResult(null, keep), handleResult) } catch (err) { next(err) } } map (mapWith, opts) { if (!FunStream) FunStream = require('./fun-stream.js') if (FunStream.isAsync(mapWith, 1, opts)) { this[MAPS].push(mapWith) return this } else { return super.map(mapWith, opts) } } } MapStream.Async = MapStreamAsync class MapStreamSync extends FunTransform { constructor (mapWith, opts) { super(opts) this[MAPS] = [mapWith] } _transform (data, encoding, next) { try { this.push(this[MAPS].reduce((data, fn) => fn(data), data)) next() } catch (err) { next(err) } } map (mapWith, opts) { if (!FunStream) FunStream = require('./fun-stream.js') if (FunStream.isAsync(mapWith, 1, opts)) { return super.map(mapWith, opts) } else { this[MAPS].push(mapWith) return this } } } MapStream.Sync = MapStreamSync