mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
Clarify _.uniq doc example and rebuild files. [closes #282]
Former-commit-id: b3ab9ae81af219dfb75b3f4339555530a6301f6e
This commit is contained in:
305
dist/lodash.compat.js
vendored
305
dist/lodash.compat.js
vendored
@@ -642,80 +642,25 @@
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
/**
|
||||
* Creates a function optimized to search large arrays for a given `value`,
|
||||
* starting at `fromIndex`, using strict equality for comparisons, i.e. `===`.
|
||||
* A basic version of `_.indexOf` without support for binary searches
|
||||
* or `fromIndex` constraints.
|
||||
*
|
||||
* @private
|
||||
* @param {Array} array The array to search.
|
||||
* @param {Mixed} value The value to search for.
|
||||
* @returns {Boolean} Returns `true`, if `value` is found, else `false`.
|
||||
* @param {Number} [fromIndex=0] The index to search from.
|
||||
* @returns {Number} Returns the index of the matched value or `-1`.
|
||||
*/
|
||||
function createCache(array) {
|
||||
var bailout,
|
||||
index = -1,
|
||||
length = array.length,
|
||||
isLarge = length >= largeArraySize,
|
||||
objCache = {};
|
||||
function basicIndexOf(array, value, fromIndex) {
|
||||
var index = (fromIndex || 0) - 1,
|
||||
length = array.length;
|
||||
|
||||
var caches = {
|
||||
'false': false,
|
||||
'function': false,
|
||||
'null': false,
|
||||
'number': {},
|
||||
'object': objCache,
|
||||
'string': {},
|
||||
'true': false,
|
||||
'undefined': false
|
||||
};
|
||||
|
||||
function cacheContains(value) {
|
||||
var type = typeof value;
|
||||
if (type == 'boolean' || value == null) {
|
||||
return caches[value];
|
||||
}
|
||||
var cache = caches[type] || (type = 'object', objCache),
|
||||
key = type == 'number' ? value : keyPrefix + value;
|
||||
|
||||
return type == 'object'
|
||||
? (cache[key] ? indexOf(cache[key], value) > -1 : false)
|
||||
: !!cache[key];
|
||||
}
|
||||
|
||||
function cachePush(value) {
|
||||
var type = typeof value;
|
||||
if (type == 'boolean' || value == null) {
|
||||
caches[value] = true;
|
||||
} else {
|
||||
var cache = caches[type] || (type = 'object', objCache),
|
||||
key = type == 'number' ? value : keyPrefix + value;
|
||||
|
||||
if (type == 'object') {
|
||||
bailout = (cache[key] || (cache[key] = [])).push(value) == length;
|
||||
} else {
|
||||
cache[key] = true;
|
||||
}
|
||||
while (++index < length) {
|
||||
if (array[index] === value) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
||||
function simpleContains(value) {
|
||||
return indexOf(array, value) > -1;
|
||||
}
|
||||
|
||||
function simplePush(value) {
|
||||
array.push(value);
|
||||
}
|
||||
|
||||
if (isLarge) {
|
||||
while (++index < length) {
|
||||
cachePush(array[index]);
|
||||
}
|
||||
if (bailout) {
|
||||
isLarge = caches = objCache = null;
|
||||
}
|
||||
}
|
||||
return isLarge
|
||||
? { 'contains': cacheContains, 'push': cachePush }
|
||||
: { 'push': simplePush, 'contains' : simpleContains };
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -817,6 +762,85 @@
|
||||
return bound;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a function optimized to search large arrays for a given `value`,
|
||||
* starting at `fromIndex`, using strict equality for comparisons, i.e. `===`.
|
||||
*
|
||||
* @private
|
||||
* @param {Array} [array=[]] The array to search.
|
||||
* @param {Mixed} value The value to search for.
|
||||
* @returns {Boolean} Returns `true`, if `value` is found, else `false`.
|
||||
*/
|
||||
function createCache(array) {
|
||||
array || (array = []);
|
||||
|
||||
var bailout,
|
||||
index = -1,
|
||||
length = array.length,
|
||||
isLarge = length >= largeArraySize,
|
||||
objCache = {};
|
||||
|
||||
var caches = {
|
||||
'false': false,
|
||||
'function': false,
|
||||
'null': false,
|
||||
'number': {},
|
||||
'object': objCache,
|
||||
'string': {},
|
||||
'true': false,
|
||||
'undefined': false
|
||||
};
|
||||
|
||||
function basicContains(value) {
|
||||
return basicIndexOf(array, value) > -1;
|
||||
}
|
||||
|
||||
function basicPush(value) {
|
||||
array.push(value);
|
||||
}
|
||||
|
||||
function cacheContains(value) {
|
||||
var type = typeof value;
|
||||
if (type == 'boolean' || value == null) {
|
||||
return caches[value];
|
||||
}
|
||||
var cache = caches[type] || (type = 'object', objCache),
|
||||
key = type == 'number' ? value : keyPrefix + value;
|
||||
|
||||
return type == 'object'
|
||||
? (cache[key] ? basicIndexOf(cache[key], value) > -1 : false)
|
||||
: !!cache[key];
|
||||
}
|
||||
|
||||
function cachePush(value) {
|
||||
var type = typeof value;
|
||||
if (type == 'boolean' || value == null) {
|
||||
caches[value] = true;
|
||||
} else {
|
||||
var cache = caches[type] || (type = 'object', objCache),
|
||||
key = type == 'number' ? value : keyPrefix + value;
|
||||
|
||||
if (type == 'object') {
|
||||
bailout = (cache[key] || (cache[key] = [])).push(value) == length;
|
||||
} else {
|
||||
cache[key] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isLarge) {
|
||||
while (++index < length) {
|
||||
cachePush(array[index]);
|
||||
}
|
||||
if (bailout) {
|
||||
isLarge = caches = objCache = null;
|
||||
}
|
||||
}
|
||||
return isLarge
|
||||
? { 'contains': cacheContains, 'push': cachePush }
|
||||
: { 'contains': basicContains, 'push': basicPush };
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates compiled iteration functions.
|
||||
*
|
||||
@@ -891,6 +915,17 @@
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Used by `escape` to convert characters to HTML entities.
|
||||
*
|
||||
* @private
|
||||
* @param {String} match The matched character to escape.
|
||||
* @returns {String} Returns the escaped character.
|
||||
*/
|
||||
function escapeHtmlChar(match) {
|
||||
return htmlEscapes[match];
|
||||
}
|
||||
|
||||
/**
|
||||
* Used by `template` to escape characters for inclusion in compiled
|
||||
* string literals.
|
||||
@@ -903,17 +938,6 @@
|
||||
return '\\' + stringEscapes[match];
|
||||
}
|
||||
|
||||
/**
|
||||
* Used by `escape` to convert characters to HTML entities.
|
||||
*
|
||||
* @private
|
||||
* @param {String} match The matched character to escape.
|
||||
* @returns {String} Returns the escaped character.
|
||||
*/
|
||||
function escapeHtmlChar(match) {
|
||||
return htmlEscapes[match];
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if `value` is a DOM node in IE < 9.
|
||||
*
|
||||
@@ -949,6 +973,29 @@
|
||||
// no operation performed
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a function that juggles arguments, allowing argument overloading
|
||||
* for `_.flatten` and `_.uniq`, before passing them to the given `func`.
|
||||
*
|
||||
* @private
|
||||
* @param {Function} func The function to wrap.
|
||||
* @returns {Function} Returns the new function.
|
||||
*/
|
||||
function overloadWrapper(func) {
|
||||
return function(array, flag, callback, thisArg) {
|
||||
// juggle arguments
|
||||
if (typeof flag != 'boolean' && flag != null) {
|
||||
thisArg = callback;
|
||||
callback = !(thisArg && thisArg[flag] === array) ? flag : undefined;
|
||||
flag = false;
|
||||
}
|
||||
if (callback != null) {
|
||||
callback = lodash.createCallback(callback, thisArg);
|
||||
}
|
||||
return func(array, flag, callback, thisArg);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* A fallback implementation of `isPlainObject` which checks if a given `value`
|
||||
* is an object created by the `Object` constructor, assuming objects created
|
||||
@@ -1131,7 +1178,7 @@
|
||||
* @param {Mixed} [thisArg] The `this` binding of `callback`.
|
||||
* @returns {Array|Object|String} Returns `collection`.
|
||||
*/
|
||||
var each = createIterator(eachIteratorOptions);
|
||||
var basicEach = createIterator(eachIteratorOptions);
|
||||
|
||||
/**
|
||||
* Used to convert characters to HTML entities:
|
||||
@@ -1316,7 +1363,7 @@
|
||||
stackB.push(result);
|
||||
|
||||
// recursively populate clone (susceptible to call stack limits)
|
||||
(isArr ? each : forOwn)(value, function(objValue, key) {
|
||||
(isArr ? basicEach : forOwn)(value, function(objValue, key) {
|
||||
result[key] = clone(objValue, deep, callback, undefined, stackA, stackB);
|
||||
});
|
||||
|
||||
@@ -2246,7 +2293,7 @@
|
||||
forIn(object, function(value, key, object) {
|
||||
if (isFunc
|
||||
? !callback(value, key, object)
|
||||
: indexOf(props, key) < 0
|
||||
: basicIndexOf(props, key) < 0
|
||||
) {
|
||||
result[key] = value;
|
||||
}
|
||||
@@ -2374,7 +2421,7 @@
|
||||
accumulator = createObject(proto);
|
||||
}
|
||||
}
|
||||
(isArr ? each : forOwn)(object, function(value, index, object) {
|
||||
(isArr ? basicEach : forOwn)(object, function(value, index, object) {
|
||||
return callback(accumulator, value, index, object);
|
||||
});
|
||||
return accumulator;
|
||||
@@ -2476,13 +2523,13 @@
|
||||
result = false;
|
||||
|
||||
fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex) || 0;
|
||||
if (typeof length == 'number') {
|
||||
if (length && typeof length == 'number') {
|
||||
result = (isString(collection)
|
||||
? collection.indexOf(target, fromIndex)
|
||||
: indexOf(collection, target, fromIndex)
|
||||
: basicIndexOf(collection, target, fromIndex)
|
||||
) > -1;
|
||||
} else {
|
||||
each(collection, function(value) {
|
||||
basicEach(collection, function(value) {
|
||||
if (++index >= fromIndex) {
|
||||
return !(result = value === target);
|
||||
}
|
||||
@@ -2590,7 +2637,7 @@
|
||||
}
|
||||
}
|
||||
} else {
|
||||
each(collection, function(value, index, collection) {
|
||||
basicEach(collection, function(value, index, collection) {
|
||||
return (result = !!callback(value, index, collection));
|
||||
});
|
||||
}
|
||||
@@ -2652,7 +2699,7 @@
|
||||
}
|
||||
}
|
||||
} else {
|
||||
each(collection, function(value, index, collection) {
|
||||
basicEach(collection, function(value, index, collection) {
|
||||
if (callback(value, index, collection)) {
|
||||
result.push(value);
|
||||
}
|
||||
@@ -2719,7 +2766,7 @@
|
||||
}
|
||||
} else {
|
||||
var result;
|
||||
each(collection, function(value, index, collection) {
|
||||
basicEach(collection, function(value, index, collection) {
|
||||
if (callback(value, index, collection)) {
|
||||
result = value;
|
||||
return false;
|
||||
@@ -2762,7 +2809,7 @@
|
||||
}
|
||||
}
|
||||
} else {
|
||||
each(collection, callback, thisArg);
|
||||
basicEach(collection, callback, thisArg);
|
||||
}
|
||||
return collection;
|
||||
}
|
||||
@@ -2897,7 +2944,7 @@
|
||||
result[index] = callback(collection[index], index, collection);
|
||||
}
|
||||
} else {
|
||||
each(collection, function(value, key, collection) {
|
||||
basicEach(collection, function(value, key, collection) {
|
||||
result[++index] = callback(value, key, collection);
|
||||
});
|
||||
}
|
||||
@@ -2962,7 +3009,7 @@
|
||||
? charAtCallback
|
||||
: lodash.createCallback(callback, thisArg);
|
||||
|
||||
each(collection, function(value, index, collection) {
|
||||
basicEach(collection, function(value, index, collection) {
|
||||
var current = callback(value, index, collection);
|
||||
if (current > computed) {
|
||||
computed = current;
|
||||
@@ -3031,7 +3078,7 @@
|
||||
? charAtCallback
|
||||
: lodash.createCallback(callback, thisArg);
|
||||
|
||||
each(collection, function(value, index, collection) {
|
||||
basicEach(collection, function(value, index, collection) {
|
||||
var current = callback(value, index, collection);
|
||||
if (current < computed) {
|
||||
computed = current;
|
||||
@@ -3109,7 +3156,7 @@
|
||||
accumulator = callback(accumulator, collection[index], index, collection);
|
||||
}
|
||||
} else {
|
||||
each(collection, function(value, index, collection) {
|
||||
basicEach(collection, function(value, index, collection) {
|
||||
accumulator = noaccum
|
||||
? (noaccum = false, value)
|
||||
: callback(accumulator, value, index, collection)
|
||||
@@ -3312,7 +3359,7 @@
|
||||
}
|
||||
}
|
||||
} else {
|
||||
each(collection, function(value, index, collection) {
|
||||
basicEach(collection, function(value, index, collection) {
|
||||
return !(result = callback(value, index, collection));
|
||||
});
|
||||
}
|
||||
@@ -3637,20 +3684,11 @@
|
||||
* _.flatten(stooges, 'quotes');
|
||||
* // => ['Oh, a wise guy, eh?', 'Poifect!', 'Spread out!', 'You knucklehead!']
|
||||
*/
|
||||
function flatten(array, isShallow, callback, thisArg) {
|
||||
var flatten = overloadWrapper(function flatten(array, isShallow, callback) {
|
||||
var index = -1,
|
||||
length = array ? array.length : 0,
|
||||
result = [];
|
||||
|
||||
// juggle arguments
|
||||
if (typeof isShallow != 'boolean' && isShallow != null) {
|
||||
thisArg = callback;
|
||||
callback = !(thisArg && thisArg[isShallow] === array) ? isShallow : undefined;
|
||||
isShallow = false;
|
||||
}
|
||||
if (callback != null) {
|
||||
callback = lodash.createCallback(callback, thisArg);
|
||||
}
|
||||
while (++index < length) {
|
||||
var value = array[index];
|
||||
if (callback) {
|
||||
@@ -3664,7 +3702,7 @@
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Gets the index at which the first occurrence of `value` is found using
|
||||
@@ -3691,21 +3729,14 @@
|
||||
* // => 2
|
||||
*/
|
||||
function indexOf(array, value, fromIndex) {
|
||||
var index = -1,
|
||||
length = array ? array.length : 0;
|
||||
|
||||
if (typeof fromIndex == 'number') {
|
||||
index = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex || 0) - 1;
|
||||
var length = array ? array.length : 0;
|
||||
fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex || 0);
|
||||
} else if (fromIndex) {
|
||||
index = sortedIndex(array, value);
|
||||
var index = sortedIndex(array, value);
|
||||
return array[index] === value ? index : -1;
|
||||
}
|
||||
while (++index < length) {
|
||||
if (array[index] === value) {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
return array ? basicIndexOf(array, value, fromIndex) : -1;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -3801,7 +3832,7 @@
|
||||
function intersection(array) {
|
||||
var args = arguments,
|
||||
argsLength = args.length,
|
||||
cache = createCache([]),
|
||||
cache = createCache(),
|
||||
caches = {},
|
||||
index = -1,
|
||||
length = array ? array.length : 0,
|
||||
@@ -4150,7 +4181,7 @@
|
||||
* Creates a duplicate-value-free version of the `array` using strict equality
|
||||
* for comparisons, i.e. `===`. If the `array` is already sorted, passing `true`
|
||||
* for `isSorted` will run a faster algorithm. If `callback` is passed, each
|
||||
* element of `array` is passed through a `callback` before uniqueness is computed.
|
||||
* element of `array` is passed through the `callback` before uniqueness is computed.
|
||||
* The `callback` is bound to `thisArg` and invoked with three arguments; (value, index, array).
|
||||
*
|
||||
* If a property name is passed for `callback`, the created "_.pluck" style
|
||||
@@ -4179,44 +4210,30 @@
|
||||
* _.uniq([1, 1, 2, 2, 3], true);
|
||||
* // => [1, 2, 3]
|
||||
*
|
||||
* _.uniq([1, 2, 1.5, 3, 2.5], function(num) { return Math.floor(num); });
|
||||
* // => [1, 2, 3]
|
||||
* _.uniq(['A', 'b', 'C', 'a', 'B', 'c'], function(letter) { return letter.toLowerCase(); });
|
||||
* // => ['A', 'b', 'C']
|
||||
*
|
||||
* _.uniq([1, 2, 1.5, 3, 2.5], function(num) { return this.floor(num); }, Math);
|
||||
* // => [1, 2, 3]
|
||||
* _.uniq([1, 2.5, 3, 1.5, 2, 3.5], function(num) { return this.floor(num); }, Math);
|
||||
* // => [1, 2.5, 3]
|
||||
*
|
||||
* // using "_.pluck" callback shorthand
|
||||
* _.uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
|
||||
* // => [{ 'x': 1 }, { 'x': 2 }]
|
||||
*/
|
||||
function uniq(array, isSorted, callback, thisArg) {
|
||||
var uniq = overloadWrapper(function(array, isSorted, callback) {
|
||||
var index = -1,
|
||||
length = array ? array.length : 0,
|
||||
isLarge = !isSorted && length >= largeArraySize,
|
||||
result = [],
|
||||
seen = result;
|
||||
seen = isLarge ? createCache() : (callback ? [] : result);
|
||||
|
||||
// juggle arguments
|
||||
if (typeof isSorted != 'boolean' && isSorted != null) {
|
||||
thisArg = callback;
|
||||
callback = !(thisArg && thisArg[isSorted] === array) ? isSorted : undefined;
|
||||
isSorted = false;
|
||||
}
|
||||
// init value cache for large arrays
|
||||
var isLarge = !isSorted && length >= largeArraySize;
|
||||
if (callback != null) {
|
||||
seen = [];
|
||||
callback = lodash.createCallback(callback, thisArg);
|
||||
}
|
||||
if (isLarge) {
|
||||
seen = createCache([]);
|
||||
}
|
||||
while (++index < length) {
|
||||
var value = array[index],
|
||||
computed = callback ? callback(value, index, array) : value;
|
||||
|
||||
if (isSorted
|
||||
? !index || seen[seen.length - 1] !== computed
|
||||
: (isLarge ? !seen.contains(computed) : indexOf(seen, computed) < 0)
|
||||
: (isLarge ? !seen.contains(computed) : basicIndexOf(seen, computed) < 0)
|
||||
) {
|
||||
if (callback || isLarge) {
|
||||
seen.push(computed);
|
||||
@@ -4225,7 +4242,7 @@
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* The inverse of `_.zip`, this method splits groups of elements into arrays
|
||||
@@ -5608,7 +5625,7 @@
|
||||
lodash.prototype.valueOf = wrapperValueOf;
|
||||
|
||||
// add `Array` functions that return unwrapped values
|
||||
each(['join', 'pop', 'shift'], function(methodName) {
|
||||
basicEach(['join', 'pop', 'shift'], function(methodName) {
|
||||
var func = arrayProto[methodName];
|
||||
lodash.prototype[methodName] = function() {
|
||||
return func.apply(this.__wrapped__, arguments);
|
||||
@@ -5616,7 +5633,7 @@
|
||||
});
|
||||
|
||||
// add `Array` functions that return the wrapped value
|
||||
each(['push', 'reverse', 'sort', 'unshift'], function(methodName) {
|
||||
basicEach(['push', 'reverse', 'sort', 'unshift'], function(methodName) {
|
||||
var func = arrayProto[methodName];
|
||||
lodash.prototype[methodName] = function() {
|
||||
func.apply(this.__wrapped__, arguments);
|
||||
@@ -5625,7 +5642,7 @@
|
||||
});
|
||||
|
||||
// add `Array` functions that return new wrapped values
|
||||
each(['concat', 'slice', 'splice'], function(methodName) {
|
||||
basicEach(['concat', 'slice', 'splice'], function(methodName) {
|
||||
var func = arrayProto[methodName];
|
||||
lodash.prototype[methodName] = function() {
|
||||
return new lodashWrapper(func.apply(this.__wrapped__, arguments));
|
||||
@@ -5635,7 +5652,7 @@
|
||||
// avoid array-like object bugs with `Array#shift` and `Array#splice`
|
||||
// in Firefox < 10 and IE < 9
|
||||
if (!support.spliceObjects) {
|
||||
each(['pop', 'shift', 'splice'], function(methodName) {
|
||||
basicEach(['pop', 'shift', 'splice'], function(methodName) {
|
||||
var func = arrayProto[methodName],
|
||||
isSplice = methodName == 'splice';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user