From d0b4305a421f4e6d26fa45cfc97737a53b999cd4 Mon Sep 17 00:00:00 2001 From: Travis CI Date: Mon, 1 Jan 2018 15:14:04 +0000 Subject: [PATCH] Travis build: 790 [ci skip] --- README.md | 104 +++++++++++++++++++--------------------- docs/index.html | 57 +++++++++++----------- snippets/isArrayLike.md | 7 ++- 3 files changed, 82 insertions(+), 86 deletions(-) diff --git a/README.md b/README.md index 57a12fcd5..48e178ea4 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,7 @@ * [`pullAtIndex`](#pullatindex) * [`pullAtValue`](#pullatvalue) * [`quickSort`](#quicksort) +* [`reducedFilter`](#reducedfilter) * [`remove`](#remove) * [`sample`](#sample) * [`sampleSize`](#samplesize) @@ -271,15 +272,6 @@ -### _Uncategorized_ - -
-View contents - -* [`reducedFilter`](#reducedfilter) - -
- --- ## 🔌 Adapter @@ -1192,6 +1184,48 @@ quickSort([4, 1, 3, 2], true); // [4,3,2,1]
[⬆ Back to top](#table-of-contents) +### reducedFilter + +Filter an array of objects based on a condition while also filtering out unspecified keys. + +Use `Array.filter()` to filter the array based on the predicate `fn` so that it returns the objects for which the condition returned a truthy value. +On the filtered array, use `Array.map()` to return the new object using `Array.reduce()` to filter out the keys which were not supplied as the `keys` argument. + +```js +const reducedFilter = (data, keys, fn) => + data.filter(fn).map(el => + keys.reduce((acc, key) => { + acc[key] = el[key]; + return acc; + }, {}) + ); +``` + +
+Examples + +```js +const data = [ + { + id: 1, + name: 'john', + age: 24 + }, + { + id: 2, + name: 'mike', + age: 50 + } +]; + +reducedFilter(data, ['id', 'name'], item => item.age > 24); // [{ id: 2, name: 'mike'}] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### remove Removes elements from an array for which the given function returns `false`. @@ -4249,13 +4283,12 @@ Checks if the provided argument is array-like (i.e. is iterable). Use the spread operator (`...`) to check if the provided argument is iterable inside a `try... catch` block and the comma operator (`,`) to return the appropriate value. ```js - - - - -const isArrayLike = val => - try {return [...val], true; } - catch (e) { return false; } +const isArrayLike = val => { + try { + return [...val], true; + } catch (e) { + return false; + } }; ``` @@ -4713,45 +4746,6 @@ yesNo('Foo', true); // true
[⬆ Back to top](#table-of-contents) ---- - ## _Uncategorized_ - -### reducedFilter - -Filter an array of objects based on a condition while also filtering out unspecified keys. - -Use `Array.filter()` to filter the array based on the predicate `fn` so that it returns the objects for which the condition returned a truthy value. -On the filtered array, use `Array.map()` to return the new object using `Array.reduce()` to filter out the keys which were not supplied as the `keys` argument. - -```js -const reducedFilter = (data, keys, fn) => - data.filter(fn).map(el => - keys.reduce((acc, key) => { - acc[key] = el[key]; - return acc; - }, {}) - ); -``` - -```js -const data = [ - { - id: 1, - name: 'john', - age: 24 - }, - { - id: 2, - name: 'mike', - age: 50 - } -]; - -reducedFilter(data, ['id', 'name'], item => item.age > 24); // [{ id: 2, name: 'mike'}] -``` - -
[⬆ back to top](#table-of-contents) - ## Collaborators diff --git a/docs/index.html b/docs/index.html index b72f07002..3d297e8b7 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wrapper.appendChild(box); box.appendChild(el); }); - }

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

 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 ]
@@ -238,6 +238,27 @@ console.log(pulled); // [ 'b', 'd' ]
       ];
 
quickSort([4, 1, 3, 2]); // [1,2,3,4]
 quickSort([4, 1, 3, 2], true); // [4,3,2,1]
+

reducedFilter

Filter an array of objects based on a condition while also filtering out unspecified keys.

Use Array.filter() to filter the array based on the predicate fn so that it returns the objects for which the condition returned a truthy value. On the filtered array, use Array.map() to return the new object using Array.reduce() to filter out the keys which were not supplied as the keys argument.

const reducedFilter = (data, keys, fn) =>
+  data.filter(fn).map(el =>
+    keys.reduce((acc, key) => {
+      acc[key] = el[key];
+      return acc;
+    }, {})
+  );
+
const data = [
+  {
+    id: 1,
+    name: 'john',
+    age: 24
+  },
+  {
+    id: 2,
+    name: 'mike',
+    age: 50
+  }
+];
+
+reducedFilter(data, ['id', 'name'], item => item.age > 24); // [{ id: 2, name: 'mike'}]
 

remove

Removes elements from an array for which the given function returns false.

Use Array.filter() to find array elements that return truthy values and Array.reduce() to remove elements using Array.splice(). The func is invoked with three arguments (value, index, array).

const remove = (arr, func) =>
   Array.isArray(arr)
     ? arr.filter(func).reduce((acc, val) => {
@@ -893,13 +914,12 @@ hexToRGB('#fff'); // 'rgb(255, 255, 255)'
 

isArray

Checks if the given argument is an array.

Use Array.isArray() to check if a value is classified as an array.

const isArray = val => !!val && Array.isArray(val);
 
isArray(null); // false
 isArray([1]); // true
-

isArrayLike

Checks if the provided argument is array-like (i.e. is iterable).

Use the spread operator (...) to check if the provided argument is iterable inside a try... catch block and the comma operator (,) to return the appropriate value.


-
-
-
-const isArrayLike = val =>
-  try {return [...val], true; }
-  catch (e)  { return false; }
+

isArrayLike

Checks if the provided argument is array-like (i.e. is iterable).

Use the spread operator (...) to check if the provided argument is iterable inside a try... catch block and the comma operator (,) to return the appropriate value.

const isArrayLike = val => {
+  try {
+    return [...val], true;
+  } catch (e) {
+    return false;
+  }
 };
 
isArrayLike(document.querySelectorAll('.className')); // true
 isArrayLike('abc'); // true
@@ -1001,25 +1021,4 @@ console.log(sdbm('age')); // 808122783
 yesNo('yes'); // true
 yesNo('No'); // false
 yesNo('Foo', true); // true
-

Uncategorized

reducedFilter

Filter an array of objects based on a condition while also filtering out unspecified keys.

Use Array.filter() to filter the array based on the predicate fn so that it returns the objects for which the condition returned a truthy value. On the filtered array, use Array.map() to return the new object using Array.reduce() to filter out the keys which were not supplied as the keys argument.

const reducedFilter = (data, keys, fn) =>
-  data.filter(fn).map(el =>
-    keys.reduce((acc, key) => {
-      acc[key] = el[key];
-      return acc;
-    }, {})
-  );
-
const data = [
-  {
-    id: 1,
-    name: 'john',
-    age: 24
-  },
-  {
-    id: 2,
-    name: 'mike',
-    age: 50
-  }
-];
-
-reducedFilter(data, ['id', 'name'], item => item.age > 24); // [{ id: 2, name: 'mike'}]
 

\ No newline at end of file diff --git a/snippets/isArrayLike.md b/snippets/isArrayLike.md index c281357bb..5417f1974 100644 --- a/snippets/isArrayLike.md +++ b/snippets/isArrayLike.md @@ -6,8 +6,11 @@ Use the spread operator (`...`) to check if the provided argument is iterable in ```js const isArrayLike = val => { - try {return [...val], true; } - catch (e) { return false; } + try { + return [...val], true; + } catch (e) { + return false; + } }; ```