@zkochan/pnpm
Version:
Fast, disk space efficient package manager
50 lines (40 loc) • 1.28 kB
JavaScript
/** @license MIT License (c) copyright 2010-2016 original author or authors */
/** @author Brian Cavalier */
/** @author John Hann */
import Pipe from '../sink/Pipe'
import Filter from './Filter'
import FilterMap from './FilterMap'
import * as base from '@most/prelude'
export default function Map (f, source) {
this.f = f
this.source = source
}
/**
* Create a mapped source, fusing adjacent map.map, filter.map,
* and filter.map.map if possible
* @param {function(*):*} f mapping function
* @param {{run:function}} source source to map
* @returns {Map|FilterMap} mapped source, possibly fused
*/
Map.create = function createMap (f, source) {
if (source instanceof Map) {
return new Map(base.compose(f, source.f), source.source)
}
if (source instanceof Filter) {
return new FilterMap(source.p, f, source.source)
}
return new Map(f, source)
}
Map.prototype.run = function (sink, scheduler) { // eslint-disable-line no-extend-native
return this.source.run(new MapSink(this.f, sink), scheduler)
}
function MapSink (f, sink) {
this.f = f
this.sink = sink
}
MapSink.prototype.end = Pipe.prototype.end
MapSink.prototype.error = Pipe.prototype.error
MapSink.prototype.event = function (t, x) {
var f = this.f
this.sink.event(t, f(x))
}