mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-04 00:57:48 +00:00
Simplify _.size.
Former-commit-id: a7d3338cbd5784ec6b9b6a25e18acd9507f4b21c
This commit is contained in:
71
lodash.js
71
lodash.js
@@ -1920,45 +1920,6 @@
|
||||
'bottom': '}'
|
||||
});
|
||||
|
||||
/**
|
||||
* Gets the size of `value` by returning `value.length` if `value` is an
|
||||
* array, string, or `arguments` object. If `value` is an object, size is
|
||||
* determined by returning the number of own enumerable properties it has.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Objects
|
||||
* @param {Array|Object|String} value The value to inspect.
|
||||
* @returns {Number} Returns `value.length` or number of own enumerable properties.
|
||||
* @example
|
||||
*
|
||||
* _.size([1, 2]);
|
||||
* // => 2
|
||||
*
|
||||
* _.size({ 'one': 1, 'two': 2, 'three': 3 });
|
||||
* // => 3
|
||||
*
|
||||
* _.size('curly');
|
||||
* // => 5
|
||||
*/
|
||||
function size(value) {
|
||||
if (!value) {
|
||||
return 0;
|
||||
}
|
||||
var className = toString.call(value),
|
||||
length = value.length;
|
||||
|
||||
// return `value.length` for `arguments` objects, arrays, strings, and DOM
|
||||
// query collections of libraries like jQuery and MooTools
|
||||
// http://code.google.com/p/fbug/source/browse/branches/firebug1.9/content/firebug/chrome/reps.js?r=12614#653
|
||||
// http://trac.webkit.org/browser/trunk/Source/WebCore/inspector/InjectedScriptSource.js?rev=125186#L609
|
||||
if (arrayLikeClasses[className] || (noArgsClass && isArguments(value)) ||
|
||||
(className == objectClass && length > -1 && length === length >>> 0 && isFunction(value.splice))) {
|
||||
return length;
|
||||
}
|
||||
return keys(value).length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an array composed of the own enumerable property values of `object`.
|
||||
*
|
||||
@@ -2365,6 +2326,34 @@
|
||||
'inLoop': '!' + filterIteratorOptions.inLoop
|
||||
});
|
||||
|
||||
/**
|
||||
* Gets the size of the `collection` by returning `collection.length` for arrays
|
||||
* and array-like objects or the number of own enumerable properties for objects.
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Collections
|
||||
* @param {Array|Object|String} collection The collection to inspect.
|
||||
* @returns {Number} Returns `collection.length` or number of own enumerable properties.
|
||||
* @example
|
||||
*
|
||||
* _.size([1, 2]);
|
||||
* // => 2
|
||||
*
|
||||
* _.size({ 'one': 1, 'two': 2, 'three': 3 });
|
||||
* // => 3
|
||||
*
|
||||
* _.size('curly');
|
||||
* // => 5
|
||||
*/
|
||||
function size(collection) {
|
||||
if (!collection) {
|
||||
return 0;
|
||||
}
|
||||
var length = collection.length;
|
||||
return length > -1 && length === length >>> 0 ? length : keys(collection).length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the `callback` returns a truthy value for **any** element of a
|
||||
* `collection`. The function returns as soon as it finds passing value, and
|
||||
@@ -3833,8 +3822,8 @@
|
||||
* @static
|
||||
* @memberOf _
|
||||
* @category Utilities
|
||||
* @param {Number} min The minimum possible value
|
||||
* @param {Number} max The maximum possible value
|
||||
* @param {Number} min The minimum possible value.
|
||||
* @param {Number} max The maximum possible value.
|
||||
* @returns {Number} Returns the random number.
|
||||
* @example
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user