Created
June 1, 2015 10:42
-
-
Save send2moran/8499b93698b4a46df2f6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* The base implementation of `_.flatten` with added support for restricting | |
* flattening and specifying the start index. | |
* | |
* @private | |
* @param {Array} array The array to flatten. | |
* @param {boolean} [isDeep] Specify a deep flatten. | |
* @param {boolean} [isStrict] Restrict flattening to arrays-like objects. | |
* @returns {Array} Returns the new flattened array. | |
*/ | |
function baseFlatten(array, isDeep, isStrict) { | |
var index = -1, | |
length = array.length, | |
resIndex = -1, | |
result = []; | |
while (++index < length) { | |
var value = array[index]; | |
if (isObjectLike(value) && isArrayLike(value) && | |
(isStrict || isArray(value) || isArguments(value))) { | |
if (isDeep) { | |
// Recursively flatten arrays (susceptible to call stack limits). | |
value = baseFlatten(value, isDeep, isStrict); | |
} | |
var valIndex = -1, | |
valLength = value.length; | |
while (++valIndex < valLength) { | |
result[++resIndex] = value[valIndex]; | |
} | |
} else if (!isStrict) { | |
result[++resIndex] = value; | |
} | |
} | |
return result; | |
} | |
/** | |
* Creates an array of unique values, in order, from all of the provided arrays | |
* using [`SameValueZero`](https://people.mozilla.org/~jorendorff/es6-draft.html#sec-samevaluezero) | |
* for equality comparisons. | |
* | |
* @static | |
* @memberOf _ | |
* @category Array | |
* @param {...Array} [arrays] The arrays to inspect. | |
* @returns {Array} Returns the new array of combined values. | |
* @example | |
* | |
* _.union([1, 2], [4, 2], [2, 1]); | |
* // => [1, 2, 4] | |
*/ | |
var union = restParam(function(arrays) { | |
return baseUniq(baseFlatten(arrays, false, true)); | |
}); | |
/** | |
* 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 = getIndexOf(), | |
length = array.length, | |
isCommon = indexOf == baseIndexOf, | |
isLarge = isCommon && length >= 200, | |
seen = isLarge ? createCache() : null, | |
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) < 0) { | |
if (iteratee || isLarge) { | |
seen.push(computed); | |
} | |
result.push(value); | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment