-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathpredicate.ts
More file actions
38 lines (33 loc) · 1.5 KB
/
predicate.ts
File metadata and controls
38 lines (33 loc) · 1.5 KB
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
import { Property } from "./observable";
import _ from "./_";
import { mapT } from "./map";
import { Desc } from "./describe";
import Observable from "./observable";
import withLatestFrom from "./withlatestfrom";
import { composeT, Transformer } from "./transform";
import { Function1 } from "./types";
export type Predicate<V> = Function1<V, boolean>
/** @hidden */
export type PredicateOrBoolean<V> = Predicate<V> | boolean
export type PredicateOrProperty<V> = Predicate<V> | boolean | Property<boolean>
/** @hidden */
export function toPredicate<V>(f: PredicateOrBoolean<V>): Predicate<V> {
if (typeof f == "boolean") {
return _.always(f)
} else if (typeof f != "function") {
throw new Error("Not a function: " + f)
} else {
return f
}
}
type Predicate2Transformer<V> = (p: Predicate<V>) => Transformer<V, V>
type BoolTuple<T> = [T, boolean]
/** @hidden */
export function withPredicate<V>(src: Observable<V>, f: PredicateOrProperty<V>, predicateTransformer: Predicate2Transformer<V>, desc: Desc): Observable<V> {
if (f instanceof Property) {
return withLatestFrom(src, f, (p, v) => <BoolTuple<V>>[p, v])
.transform(composeT(<any>predicateTransformer(<any>((tuple: BoolTuple<V>) => tuple[1])), <any>mapT((tuple: BoolTuple<V>) => tuple[0])), desc)
// the `any` type above is needed because the type argument for Predicate2Transformer is fixed. We'd need higher-kinded types to be able to express this properly, I think.
}
return src.transform(predicateTransformer(toPredicate(f)), desc)
}