-
Notifications
You must be signed in to change notification settings - Fork 133
/
entity-filters.ts
29 lines (26 loc) · 1.03 KB
/
entity-filters.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
/**
* Filters the `entities` array argument and returns the original `entities`,
* or a new filtered array of entities.
* NEVER mutate the original `entities` array itself.
**/
export type EntityFilterFn<T> = (entities: T[], pattern?: any) => T[];
/**
* Creates an {EntityFilterFn} that matches RegExp or RegExp string pattern
* anywhere in any of the given props of an entity.
* If pattern is a string, spaces are significant and ignores case.
*/
export function PropsFilterFnFactory<T = any>(props: (keyof T)[] = []): EntityFilterFn<T> {
if (props.length === 0) {
// No properties -> nothing could match -> return unfiltered
return (entities: T[], pattern: string) => entities;
}
return (entities: T[], pattern: string | RegExp) => {
if (!entities) { return []; }
const regExp = typeof pattern === 'string' ? new RegExp(pattern, 'i') : pattern;
if (regExp) {
const predicate = (e: any) => props.some(prop => regExp.test(e[prop]));
return entities.filter(predicate);
}
return entities;
};
}