From 92016c79980902def1c7f38232e41f02ac33c65b Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Thu, 25 Jan 2018 11:43:21 +0000 Subject: [PATCH] Travis build: 1415 --- README.md | 35 +++++++++++++++++++++++++++++++++++ docs/index.html | 12 ++++++++++-- snippets/reduceWhich.md | 8 ++++++-- tag_database | 1 + 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 16af20a52..e15f1990a 100644 --- a/README.md +++ b/README.md @@ -412,6 +412,15 @@ average(1, 2, 3); +### _Uncategorized_ + +
+View contents + +* [`reduceWhich`](#reducewhich) + +
+ --- ## 🔌 Adapter @@ -7327,6 +7336,32 @@ yesNo('Foo', true); // true
[⬆ Back to top](#table-of-contents) +--- + ## _Uncategorized_ + +### reduceWhich + +Returns the minimum/maximum value of an array, after applying the provided function to set comparing rule. + +Use `Array.reduce()` in combination with the `comparator` function to get the appropriate element in the array. +You can omit the second parameter, `comparator`, to use the default one that returns the minimum element in the array. + +```js +const reduceWhich = (arr, comparator = (a, b) => a - b) => + arr.reduce((a, b) => (comparator(a, b) >= 0 ? b : a)); +``` + +```js +reduceWhich([1, 3, 2]); // 1 +reduceWhich([1, 3, 2], (a, b) => b - a); // 3 +reduceWhich( + [{ name: 'Tom', age: 12 }, { name: 'Jack', age: 18 }, { name: 'Lucy', age: 9 }], + (a, b) => a.age - b.age +); // {name: "Lucy", age: 9} +``` + +
[⬆ back to top](#table-of-contents) + ## Collaborators diff --git a/docs/index.html b/docs/index.html index 35a8033f7..db73c3d82 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

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
 
const firstTwoMax = ary(Math.max, 2);
 [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]
 

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);
@@ -1706,4 +1706,12 @@ Logs: {
 yesNo('yes'); // true
 yesNo('No'); // false
 yesNo('Foo', true); // true
-
\ No newline at end of file +

Uncategorized

reduceWhich

Returns the minimum/maximum value of an array, after applying the provided function to set comparing rule.

Use Array.reduce() in combination with the comparator function to get the appropriate element in the array. You can omit the second parameter, comparator, to use the default one that returns the minimum element in the array.

const reduceWhich = (arr, comparator = (a, b) => a - b) =>
+  arr.reduce((a, b) => (comparator(a, b) >= 0 ? b : a));
+
reduceWhich([1, 3, 2]); // 1
+reduceWhich([1, 3, 2], (a, b) => b - a); // 3
+reduceWhich(
+  [{ name: 'Tom', age: 12 }, { name: 'Jack', age: 18 }, { name: 'Lucy', age: 9 }],
+  (a, b) => a.age - b.age
+); // {name: "Lucy", age: 9}
+
\ No newline at end of file diff --git a/snippets/reduceWhich.md b/snippets/reduceWhich.md index c59153c4e..49a188ce6 100644 --- a/snippets/reduceWhich.md +++ b/snippets/reduceWhich.md @@ -6,11 +6,15 @@ Use `Array.reduce()` in combination with the `comparator` function to get the ap You can omit the second parameter, `comparator`, to use the default one that returns the minimum element in the array. ```js -const reduceWhich = (arr, comparator = (a, b) => a - b) => arr.reduce((a, b) => comparator(a, b) >= 0 ? b : a); +const reduceWhich = (arr, comparator = (a, b) => a - b) => + arr.reduce((a, b) => (comparator(a, b) >= 0 ? b : a)); ``` ```js reduceWhich([1, 3, 2]); // 1 reduceWhich([1, 3, 2], (a, b) => b - a); // 3 -reduceWhich([{name: 'Tom', age: 12}, {name: 'Jack', age: 18}, {name: 'Lucy', age: 9}], (a, b) => a.age - b.age); // {name: "Lucy", age: 9} +reduceWhich( + [{ name: 'Tom', age: 12 }, { name: 'Jack', age: 18 }, { name: 'Lucy', age: 9 }], + (a, b) => a.age - b.age +); // {name: "Lucy", age: 9} ``` diff --git a/tag_database b/tag_database index 9bd036292..75adffe16 100644 --- a/tag_database +++ b/tag_database @@ -183,6 +183,7 @@ readFileLines:node,array,string redirect:browser,url reducedFilter:array reduceSuccessive:array,function +reduceWhich:uncategorized remove:array reverseString:string,array RGBToHex:utility