/
filter-fuzzy.ts
60 lines (58 loc) · 1.66 KB
/
filter-fuzzy.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import type { Fn, Predicate2 } from "@thi.ng/api";
import { fuzzyMatch } from "@thi.ng/arrays";
import type { Transducer } from "../api";
import { $iter } from "../iterator";
import { filter } from "./filter";
export interface FilterFuzzyOpts<A, B> {
/**
* Key extractor function.
*/
key: Fn<A, ArrayLike<B>>;
/**
* Equivalence predicate function.
*/
equiv: Predicate2<any>;
}
/**
* Returns transducer which calls {@link @thi.ng/arrays#fuzzyMatch} for
* each value and discards all non-matching values.
*
* @remarks
* The `key` option function can be used to extract/produce the actual
* value used for the search. The `equiv` option predicate can be used
* to customize item equality checking. Uses {@link @thi.ng/equiv#equiv}
* by default.
*
* @example
* ```ts
* [...filterFuzzy("ho", ["hello", "hallo", "hey", "heyoka"])]
* // ["hello", "hallo", "heyoka"]
* ```
*
* @param opts -
* @param src -
*/
export function filterFuzzy<A, B>(
query: ArrayLike<B>,
opts?: Partial<FilterFuzzyOpts<A, B>>
): Transducer<A, A>;
export function filterFuzzy<A, B>(
query: ArrayLike<B>,
src: Iterable<A>
): IterableIterator<A>;
export function filterFuzzy<A, B>(
query: ArrayLike<B>,
opts: Partial<FilterFuzzyOpts<A, B>>,
src: Iterable<A>
): IterableIterator<A>;
export function filterFuzzy<A, B>(...args: any[]): any {
const iter = args.length > 1 && $iter(filterFuzzy, args);
if (iter) {
return iter;
}
const query: ArrayLike<B> = args[0];
const { key, equiv } = <FilterFuzzyOpts<A, B>>(args[1] || {});
return filter((x: A) =>
fuzzyMatch(key != null ? key(x) : <any>x, query, equiv)
);
}