Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var baseIndexOf = require('./baseIndexOf'),
2
cacheIndexOf = require('./cacheIndexOf'),
3
createCache = require('./createCache');
4
5
/**
6
* The base implementation of `_.difference` which accepts a single array
7
* of values to exclude.
8
*
9
* @private
10
* @param {Array} array The array to inspect.
11
* @param {Array} values The values to exclude.
12
* @returns {Array} Returns the new array of filtered values.
13
*/
14
function baseDifference(array, values) {
15
var length = array ? array.length : 0,
16
result = [];
17
18
if (!length) {
19
return result;
20
}
21
var index = -1,
22
indexOf = baseIndexOf,
23
isCommon = true,
24
cache = (isCommon && values.length >= 200) ? createCache(values) : null,
25
valuesLength = values.length;
26
27
if (cache) {
28
indexOf = cacheIndexOf;
29
isCommon = false;
30
values = cache;
31
}
32
outer:
33
while (++index < length) {
34
var value = array[index];
35
36
if (isCommon && value === value) {
37
var valuesIndex = valuesLength;
38
while (valuesIndex--) {
39
if (values[valuesIndex] === value) {
40
continue outer;
41
}
42
}
43
result.push(value);
44
}
45
else if (indexOf(values, value, 0) < 0) {
46
result.push(value);
47
}
48
}
49
return result;
50
}
51
52
module.exports = baseDifference;
53
54