ecb6fe44bda7ab1c1846a4718f218bdd42025d93
[yaffs-website] / node_modules / grunt-legacy-util / node_modules / lodash / _baseUniq.js
1 var 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. */
9 var 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  */
20 function 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
71 module.exports = baseUniq;