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 ] @@ -1031,6 +1031,23 @@ Foo.prototype: { user: 'pebbles', age: 1 } }; mapValues(users, u => u.age); // { fred: 40, pebbles: 1 } +matches
Compares two objects to determine if the first one contains equivalent property values to the second one.
Use
Object.keys(source)to get all the keys of the second object, thenArray.every(),Object.hasOwnProperty()and strict comparison to determine if all keys exist in the first object and have the same values.const matches = (obj, source) => + Object.keys(source).every(key => obj.hasOwnProperty(key) && obj[key] === source[key]); +matches({ age: 25, hair: 'long', beard: true }, { hair: 'long', beard: true }); // true +matches({ hair: 'long', beard: true }, { age: 25, hair: 'long', beard: true }); // false +matchesWith
Compares two objects to determine if the first one contains equivalent property values to the second one, based on a provided function.
Use
Object.keys(source)to get all the keys of the second object, thenArray.every(),Object.hasOwnProperty()and the provided function to determine if all keys exist in the first object and have equivalent values. If no function is provided, the values will be compared using the equality operator.const matchesWith = (obj, source, fn) => + Object.keys(source).every( + key => + obj.hasOwnProperty(key) && fn + ? fn(obj[key], source[key], key, obj, source) + : obj[key] == source[key] + ); +const isGreeting = val => /^h(?:i|ello)$/.test(val); +matchesWith( + { greeting: 'hello' }, + { greeting: 'hi' }, + (oV, sV) => isGreeting(oV) && isGreeting(sV) +); // truemerge
Creates a new object from the combination of two or more objects.
Use
Array.reduce()combined withObject.keys(obj)to iterate over all objects and keys. UsehasOwnProperty()andArray.concat()to append values for keys existing in multiple objects.const merge = (...objs) => [...objs].reduce( (acc, obj) => diff --git a/snippets/matches.md b/snippets/matches.md index 1be130fb0..47610d3d8 100644 --- a/snippets/matches.md +++ b/snippets/matches.md @@ -6,12 +6,10 @@ Use `Object.keys(source)` to get all the keys of the second object, then `Array. ```js const matches = (obj, source) => - Object.keys(source).every( - key => obj.hasOwnProperty(key) && obj[key] === source[key] - ); + Object.keys(source).every(key => obj.hasOwnProperty(key) && obj[key] === source[key]); ``` ```js -matches({ age: 25, hair: 'long', beard: true }, { hair: 'long', beard: true }) // true -matches({ hair: 'long', beard: true }, { age: 25, hair: 'long', beard: true }) // false +matches({ age: 25, hair: 'long', beard: true }, { hair: 'long', beard: true }); // true +matches({ hair: 'long', beard: true }, { age: 25, hair: 'long', beard: true }); // false ``` diff --git a/snippets/matchesWith.md b/snippets/matchesWith.md index e3777e780..a04814191 100644 --- a/snippets/matchesWith.md +++ b/snippets/matchesWith.md @@ -17,5 +17,9 @@ const matchesWith = (obj, source, fn) => ```js const isGreeting = val => /^h(?:i|ello)$/.test(val); -matchesWith({ greeting: 'hello' }, { greeting: 'hi' }, (oV, sV) => isGreeting(oV) && isGreeting(sV)); // true +matchesWith( + { greeting: 'hello' }, + { greeting: 'hi' }, + (oV, sV) => isGreeting(oV) && isGreeting(sV) +); // true ```