UNPKG

1.91 kBJavaScriptView Raw
1var SetCache = require('./_SetCache'),
2 arrayIncludes = require('./_arrayIncludes'),
3 arrayIncludesWith = require('./_arrayIncludesWith'),
4 cacheHas = require('./_cacheHas'),
5 createSet = require('./_createSet'),
6 setToArray = require('./_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 value = (comparator || value !== 0) ? value : 0;
50 if (isCommon && computed === computed) {
51 var seenIndex = seen.length;
52 while (seenIndex--) {
53 if (seen[seenIndex] === computed) {
54 continue outer;
55 }
56 }
57 if (iteratee) {
58 seen.push(computed);
59 }
60 result.push(value);
61 }
62 else if (!includes(seen, computed, comparator)) {
63 if (seen !== result) {
64 seen.push(computed);
65 }
66 result.push(value);
67 }
68 }
69 return result;
70}
71
72module.exports = baseUniq;