-
-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dropRepeats): implement core instance operator dropRepeats()
- Loading branch information
Andre Medeiros
committed
Apr 6, 2016
1 parent
76879a5
commit b7dccf9
Showing
4 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import {InternalListener} from '../InternalListener'; | ||
import {Operator} from '../Operator'; | ||
import {Stream} from '../Stream'; | ||
import {emptyListener} from '../utils/emptyListener'; | ||
import {empty} from '../utils/empty'; | ||
|
||
export class Proxy<T> implements InternalListener<T> { | ||
constructor(private out: Stream<T>, | ||
private op: DropRepeatsOperator<T>) { | ||
} | ||
|
||
isEq(x: T, y: T) { | ||
return this.op.compare ? this.op.compare(x, y) : x === y; | ||
} | ||
|
||
_n(t: T) { | ||
const op = this.op; | ||
if (op.v === empty || !this.isEq(t, op.v)) { | ||
this.out._n(t); | ||
} | ||
op.v = t; | ||
} | ||
|
||
_e(err: any) { | ||
this.out._e(err); | ||
} | ||
|
||
_c() { | ||
this.out._c(); | ||
} | ||
} | ||
|
||
export class DropRepeatsOperator<T> implements Operator<T, T> { | ||
private proxy: InternalListener<T> = emptyListener; | ||
public v: T = <any> empty; | ||
|
||
constructor(public compare: (x: T, y: T) => boolean, | ||
public ins: Stream<T>) { | ||
} | ||
|
||
_start(out: Stream<T>): void { | ||
this.ins._add(this.proxy = new Proxy(out, this)); | ||
} | ||
|
||
_stop(): void { | ||
this.ins._remove(this.proxy); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import xs from '../../src/index'; | ||
import * as assert from 'assert'; | ||
|
||
describe('Stream.prototype.dropRepeats', () => { | ||
it('should drop consecutive duplicate numbers (as events)', (done) => { | ||
const stream = xs.of(1, 2, 1, 1, 1, 2, 3, 4, 3, 3).dropRepeats(); | ||
const expected = [1, 2, 1, 2, 3, 4, 3]; | ||
|
||
stream.addListener({ | ||
next: (x: number) => { | ||
assert.equal(x, expected.shift()); | ||
}, | ||
error: (err: any) => done(err), | ||
complete: () => { | ||
assert.equal(expected.length, 0); | ||
done(); | ||
}, | ||
}); | ||
}); | ||
|
||
it('should drop consecutive \'duplicate\' strings, with a custom isEqual', (done) => { | ||
const stream = xs.of('a', 'b', 'a', 'A', 'B', 'b') | ||
.dropRepeats((x, y) => x.toLowerCase() === y.toLowerCase()); | ||
const expected = ['a', 'b', 'a', 'B']; | ||
|
||
stream.addListener({ | ||
next: (x: string) => { | ||
assert.equal(x, expected.shift()); | ||
}, | ||
error: (err: any) => done(err), | ||
complete: () => { | ||
assert.equal(expected.length, 0); | ||
done(); | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters