main
Last change
on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[d565449] | 1 | import baseKeys from './_baseKeys.js';
|
---|
| 2 | import getTag from './_getTag.js';
|
---|
| 3 | import isArrayLike from './isArrayLike.js';
|
---|
| 4 | import isString from './isString.js';
|
---|
| 5 | import stringSize from './_stringSize.js';
|
---|
| 6 |
|
---|
| 7 | /** `Object#toString` result references. */
|
---|
| 8 | var 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 | */
|
---|
| 32 | function 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 |
|
---|
| 46 | export default size;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.