Files
30-seconds-of-code/snippets/size.md
2017-12-30 17:27:37 +01:00

23 lines
632 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

### size
Get size of arrays, objects or strings.
Get type of value array, object and string. Use `length` property for arrays. Return `length` or `size` value if available or number of object keys. Split strings into array of characters with `split('')` and return its length.
```js
const size = value =>
Array.isArray(value)
? value.length
: value && typeof value === 'object'
? value.size || value.length || Object.keys(value).length
: typeof value === 'string'
? value.split('').length
: 0;
```
```js
size([ 1, 2, 3, 4, 5 ]); // 5
size('size'); // 4
size({ one: 1, two: 2, three: 3 }); // 3
```