UNPKG

1.27 kBJavaScriptView Raw
1import _curry2 from "./internal/_curry2.js";
2import _Set from "./internal/_Set.js";
3/**
4 * Finds the set (i.e. no duplicates) of all elements in the first list not
5 * contained in the second list. Objects and Arrays are compared in terms of
6 * value equality, not reference equality.
7 *
8 * @func
9 * @memberOf R
10 * @since v0.1.0
11 * @category Relation
12 * @sig [*] -> [*] -> [*]
13 * @param {Array} list1 The first list.
14 * @param {Array} list2 The second list.
15 * @return {Array} The elements in `list1` that are not in `list2`.
16 * @see R.differenceWith, R.symmetricDifference, R.symmetricDifferenceWith, R.without
17 * @example
18 *
19 * R.difference([1,2,3,4], [7,6,5,4,3]); //=> [1,2]
20 * R.difference([7,6,5,4,3], [1,2,3,4]); //=> [7,6,5]
21 * R.difference([{a: 1}, {b: 2}], [{a: 1}, {c: 3}]) //=> [{b: 2}]
22 */
23
24var difference =
25/*#__PURE__*/
26_curry2(function difference(first, second) {
27 var out = [];
28 var idx = 0;
29 var firstLen = first.length;
30 var secondLen = second.length;
31 var toFilterOut = new _Set();
32
33 for (var i = 0; i < secondLen; i += 1) {
34 toFilterOut.add(second[i]);
35 }
36
37 while (idx < firstLen) {
38 if (toFilterOut.add(first[idx])) {
39 out[out.length] = first[idx];
40 }
41
42 idx += 1;
43 }
44
45 return out;
46});
47
48export default difference;
\No newline at end of file