UNPKG

1.33 kBJavaScriptView Raw
1import _includesWith from "./internal/_includesWith.js";
2import _curry3 from "./internal/_curry3.js";
3/**
4 * Finds the set (i.e. no duplicates) of all elements in the first list not
5 * contained in the second list. Duplication is determined according to the
6 * value returned by applying the supplied predicate to two list elements.
7 *
8 * @func
9 * @memberOf R
10 * @since v0.1.0
11 * @category Relation
12 * @sig ((a, a) -> Boolean) -> [a] -> [a] -> [a]
13 * @param {Function} pred A predicate used to test whether two items are equal.
14 * @param {Array} list1 The first list.
15 * @param {Array} list2 The second list.
16 * @return {Array} The elements in `list1` that are not in `list2`.
17 * @see R.difference, R.symmetricDifference, R.symmetricDifferenceWith
18 * @example
19 *
20 * const cmp = (x, y) => x.a === y.a;
21 * const l1 = [{a: 1}, {a: 2}, {a: 3}];
22 * const l2 = [{a: 3}, {a: 4}];
23 * R.differenceWith(cmp, l1, l2); //=> [{a: 1}, {a: 2}]
24 */
25
26var differenceWith =
27/*#__PURE__*/
28_curry3(function differenceWith(pred, first, second) {
29 var out = [];
30 var idx = 0;
31 var firstLen = first.length;
32
33 while (idx < firstLen) {
34 if (!_includesWith(pred, first[idx], second) && !_includesWith(pred, first[idx], out)) {
35 out.push(first[idx]);
36 }
37
38 idx += 1;
39 }
40
41 return out;
42});
43
44export default differenceWith;
\No newline at end of file