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 ] @@ -188,6 +188,16 @@ Object.assignthElement
Returns the nth element of an array.
Use
Array.slice()to get an array containing the nth element at the first place. If the index is out of bounds, return[]. Omit the second argument,n, to get the first element of the array.const nthElement = (arr, n = 0) => (n > 0 ? arr.slice(n, n + 1) : arr.slice(n))[0];nthElement(['a', 'b', 'c'], 1); // 'b' nthElement(['a', 'b', 'b'], -3); // 'a' +partition
Groups the elements into two arrays, depending on the provided function's truthiness for each element.
Use
Array.reduce()to create an array of two arrays. UseArray.push()to add elements for whichfnreturnstrueto the first array and elements for whichfnreturnsfalseto the second one.const partition = (arr, fn) => + arr.reduce( + (acc, val, i, arr) => { + acc[fn(val, i, arr) ? 0 : 1].push(val); + return acc; + }, + [[], []] + ); +var users = [{ user: 'barney', age: 36, active: false }, { user: 'fred', age: 40, active: true }]; +partition(users, o => o.active); // [[{ 'user': 'fred', 'age': 40, 'active': true }],[{ 'user': 'barney', 'age': 36, 'active': false }]]pick
Picks the key-value pairs corresponding to the given keys from an object.
Use
Array.reduce()to convert the filtered/picked keys back to an object with the corresponding key-value pair if the key exists in the obj.const pick = (obj, arr) => arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {});pick({ a: 1, b: '2', c: 3 }, ['a', 'c']); // { 'a': 1, 'c': 3 } diff --git a/snippets/partition.md b/snippets/partition.md index f347962ee..2f48435d5 100644 --- a/snippets/partition.md +++ b/snippets/partition.md @@ -7,13 +7,16 @@ Use `Array.push()` to add elements for which `fn` returns `true` to the first ar ```js const partition = (arr, fn) => - arr.reduce((acc, val, i, arr) => {acc[fn(val,i,arr) ? 0 :1].push(val); return acc;},[[],[]]); + arr.reduce( + (acc, val, i, arr) => { + acc[fn(val, i, arr) ? 0 : 1].push(val); + return acc; + }, + [[], []] + ); ``` ```js -var users = [ - { 'user': 'barney', 'age': 36, 'active': false }, - { 'user': 'fred', 'age': 40, 'active': true } -]; -partition(users, o => o.active) // [[{ 'user': 'fred', 'age': 40, 'active': true }],[{ 'user': 'barney', 'age': 36, 'active': false }]] +var users = [{ user: 'barney', age: 36, active: false }, { user: 'fred', age: 40, active: true }]; +partition(users, o => o.active); // [[{ 'user': 'fred', 'age': 40, 'active': true }],[{ 'user': 'barney', 'age': 36, 'active': false }]] ```