From b660101f808f527ba451909bbb1600ba8867ec3e Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Fri, 12 Jan 2018 11:39:03 +0000 Subject: [PATCH] Travis build: 1209 --- README.md | 47 +++++++++++-------------------------------- docs/index.html | 20 ++++++++---------- snippets/functions.md | 5 ++++- 3 files changed, 24 insertions(+), 48 deletions(-) diff --git a/README.md b/README.md index 8d9697041..22b6a898a 100644 --- a/README.md +++ b/README.md @@ -262,7 +262,6 @@ average(1, 2, 3); * [`cleanObj`](#cleanobj) * [`functions`](#functions) -* [`functionsIn`](#functionsin) * [`invertKeyValues`](#invertkeyvalues) * [`lowercaseKeys`](#lowercasekeys) * [`mapKeys`](#mapkeys) @@ -3926,42 +3925,19 @@ cleanObj(testObj, ['a'], 'children'); // { a: 1, children : { a: 1}} ### functions -Returns an array of function property names from own enumerable properties of object. +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, `Array.filter()` to keep only those that are functions. +Use `Object.keys(obj)` to iterate over the object's own properties. +If `inherited` is `true`, use `Object.get.PrototypeOf(obj)` to also get the object's inherited properties. +Use `Array.filter()` to keep only those properties that are functions. +Omit the second argument, `inherited`, to not include inherited properties by default. ```js -const functions = obj => Object.keys(obj).filter(key => typeof obj[key] === 'function'); -``` - -
-Examples - -```js -function Foo() { - this.a = () => 1; - this.b = () => 2; -} -functions(new Foo()); // ['a', 'b'] -``` - -
- -
[⬆ Back to top](#table-of-contents) - - -### functionsIn - -Returns an array of function property names from own and inherited enumerable properties of object. - -Use `Object.keys(obj)` and `Object.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. - -```js -const functionsIn = obj => - [...Object.keys(obj), ...Object.keys(Object.getPrototypeOf(obj))].filter( - key => typeof obj[key] === 'function' - ); +const functions = (obj, inherited = false) => + (inherited + ? [...Object.keys(obj), ...Object.keys(Object.getPrototypeOf(obj))] + : Object.keys(obj) + ).filter(key => typeof obj[key] === 'function'); ```
@@ -3973,7 +3949,8 @@ function Foo() { 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'] ```
diff --git a/docs/index.html b/docs/index.html index c3eb24e3e..fc405c7be 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 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);
+      }

logo 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) and Object.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. If inherited is true, use Object.get.PrototypeOf(obj) to also get the object's inherited properties. Use Array.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() and Array.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