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 ] @@ -849,22 +849,18 @@ console.log< };const testObj = { a: 1, b: 2, children: { a: 1, b: 2 } }; cleanObj(testObj, ['a'], 'children'); // { a: 1, children : { a: 1}} -functions
Returns an array of function property names from own enumerable properties of object.
Use
Object.keys(obj)to iterate over the object's own properties,Array.filter()to keep only those that are functions.const functions = obj => Object.keys(obj).filter(key => typeof obj[key] === 'function'); -function Foo() { - this.a = () => 1; - this.b = () => 2; -} -functions(new Foo()); // ['a', 'b'] -functionsIn
Returns an array of function property names from own and inherited enumerable properties of object.
Use
Object.keys(obj)andObject.get.PrototypeOf(obj)to iterate over the object's own and inherited properties,Array.filter()to keep only those that are functions. Use the spread operator (...) to combine all returned property names into one array.const functionsIn = obj => - [...Object.keys(obj), ...Object.keys(Object.getPrototypeOf(obj))].filter( - key => typeof obj[key] === 'function' - ); +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))] + : Object.keys(obj) + ).filter(key => typeof obj[key] === 'function');function Foo() { this.a = () => 1; this.b = () => 2; } Foo.prototype.c = () => 3; -functionsIn(new Foo()); // ['a', 'b', 'c'] +functions(new Foo()); // ['a', 'b'] +functions(new Foo(), true); // ['a', 'b', 'c']invertKeyValues
Inverts the key-value pairs of an object, without mutating it.
Use
Object.keys()andArray.reduce()to invert the key-value pairs of an object.const invertKeyValues = obj => Object.keys(obj).reduce((acc, key) => { acc[obj[key]] = key; diff --git a/snippets/functions.md b/snippets/functions.md index 3c9550d34..7dd588fe8 100644 --- a/snippets/functions.md +++ b/snippets/functions.md @@ -9,7 +9,10 @@ Omit the second argument, `inherited`, to not include inherited properties by de ```js const functions = (obj, inherited = false) => - (inherited ? [...Object.keys(obj), ...Object.keys(Object.getPrototypeOf(obj))] : Object.keys(obj)).filter(key => typeof obj[key] === 'function'); + (inherited + ? [...Object.keys(obj), ...Object.keys(Object.getPrototypeOf(obj))] + : Object.keys(obj) + ).filter(key => typeof obj[key] === 'function'); ``` ```js