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 ] @@ -949,6 +949,27 @@ console.log< return keys.every(k => equals(a[k], b[k])); };equals({ a: [2, { e: 3 }], b: [4], c: 'foo' }, { a: [2, { e: 3 }], b: [4], c: 'foo' }); // true +findKey
Returns the first key that satisfies the provided testing function. Otherwise
undefinedis returned.Use
Object.keys(obj)to get all the properties of the object,Array.find()to test the provided function for each key-value pair. The callback receives three arguments - the value, the key and the object.const findKey = (obj, fn) => Object.keys(obj).find(key => fn(obj[key], key, obj)); +findKey( + { + barney: { age: 36, active: true }, + fred: { age: 40, active: false }, + pebbles: { age: 1, active: true } + }, + o => o['active'] +); // 'barney' +findLastKey
Returns the last key that satisfies the provided testing function. Otherwise
undefinedis returned.Use
Object.keys(obj)to get all the properties of the object,Array.reverse()to reverse their order andArray.find()to test the provided function for each key-value pair. The callback receives three arguments - the value, the key and the object.const findLastKey = (obj, fn) => + Object.keys(obj) + .reverse() + .find(key => fn(obj[key], key, obj)); +findLastKey( + { + barney: { age: 36, active: true }, + fred: { age: 40, active: false }, + pebbles: { age: 1, active: true } + }, + o => o['active'] +); // 'pebbles'forOwn
Iterates over all own properties of an object, running a callback for each one.
Use
Object.keys(obj)to get all the properties of the object,Array.forEach()to run the provided function for each key-value pair. The callback receives three arguments - the value, the key and the object.const forOwn = (obj, fn) => Object.keys(obj).forEach(key => fn(obj[key], key, obj));forOwn({ foo: 'bar', a: 1 }, v => console.log(v)); // 'bar', 1forOwnRight
Iterates over all own properties of an object in reverse, running a callback for each one.
Use
Object.keys(obj)to get all the properties of the object,Array.reverse()to reverse their order andArray.forEach()to run the provided function for each key-value pair. The callback receives three arguments - the value, the key and the object.const forOwnRight = (obj, fn) => diff --git a/snippets/findKey.md b/snippets/findKey.md index 96d563f83..81e41d226 100644 --- a/snippets/findKey.md +++ b/snippets/findKey.md @@ -5,10 +5,16 @@ Returns the first key that satisfies the provided testing function. Otherwise `u Use `Object.keys(obj)` to get all the properties of the object, `Array.find()` to test the provided function for each key-value pair. The callback receives three arguments - the value, the key and the object. ```js -const findKey = (obj, fn) => - Object.keys(obj).find(key => fn(obj[key], key, obj)); +const findKey = (obj, fn) => Object.keys(obj).find(key => fn(obj[key], key, obj)); ``` ```js -findKey({barney: { age: 36, active: true }, fred: { age: 40, active: false }, pebbles: { age: 1, active: true }}, o => o['active']); // 'barney' +findKey( + { + barney: { age: 36, active: true }, + fred: { age: 40, active: false }, + pebbles: { age: 1, active: true } + }, + o => o['active'] +); // 'barney' ``` diff --git a/snippets/findLastKey.md b/snippets/findLastKey.md index ee1390a21..1333a98d3 100644 --- a/snippets/findLastKey.md +++ b/snippets/findLastKey.md @@ -6,9 +6,18 @@ Use `Object.keys(obj)` to get all the properties of the object, `Array.reverse() ```js const findLastKey = (obj, fn) => - Object.keys(obj).reverse().find(key => fn(obj[key], key, obj)); + Object.keys(obj) + .reverse() + .find(key => fn(obj[key], key, obj)); ``` ```js -findLastKey({barney: { age: 36, active: true }, fred: { age: 40, active: false }, pebbles: { age: 1, active: true }}, o => o['active']); // 'pebbles' +findLastKey( + { + barney: { age: 36, active: true }, + fred: { age: 40, active: false }, + pebbles: { age: 1, active: true } + }, + o => o['active'] +); // 'pebbles' ```