58 lines
1.4 KiB
JavaScript
58 lines
1.4 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.default = Filter;
|
||
|
|
||
|
var _Pipe = require('../sink/Pipe');
|
||
|
|
||
|
var _Pipe2 = _interopRequireDefault(_Pipe);
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
function Filter(p, source) {
|
||
|
this.p = p;
|
||
|
this.source = source;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Create a filtered source, fusing adjacent filter.filter if possible
|
||
|
* @param {function(x:*):boolean} p filtering predicate
|
||
|
* @param {{run:function}} source source to filter
|
||
|
* @returns {Filter} filtered source
|
||
|
*/
|
||
|
/** @license MIT License (c) copyright 2010-2016 original author or authors */
|
||
|
/** @author Brian Cavalier */
|
||
|
/** @author John Hann */
|
||
|
|
||
|
Filter.create = function createFilter(p, source) {
|
||
|
if (source instanceof Filter) {
|
||
|
return new Filter(and(source.p, p), source.source);
|
||
|
}
|
||
|
|
||
|
return new Filter(p, source);
|
||
|
};
|
||
|
|
||
|
Filter.prototype.run = function (sink, scheduler) {
|
||
|
return this.source.run(new FilterSink(this.p, sink), scheduler);
|
||
|
};
|
||
|
|
||
|
function FilterSink(p, sink) {
|
||
|
this.p = p;
|
||
|
this.sink = sink;
|
||
|
}
|
||
|
|
||
|
FilterSink.prototype.end = _Pipe2.default.prototype.end;
|
||
|
FilterSink.prototype.error = _Pipe2.default.prototype.error;
|
||
|
|
||
|
FilterSink.prototype.event = function (t, x) {
|
||
|
var p = this.p;
|
||
|
p(x) && this.sink.event(t, x);
|
||
|
};
|
||
|
|
||
|
function and(p, q) {
|
||
|
return function (x) {
|
||
|
return p(x) && q(x);
|
||
|
};
|
||
|
}
|