Cleaned up the current snippets for consistency and minor problems, added multiple tags to most of them, archived a few.
26 lines
732 B
Markdown
26 lines
732 B
Markdown
### size
|
|
|
|
Get size of arrays, objects or strings.
|
|
|
|
Get type of `val` (`array`, `object` or `string`).
|
|
Use `length` property for arrays.
|
|
Use `length` or `size` value if available or number of keys for objects.
|
|
Use `size` of a [`Blob` object](https://developer.mozilla.org/en-US/docs/Web/API/Blob) created from `val` for strings.
|
|
|
|
Split strings into array of characters with `split('')` and return its length.
|
|
|
|
```js
|
|
const size = val =>
|
|
Array.isArray(val)
|
|
? val.length
|
|
: val && typeof val === 'object'
|
|
? val.size || val.length || Object.keys(val).length
|
|
: typeof val === 'string' ? new Blob([val]).size : 0;
|
|
```
|
|
|
|
```js
|
|
size([1, 2, 3, 4, 5]); // 5
|
|
size('size'); // 4
|
|
size({ one: 1, two: 2, three: 3 }); // 3
|
|
```
|