xstream
Version:
An extremely intuitive, small, and fast functional reactive stream library for JavaScript
122 lines • 2.92 kB
JavaScript
"use strict";
var core_1 = require('../core');
var empty = {};
var DropRepeatsOperator = (function () {
function DropRepeatsOperator(fn, ins) {
this.fn = fn;
this.ins = ins;
this.type = 'dropRepeats';
this.out = null;
this.v = empty;
}
DropRepeatsOperator.prototype._start = function (out) {
this.out = out;
this.ins._add(this);
};
DropRepeatsOperator.prototype._stop = function () {
this.ins._remove(this);
this.out = null;
this.v = empty;
};
DropRepeatsOperator.prototype.isEq = function (x, y) {
return this.fn ? this.fn(x, y) : x === y;
};
DropRepeatsOperator.prototype._n = function (t) {
var u = this.out;
if (!u)
return;
var v = this.v;
if (v !== empty && this.isEq(t, v))
return;
this.v = t;
u._n(t);
};
DropRepeatsOperator.prototype._e = function (err) {
var u = this.out;
if (!u)
return;
u._e(err);
};
DropRepeatsOperator.prototype._c = function () {
var u = this.out;
if (!u)
return;
u._c();
};
return DropRepeatsOperator;
}());
exports.DropRepeatsOperator = DropRepeatsOperator;
/**
* Drops consecutive duplicate values in a stream.
*
* Marble diagram:
*
* ```text
* --1--2--1--1--1--2--3--4--3--3|
* dropRepeats
* --1--2--1--------2--3--4--3---|
* ```
*
* Example:
*
* ```js
* import dropRepeats from 'xstream/extra/dropRepeats'
*
* const stream = xs.of(1, 2, 1, 1, 1, 2, 3, 4, 3, 3)
* .compose(dropRepeats())
*
* stream.addListener({
* next: i => console.log(i),
* error: err => console.error(err),
* complete: () => console.log('completed')
* })
* ```
*
* ```text
* > 1
* > 2
* > 1
* > 2
* > 3
* > 4
* > 3
* > completed
* ```
*
* Example with a custom isEqual function:
*
* ```js
* import dropRepeats from 'xstream/extra/dropRepeats'
*
* const stream = xs.of('a', 'b', 'a', 'A', 'B', 'b')
* .compose(dropRepeats((x, y) => x.toLowerCase() === y.toLowerCase()))
*
* stream.addListener({
* next: i => console.log(i),
* error: err => console.error(err),
* complete: () => console.log('completed')
* })
* ```
*
* ```text
* > a
* > b
* > a
* > B
* > completed
* ```
*
* @param {Function} isEqual An optional function of type
* `(x: T, y: T) => boolean` that takes an event from the input stream and
* checks if it is equal to previous event, by returning a boolean.
* @return {Stream}
*/
function dropRepeats(isEqual) {
if (isEqual === void 0) { isEqual = null; }
return function dropRepeatsOperator(ins) {
return new core_1.Stream(new DropRepeatsOperator(isEqual, ins));
};
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = dropRepeats;
//# sourceMappingURL=dropRepeats.js.map