mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 02:17:50 +00:00
Bump to v3.0.0.
This commit is contained in:
57
internal/baseUniq.js
Normal file
57
internal/baseUniq.js
Normal file
@@ -0,0 +1,57 @@
|
||||
import baseIndexOf from './baseIndexOf';
|
||||
import cacheIndexOf from './cacheIndexOf';
|
||||
import createCache from './createCache';
|
||||
|
||||
/**
|
||||
* The base implementation of `_.uniq` without support for callback shorthands
|
||||
* and `this` binding.
|
||||
*
|
||||
* @private
|
||||
* @param {Array} array The array to inspect.
|
||||
* @param {Function} [iteratee] The function invoked per iteration.
|
||||
* @returns {Array} Returns the new duplicate-value-free array.
|
||||
*/
|
||||
function baseUniq(array, iteratee) {
|
||||
var index = -1,
|
||||
indexOf = baseIndexOf,
|
||||
length = array.length,
|
||||
isCommon = true,
|
||||
isLarge = isCommon && length >= 200,
|
||||
seen = isLarge && createCache(),
|
||||
result = [];
|
||||
|
||||
if (seen) {
|
||||
indexOf = cacheIndexOf;
|
||||
isCommon = false;
|
||||
} else {
|
||||
isLarge = false;
|
||||
seen = iteratee ? [] : result;
|
||||
}
|
||||
outer:
|
||||
while (++index < length) {
|
||||
var value = array[index],
|
||||
computed = iteratee ? iteratee(value, index, array) : value;
|
||||
|
||||
if (isCommon && value === value) {
|
||||
var seenIndex = seen.length;
|
||||
while (seenIndex--) {
|
||||
if (seen[seenIndex] === computed) {
|
||||
continue outer;
|
||||
}
|
||||
}
|
||||
if (iteratee) {
|
||||
seen.push(computed);
|
||||
}
|
||||
result.push(value);
|
||||
}
|
||||
else if (indexOf(seen, computed) < 0) {
|
||||
if (iteratee || isLarge) {
|
||||
seen.push(computed);
|
||||
}
|
||||
result.push(value);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export default baseUniq;
|
||||
Reference in New Issue
Block a user