diff --git a/README.md b/README.md index d056ac44a..f374490a5 100644 --- a/README.md +++ b/README.md @@ -258,6 +258,15 @@ +### _Uncategorized_ + +
+View contents + +* [`size`](#size) + +
+ --- ## 🔌 Adapter @@ -4296,6 +4305,37 @@ yesNo('Foo', true); // true
[⬆ Back to top](#table-of-contents) +--- + ## _Uncategorized_ + +### size + +Get size of arrays, objects or strings. + +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 => + Array.isArray(value) + ? value.length + : value && typeof value === 'object' + ? value.size || value.length || Object.keys(value).length + : typeof value === 'string' ? new Blob([value]).size : 0; +``` + +```js +size([1, 2, 3, 4, 5]); // 5 +size('size'); // 4 +size({ one: 1, two: 2, three: 3 }); // 3 +``` + +
[⬆ back to top](#table-of-contents) + ## Credits diff --git a/docs/index.html b/docs/index.html index 2c46f0eeb..5d0befb5a 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wrapper.appendChild(box); box.appendChild(el); }); - }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+    }

 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -888,4 +888,13 @@ console.log(sdbm('age')); // 808122783
 yesNo('yes'); // true
 yesNo('No'); // false
 yesNo('Foo', true); // true
+

Uncategorized

size

Get size of arrays, objects or strings.

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 created from value for strings.

Split strings into array of characters with split('') and return its length.

const size = value =>
+  Array.isArray(value)
+    ? value.length
+    : value && typeof value === 'object'
+      ? value.size || value.length || Object.keys(value).length
+      : typeof value === 'string' ? new Blob([value]).size : 0;
+
size([1, 2, 3, 4, 5]); // 5
+size('size'); // 4
+size({ one: 1, two: 2, three: 3 }); // 3
 

\ No newline at end of file diff --git a/snippets/size.md b/snippets/size.md index 92b3a34a1..1c3127929 100644 --- a/snippets/size.md +++ b/snippets/size.md @@ -14,14 +14,12 @@ const size = value => Array.isArray(value) ? value.length : value && typeof value === 'object' - ? value.size || value.length || Object.keys(value).length - : typeof value === 'string' - ? new Blob([value]).size - : 0; + ? value.size || value.length || Object.keys(value).length + : typeof value === 'string' ? new Blob([value]).size : 0; ``` ```js -size([ 1, 2, 3, 4, 5 ]); // 5 +size([1, 2, 3, 4, 5]); // 5 size('size'); // 4 size({ one: 1, two: 2, three: 3 }); // 3 ``` diff --git a/tag_database b/tag_database index 5bcb338a5..4b47db2fc 100644 --- a/tag_database +++ b/tag_database @@ -121,6 +121,7 @@ shallowClone:object show:browser shuffle:array similarity:array +size:uncategorized sleep:function sortCharactersInString:string speechSynthesis:browser