diff --git a/README.md b/README.md index 75c988bff..e672ec3c8 100644 --- a/README.md +++ b/README.md @@ -261,6 +261,8 @@ average(1, 2, 3); View contents * [`cleanObj`](#cleanobj) +* [`functions`](#functions) +* [`functionsIn`](#functionsin) * [`invertKeyValues`](#invertkeyvalues) * [`lowercaseKeys`](#lowercasekeys) * [`mapKeys`](#mapkeys) @@ -3922,6 +3924,63 @@ cleanObj(testObj, ['a'], 'children'); // { a: 1, children : { a: 1}}
[⬆ Back to top](#table-of-contents) +### 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. + +```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) + + +### functionName + +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' + ); +``` + +
+Examples + +```js +function Foo() { + this.a = () => 1; + this.b = () => 2; +} +Foo.prototype.c = () => 3; +functionsIn(new Foo()); // ['a', 'b', 'c'] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### invertKeyValues Inverts the key-value pairs of an object, without mutating it. @@ -5427,6 +5486,7 @@ const httpPost = (url, callback, data = null, err = console.error) => { + const newPost = { "userId": 1, "id": 1337, diff --git a/docs/index.html b/docs/index.html index 657d6969b..67408ca39 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,6 +849,22 @@ 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']
+

functionName

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'
+  );
+
function Foo() {
+  this.a = () => 1;
+  this.b = () => 2;
+}
+Foo.prototype.c = () => 3;
+functionsIn(new Foo()); // ['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;
@@ -1217,6 +1233,7 @@ Logs: {
 
 
 
+
 const newPost = {
   "userId": 1,
   "id": 1337,
diff --git a/snippets/functions.md b/snippets/functions.md
index 7c3856cee..fff51096a 100644
--- a/snippets/functions.md
+++ b/snippets/functions.md
@@ -5,8 +5,7 @@ Returns an array of function property names from own enumerable properties of ob
 Use `Object.keys(obj)` to iterate over the object's own properties, `Array.filter()` to keep only those that are functions.
 
 ```js
-const functions = obj =>
-  Object.keys(obj).filter(key => typeof obj[key] === 'function');
+const functions = obj => Object.keys(obj).filter(key => typeof obj[key] === 'function');
 ```
 
 ```js
@@ -14,5 +13,5 @@ function Foo() {
   this.a = () => 1;
   this.b = () => 2;
 }
-functions(new Foo); // ['a', 'b']
+functions(new Foo()); // ['a', 'b']
 ```
diff --git a/snippets/functionsIn.md b/snippets/functionsIn.md
index cc0028493..8a2e198c4 100644
--- a/snippets/functionsIn.md
+++ b/snippets/functionsIn.md
@@ -7,7 +7,9 @@ Use the spread operator (`...`) to combine all returned property names into one
 
 ```js
 const functionsIn = obj =>
-  [...Object.keys(obj), ...Object.keys(Object.getPrototypeOf(obj))].filter(key => typeof obj[key] === 'function');
+  [...Object.keys(obj), ...Object.keys(Object.getPrototypeOf(obj))].filter(
+    key => typeof obj[key] === 'function'
+  );
 ```
 
 ```js
@@ -16,5 +18,5 @@ function Foo() {
   this.b = () => 2;
 }
 Foo.prototype.c = () => 3;
-functionsIn(new Foo); // ['a', 'b', 'c']
+functionsIn(new Foo()); // ['a', 'b', 'c']
 ```
diff --git a/snippets/httpPost.md b/snippets/httpPost.md
index 26181cd30..7334a93cf 100644
--- a/snippets/httpPost.md
+++ b/snippets/httpPost.md
@@ -36,6 +36,7 @@ const httpPost = (url, callback, data = null, err = console.error) => {
 
 
 
+
 const newPost = {
   "userId": 1,
   "id": 1337,