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 ] @@ -924,6 +924,15 @@ Foo.prototypeShow examplessize([1, 2, 3, 4, 5]); // 5 size('size'); // 4 size({ one: 1, two: 2, three: 3 }); // 3 +transform
Applies a function against an accumulator and each key in the object (from left to right).
Use
Object.keys(obj)to iterate over each key in the object,Array.reduce()to call the apply the specified function against the given accumulator.const transform = (obj, fn, acc) => Object.keys(obj).reduce((a, k) => fn(a, obj[k], k, obj), acc); +transform( + { a: 1, b: 2, c: 1 }, + (r, v, k) => { + (r[v] || (r[v] = [])).push(k); + return r; + }, + {} +); // { '1': ['a', 'c'], '2': ['b'] }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'); // trueString
anagrams
⚠️ WARNING: This function's execution time increases exponentially with each character. Anything more than 8 to 10 characters will cause your browser to hang as it tries to solve all the different combinations.
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, thenArray.reduce()to combine all anagrams in one array. Base cases are for stringlengthequal to2or1.const anagrams = str => { diff --git a/snippets/transform.md b/snippets/transform.md index de8d1236d..0e7573494 100644 --- a/snippets/transform.md +++ b/snippets/transform.md @@ -5,13 +5,16 @@ Applies a function against an accumulator and each key in the object (from left Use `Object.keys(obj)` to iterate over each key in the object, `Array.reduce()` to call the apply the specified function against the given accumulator. ```js -const transform = (obj, fn, acc) => - Object.keys(obj).reduce((a, k) => fn(a, obj[k], k, obj), acc); +const transform = (obj, fn, acc) => Object.keys(obj).reduce((a, k) => fn(a, obj[k], k, obj), acc); ``` ```js -transform({ 'a': 1, 'b': 2, 'c': 1 }, (r, v, k) => { - (r[v] || (r[v] = [])).push(k); - return r; -}, {}); // { '1': ['a', 'c'], '2': ['b'] } +transform( + { a: 1, b: 2, c: 1 }, + (r, v, k) => { + (r[v] || (r[v] = [])).push(k); + return r; + }, + {} +); // { '1': ['a', 'c'], '2': ['b'] } ```