-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathskipDuplicates.ts
42 lines (38 loc) · 1.08 KB
/
skipDuplicates.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { Event, isValue, Observer, Scope } from "./abstractions"
import { applyScopeMaybe } from "./applyscope"
import {
transform,
Transformer,
UnaryTransformOp,
UnaryTransformOpScoped,
} from "./transform"
export type Predicate2<A> = (prev: A, next: A) => boolean
export function skipDuplicates<A>(fn: Predicate2<A>): UnaryTransformOp<A>
export function skipDuplicates<A>(
fn: Predicate2<A>,
scope: Scope
): UnaryTransformOpScoped<A>
export function skipDuplicates<A>(fn: Predicate2<A>, scope?: Scope): any {
return transform(
["skipDuplicates", [fn]],
skipDuplicatesT(fn),
scope as Scope
)
}
function skipDuplicatesT<A>(fn: Predicate2<A>): Transformer<A, A> {
let current: A | typeof uninitialized = uninitialized
return {
changes: (subscribe) => (onValue, onEnd) =>
subscribe((value) => {
if (current === uninitialized || !fn(current, value)) {
current = value
onValue(value)
}
}, onEnd),
init: (value: A) => {
current = value
return value
},
}
}
const uninitialized: unique symbol = Symbol()