-
Notifications
You must be signed in to change notification settings - Fork 83
/
pull.ts
31 lines (30 loc) · 1.05 KB
/
pull.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
import { UpdateOptions } from "../../core";
import { Query } from "../../query";
import { AnyVal, ArrayOrObject, RawArray, RawObject } from "../../types";
import { Action, applyUpdate, walkExpression } from "./_internal";
/** Removes from an existing array all instances of a value or values that match a specified condition. */
export const $pull = (
obj: RawObject,
expr: RawObject,
arrayFilters: RawObject[] = [],
options: UpdateOptions = {}
) => {
return walkExpression(expr, arrayFilters, options, ((val, node, queries) => {
const query = new Query({ k: val }, options.queryOptions);
const pred = (v: AnyVal) => query.test({ k: v });
return applyUpdate(obj, node, queries, (o: ArrayOrObject, k: string) => {
const prev = o[k] as RawArray;
const curr = new Array<AnyVal>();
const found = prev
.map(v => {
const b = pred(v);
if (!b) curr.push(v);
return b;
})
.some(Boolean);
if (!found) return false;
o[k] = curr;
return true;
});
}) as Action);
};