PageRenderTime 11ms CodeModel.GetById 7ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/lodash/size.js

https://bitbucket.org/coleman333/smartsite
JavaScript | 46 lines | 21 code | 3 blank | 22 comment | 7 complexity | 50b2a5e055bef4de615cf61c2e3b05bf MD5 | raw file
 1var baseKeys = require('./_baseKeys'),
 2    getTag = require('./_getTag'),
 3    isArrayLike = require('./isArrayLike'),
 4    isString = require('./isString'),
 5    stringSize = require('./_stringSize');
 6
 7/** `Object#toString` result references. */
 8var mapTag = '[object Map]',
 9    setTag = '[object Set]';
10
11/**
12 * Gets the size of `collection` by returning its length for array-like
13 * values or the number of own enumerable string keyed properties for objects.
14 *
15 * @static
16 * @memberOf _
17 * @since 0.1.0
18 * @category Collection
19 * @param {Array|Object|string} collection The collection to inspect.
20 * @returns {number} Returns the collection size.
21 * @example
22 *
23 * _.size([1, 2, 3]);
24 * // => 3
25 *
26 * _.size({ 'a': 1, 'b': 2 });
27 * // => 2
28 *
29 * _.size('pebbles');
30 * // => 7
31 */
32function size(collection) {
33  if (collection == null) {
34    return 0;
35  }
36  if (isArrayLike(collection)) {
37    return isString(collection) ? stringSize(collection) : collection.length;
38  }
39  var tag = getTag(collection);
40  if (tag == mapTag || tag == setTag) {
41    return collection.size;
42  }
43  return baseKeys(collection).length;
44}
45
46module.exports = size;