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 ]
@@ -647,6 +647,15 @@ select(obj, 'selector.to.val'); // 'val to select'
const a = { x: true, y: 1 };
const b = shallowClone(a);
a === b; // false
+
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
truthCheckCollection
Checks if the predicate (second argument) is truthy on all elements of a collection (first argument).
Use Array.every() to check if each passed object has the specified property and if it returns a truthy value.
const truthCheckCollection = (collection, pre) => collection.every(obj => obj[pre]);
truthCheckCollection([{ user: 'Tinky-Winky', sex: 'male' }, { user: 'Dipsy', sex: 'male' }], 'sex'); // true
String
anagrams
Generates all anagrams of a string (contains duplicates).
Use recursion. For each letter in the given string, create all the partial anagrams for the rest of its letters. Use Array.map() to combine the letter with each partial anagram, then Array.reduce() to combine all anagrams in one array. Base cases are for string length equal to 2 or 1.
const anagrams = str => {
@@ -888,13 +897,4 @@ 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