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 ] @@ -939,6 +939,13 @@ 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 +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', 1 +forOwnRight
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) => + Object.keys(obj) + .reverse() + .forEach(key => fn(obj[key], key, obj)); +forOwnRight({ foo: 'bar', a: 1 }, v => console.log(v)); // 1, 'bar'functions
Returns an array of function property names from own (and optionally inherited) enumerable properties of an object.
Use
Object.keys(obj)to iterate over the object's own properties. Ifinheritedistrue, useObject.get.PrototypeOf(obj)to also get the object's inherited properties. UseArray.filter()to keep only those properties that are functions. Omit the second argument,inherited, to not include inherited properties by default.const functions = (obj, inherited = false) => (inherited ? [...Object.keys(obj), ...Object.keys(Object.getPrototypeOf(obj))] diff --git a/snippets/forOwn.md b/snippets/forOwn.md index 7618f4618..054bbac79 100644 --- a/snippets/forOwn.md +++ b/snippets/forOwn.md @@ -5,9 +5,9 @@ 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. ```js -const forOwn = (obj, fn) => Object.keys(obj).forEach(key => fn(obj[key],key,obj)); +const forOwn = (obj, fn) => Object.keys(obj).forEach(key => fn(obj[key], key, obj)); ``` ```js -forOwn({foo: 'bar', a: 1}, v => console.log(v)); // 'bar', 1 +forOwn({ foo: 'bar', a: 1 }, v => console.log(v)); // 'bar', 1 ``` diff --git a/snippets/forOwnRight.md b/snippets/forOwnRight.md index 7a85c9d15..31094c832 100644 --- a/snippets/forOwnRight.md +++ b/snippets/forOwnRight.md @@ -5,9 +5,12 @@ Iterates over all own properties of an object in reverse, running a callback for Use `Object.keys(obj)` to get all the properties of the object, `Array.reverse()` to reverse their order and `Array.forEach()` to run the provided function for each key-value pair. The callback receives three arguments - the value, the key and the object. ```js -const forOwnRight = (obj, fn) => Object.keys(obj).reverse().forEach(key => fn(obj[key],key,obj)); +const forOwnRight = (obj, fn) => + Object.keys(obj) + .reverse() + .forEach(key => fn(obj[key], key, obj)); ``` ```js -forOwnRight({foo: 'bar', a: 1}, v => console.log(v)); // 1, 'bar' +forOwnRight({ foo: 'bar', a: 1 }, v => console.log(v)); // 1, 'bar' ```