UNPKG

1.84 kBJavaScriptView Raw
1import SetCache from './_SetCache';
2import arrayIncludes from './_arrayIncludes';
3import arrayIncludesWith from './_arrayIncludesWith';
4import cacheHas from './_cacheHas';
5import createSet from './_createSet';
6import setToArray from './_setToArray';
7
8/** Used as the size to enable large array optimizations. */
9var LARGE_ARRAY_SIZE = 200;
10
11/**
12 * The base implementation of `_.uniqBy` without support for iteratee shorthands.
13 *
14 * @private
15 * @param {Array} array The array to inspect.
16 * @param {Function} [iteratee] The iteratee invoked per element.
17 * @param {Function} [comparator] The comparator invoked per element.
18 * @returns {Array} Returns the new duplicate free array.
19 */
20function baseUniq(array, iteratee, comparator) {
21 var index = -1,
22 includes = arrayIncludes,
23 length = array.length,
24 isCommon = true,
25 result = [],
26 seen = result;
27
28 if (comparator) {
29 isCommon = false;
30 includes = arrayIncludesWith;
31 }
32 else if (length >= LARGE_ARRAY_SIZE) {
33 var set = iteratee ? null : createSet(array);
34 if (set) {
35 return setToArray(set);
36 }
37 isCommon = false;
38 includes = cacheHas;
39 seen = new SetCache;
40 }
41 else {
42 seen = iteratee ? [] : result;
43 }
44 outer:
45 while (++index < length) {
46 var value = array[index],
47 computed = iteratee ? iteratee(value) : value;
48
49 if (isCommon && computed === computed) {
50 var seenIndex = seen.length;
51 while (seenIndex--) {
52 if (seen[seenIndex] === computed) {
53 continue outer;
54 }
55 }
56 if (iteratee) {
57 seen.push(computed);
58 }
59 result.push(value);
60 }
61 else if (!includes(seen, computed, comparator)) {
62 if (seen !== result) {
63 seen.push(computed);
64 }
65 result.push(value);
66 }
67 }
68 return result;
69}
70
71export default baseUniq;