-
Notifications
You must be signed in to change notification settings - Fork 7
/
whereEq.ts
39 lines (33 loc) · 1.17 KB
/
whereEq.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
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import curryN from './utils/curry_n.ts';
import type { Func, Obj, PH, Tests } from './utils/types.ts';
import { whereAll } from './whereAll.ts';
import { map } from './map.ts';
import { equals } from './equals.ts';
// @types
type WhereEq_2<T> = (testObj: Obj<T>) => boolean;
type WhereEq_1<T> = (spec: Obj<T>) => boolean;
type WhereEq =
& (<T>(spec: Obj<T>, testObj: Obj<T>) => boolean)
& (<T>(spec: Obj<T>) => WhereEq_2<T>)
& (<T>(spec: PH, testObj: Obj<T>) => WhereEq_1<T>)
& (<T>(spec: Obj<T>, testObj: Obj<T>) => boolean);
function _whereEq<T>(spec: Obj<T>, testObj: Obj<T>) {
return whereAll(
(map(equals as Func, spec) as unknown) as Tests<T>,
testObj,
);
}
/**
* Takes a spec object and a test object, returns true if the test satisfies
* the spec, false otherwise.
* `whereEq` is a specialization of [`where`].
*
* const pred = Fae.whereEq({a: 1, b: 2})
*
* pred({a: 1}) //=> false
* pred({a: 1, b: 2}) //=> true
* pred({a: 1, b: 2, c: 3}) //=> true
* pred({a: 1, b: 1}) //=> false
*/
export const whereEq: WhereEq = curryN(2, _whereEq);