89 lines
2.4 KiB
JavaScript
89 lines
2.4 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.filter = filter;
|
||
|
exports.skipRepeats = skipRepeats;
|
||
|
exports.skipRepeatsWith = skipRepeatsWith;
|
||
|
|
||
|
var _Stream = require('../Stream');
|
||
|
|
||
|
var _Stream2 = _interopRequireDefault(_Stream);
|
||
|
|
||
|
var _Pipe = require('../sink/Pipe');
|
||
|
|
||
|
var _Pipe2 = _interopRequireDefault(_Pipe);
|
||
|
|
||
|
var _Filter = require('../fusion/Filter');
|
||
|
|
||
|
var _Filter2 = _interopRequireDefault(_Filter);
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
/**
|
||
|
* Retain only items matching a predicate
|
||
|
* @param {function(x:*):boolean} p filtering predicate called for each item
|
||
|
* @param {Stream} stream stream to filter
|
||
|
* @returns {Stream} stream containing only items for which predicate returns truthy
|
||
|
*/
|
||
|
function filter(p, stream) {
|
||
|
return new _Stream2.default(_Filter2.default.create(p, stream.source));
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Skip repeated events, using === to detect duplicates
|
||
|
* @param {Stream} stream stream from which to omit repeated events
|
||
|
* @returns {Stream} stream without repeated events
|
||
|
*/
|
||
|
/** @license MIT License (c) copyright 2010-2016 original author or authors */
|
||
|
/** @author Brian Cavalier */
|
||
|
/** @author John Hann */
|
||
|
|
||
|
function skipRepeats(stream) {
|
||
|
return skipRepeatsWith(same, stream);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Skip repeated events using the provided equals function to detect duplicates
|
||
|
* @param {function(a:*, b:*):boolean} equals optional function to compare items
|
||
|
* @param {Stream} stream stream from which to omit repeated events
|
||
|
* @returns {Stream} stream without repeated events
|
||
|
*/
|
||
|
function skipRepeatsWith(equals, stream) {
|
||
|
return new _Stream2.default(new SkipRepeats(equals, stream.source));
|
||
|
}
|
||
|
|
||
|
function SkipRepeats(equals, source) {
|
||
|
this.equals = equals;
|
||
|
this.source = source;
|
||
|
}
|
||
|
|
||
|
SkipRepeats.prototype.run = function (sink, scheduler) {
|
||
|
return this.source.run(new SkipRepeatsSink(this.equals, sink), scheduler);
|
||
|
};
|
||
|
|
||
|
function SkipRepeatsSink(equals, sink) {
|
||
|
this.equals = equals;
|
||
|
this.sink = sink;
|
||
|
this.value = void 0;
|
||
|
this.init = true;
|
||
|
}
|
||
|
|
||
|
SkipRepeatsSink.prototype.end = _Pipe2.default.prototype.end;
|
||
|
SkipRepeatsSink.prototype.error = _Pipe2.default.prototype.error;
|
||
|
|
||
|
SkipRepeatsSink.prototype.event = function (t, x) {
|
||
|
if (this.init) {
|
||
|
this.init = false;
|
||
|
this.value = x;
|
||
|
this.sink.event(t, x);
|
||
|
} else if (!this.equals(this.value, x)) {
|
||
|
this.value = x;
|
||
|
this.sink.event(t, x);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
function same(a, b) {
|
||
|
return a === b;
|
||
|
}
|