From 38b1eb36fc7a5c41d716df9b8b6a2a730455ba5a Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Fri, 19 Jan 2018 11:24:50 +0000 Subject: [PATCH] Travis build: 1317 --- README.md | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ docs/index.html | 12 ++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index eb58ef8a6..fd9d07be5 100644 --- a/README.md +++ b/README.md @@ -284,8 +284,10 @@ average(1, 2, 3); * [`objectFromPairs`](#objectfrompairs) * [`objectToPairs`](#objecttopairs) * [`omit`](#omit) +* [`omitBy`](#omitby) * [`orderBy`](#orderby) * [`pick`](#pick) +* [`pickBy`](#pickby) * [`shallowClone`](#shallowclone) * [`size`](#size) * [`transform`](#transform) @@ -4500,6 +4502,31 @@ omit({ a: 1, b: '2', c: 3 }, ['b']); // { 'a': 1, 'c': 3 }
[⬆ Back to top](#table-of-contents) +### omitBy + +Creates an object composed of the properties the given function returns falsey for. The function is invoked with two arguments: (value, key). + +Use `Object.keys(obj)` and `Array.filter()`to remove the keys for which `fn` returns a truthy value. +Use `Array.reduce()` to convert the filtered keys back to an object with the corresponding key-value pairs. + +```js +const omitBy = (obj, fn) => + Object.keys(obj) + .filter(k => !fn(obj[k], k)) + .reduce((acc, key) => ((acc[key] = obj[key]), acc), {}); +``` + +
+Examples + +```js +omitBy({ a: 1, b: '2', c: 3 }, x => typeof x === 'number'); // { b: '2' } + +
+ +
[⬆ Back to top](#table-of-contents) + + ### orderBy Returns a sorted array of objects ordered by properties and orders. @@ -4557,6 +4584,32 @@ pick({ a: 1, b: '2', c: 3 }, ['a', 'c']); // { 'a': 1, 'c': 3 }
[⬆ Back to top](#table-of-contents) +### pickBy + +Creates an object composed of the properties the given function returns truthy for. The function is invoked with two arguments: (value, key). + +Use `Object.keys(obj)` and `Array.filter()`to remove the keys for which `fn` returns a falsey value. +Use `Array.reduce()` to convert the filtered keys back to an object with the corresponding key-value pairs. + +```js +const pickBy = (obj, fn) => + Object.keys(obj) + .filter(k => fn(obj[k], k)) + .reduce((acc, key) => ((acc[key] = obj[key]), acc), {}); +``` + +
+Examples + +```js +pickBy({ a: 1, b: '2', c: 3 }, x => typeof x === 'number'); // { 'a': 1, 'c': 3 } +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### shallowClone Creates a shallow clone of an object. diff --git a/docs/index.html b/docs/index.html index d173421fa..b294be08d 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 ]
@@ -1010,6 +1010,11 @@ Foo.prototypefilter(k => !arr.includes(k))
     .reduce((acc, key) => ((acc[key] = obj[key]), acc), {});
 
omit({ a: 1, b: '2', c: 3 }, ['b']); // { 'a': 1, 'c': 3 }
+

omitBy

Creates an object composed of the properties the given function returns falsey for. The function is invoked with two arguments: (value, key).

Use Object.keys(obj) and Array.filter()to remove the keys for which fn returns a truthy value. Use Array.reduce() to convert the filtered keys back to an object with the corresponding key-value pairs.

const omitBy = (obj, fn) =>
+  Object.keys(obj)
+    .filter(k => !fn(obj[k], k))
+    .reduce((acc, key) => ((acc[key] = obj[key]), acc), {});
+
omitBy({ a: 1, b: '2', c: 3 }, x => typeof x === 'number'); // { b: '2' }
 

orderBy

Returns a sorted array of objects ordered by properties and orders.

Uses Array.sort(), Array.reduce() on the props array with a default value of 0, use array destructuring to swap the properties position depending on the order passed. If no orders array is passed it sort by 'asc' by default.

const orderBy = (arr, props, orders) =>
   [...arr].sort((a, b) =>
     props.reduce((acc, prop, i) => {
@@ -1026,6 +1031,11 @@ Foo.prototype📋 Copy to clipboard

pick

Picks the key-value pairs corresponding to the given keys from an object.

Use Array.reduce() to convert the filtered/picked keys back to an object with the corresponding key-value pairs if the key exists in the object.

const pick = (obj, arr) =>
   arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {});
 
pick({ a: 1, b: '2', c: 3 }, ['a', 'c']); // { 'a': 1, 'c': 3 }
+

pickBy

Creates an object composed of the properties the given function returns truthy for. The function is invoked with two arguments: (value, key).

Use Object.keys(obj) and Array.filter()to remove the keys for which fn returns a falsey value. Use Array.reduce() to convert the filtered keys back to an object with the corresponding key-value pairs.

const pickBy = (obj, fn) =>
+  Object.keys(obj)
+    .filter(k => fn(obj[k], k))
+    .reduce((acc, key) => ((acc[key] = obj[key]), acc), {});
+
pickBy({ a: 1, b: '2', c: 3 }, x => typeof x === 'number'); // { 'a': 1, 'c': 3 }
 

shallowClone

Creates a shallow clone of an object.

Use Object.assign() and an empty object ({}) to create a shallow clone of the original.

const shallowClone = obj => Object.assign({}, obj);
 
const a = { x: true, y: 1 };
 const b = shallowClone(a); // a !== b