From 8ecb88497b76285f819afbd2a7f144b02d068d2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Holl=C3=B3?= Date: Sat, 30 Dec 2017 15:46:01 +0100 Subject: [PATCH 1/3] Create size.md --- snippets/size.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 snippets/size.md diff --git a/snippets/size.md b/snippets/size.md new file mode 100644 index 000000000..42b3cfd9d --- /dev/null +++ b/snippets/size.md @@ -0,0 +1,15 @@ +### 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 +``` From f8b2499bfcd8e61161817ea94106f41dbc62dc22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Holl=C3=B3?= Date: Sat, 30 Dec 2017 17:27:37 +0100 Subject: [PATCH 2/3] Add line breaks between conditions --- snippets/size.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/snippets/size.md b/snippets/size.md index 42b3cfd9d..61c50e19e 100644 --- a/snippets/size.md +++ b/snippets/size.md @@ -5,7 +5,14 @@ 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; +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 From 0e2a19c671fa82ef7b3bc8243d119f0452bfb394 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 31 Dec 2017 12:42:08 +0200 Subject: [PATCH 3/3] Update size.md --- snippets/size.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/snippets/size.md b/snippets/size.md index 61c50e19e..92b3a34a1 100644 --- a/snippets/size.md +++ b/snippets/size.md @@ -2,7 +2,12 @@ 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. +Get type of `value` (`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 `value` for strings. + +Split strings into array of characters with `split('')` and return its length. ```js const size = value => @@ -11,7 +16,7 @@ const size = value => : value && typeof value === 'object' ? value.size || value.length || Object.keys(value).length : typeof value === 'string' - ? value.split('').length + ? new Blob([value]).size : 0; ```