From 606e88fd01e896c0d357e0b9f297d04f81eeae95 Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Fri, 19 Jan 2018 01:08:43 +0000 Subject: [PATCH] Travis build: 1310 --- README.md | 49 +++++++++++++++++++++++++++++++++++++++++ docs/index.html | 9 +++++++- snippets/forOwn.md | 4 ++-- snippets/forOwnRight.md | 7 ++++-- 4 files changed, 64 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0896ddf02..6219cc983 100644 --- a/README.md +++ b/README.md @@ -274,6 +274,8 @@ average(1, 2, 3); * [`cleanObj`](#cleanobj) * [`equals`](#equals-) +* [`forOwn`](#forown) +* [`forOwnRight`](#forownright) * [`functions`](#functions) * [`get`](#get) * [`invertKeyValues`](#invertkeyvalues) @@ -4220,6 +4222,53 @@ equals({ a: [2, { e: 3 }], b: [4], c: 'foo' }, { a: [2, { e: 3 }], b: [4], c: 'f
[⬆ Back to top](#table-of-contents) +### 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. + +```js +const forOwn = (obj, fn) => Object.keys(obj).forEach(key => fn(obj[key], key, obj)); +``` + +
+Examples + +```js +forOwn({ foo: 'bar', a: 1 }, v => console.log(v)); // 'bar', 1 +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + +### 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 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)); +``` + +
+Examples + +```js +forOwnRight({ foo: 'bar', a: 1 }, v => console.log(v)); // 1, 'bar' +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### functions Returns an array of function property names from own (and optionally inherited) enumerable properties of an object. diff --git a/docs/index.html b/docs/index.html index 60ef59e4e..ece1300fc 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 ]
@@ -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 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.

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. 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))]
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'
 ```