Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var getLength = require('../internal/getLength'),
2
isLength = require('../internal/isLength'),
3
keys = require('../object/keys');
4
5
/**
6
* Gets the size of `collection` by returning its length for array-like
7
* values or the number of own enumerable properties for objects.
8
*
9
* @static
10
* @memberOf _
11
* @category Collection
12
* @param {Array|Object|string} collection The collection to inspect.
13
* @returns {number} Returns the size of `collection`.
14
* @example
15
*
16
* _.size([1, 2, 3]);
17
* // => 3
18
*
19
* _.size({ 'a': 1, 'b': 2 });
20
* // => 2
21
*
22
* _.size('pebbles');
23
* // => 7
24
*/
25
function size(collection) {
26
var length = collection ? getLength(collection) : 0;
27
return isLength(length) ? length : keys(collection).length;
28
}
29
30
module.exports = size;
31
32