size.js 761 B

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