Travis build: 1185

This commit is contained in:
30secondsofcode
2018-01-11 11:47:13 +00:00
parent bcc05a9a6e
commit b2105b096e
6 changed files with 71 additions and 19 deletions

View File

@ -89,6 +89,7 @@ average(1, 2, 3);
* [`chunk`](#chunk) * [`chunk`](#chunk)
* [`compact`](#compact) * [`compact`](#compact)
* [`countBy`](#countby)
* [`countOccurrences`](#countoccurrences) * [`countOccurrences`](#countoccurrences)
* [`deepFlatten`](#deepflatten) * [`deepFlatten`](#deepflatten)
* [`difference`](#difference) * [`difference`](#difference)
@ -561,6 +562,34 @@ compact([0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34]); // [ 1, 2, 3, 'a'
<br>[⬆ Back to top](#table-of-contents) <br>[⬆ Back to top](#table-of-contents)
### countBy
Groups the elements of an array based on the given function and returns the count of elements in each group.
Use `Array.map()` to map the values of an array to a function or property name.
Use `Array.reduce()` to create an object, where the keys are produced from the mapped results.
```js
const countBy = (arr, fn) =>
arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val, i) => {
acc[val] = (acc[val] || 0) + 1;
return acc;
}, {});
```
<details>
<summary>Examples</summary>
```js
countBy([6.1, 4.2, 6.3], Math.floor); // {4: 1, 6: 2}
countBy(['one', 'two', 'three'], 'length'); // {3: 2, 5: 1}
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### countOccurrences ### countOccurrences
Counts the occurrences of a value in an array. Counts the occurrences of a value in an array.
@ -833,8 +862,8 @@ Use `Array.map()` to map the values of an array to a function or property name.
Use `Array.reduce()` to create an object, where the keys are produced from the mapped results. Use `Array.reduce()` to create an object, where the keys are produced from the mapped results.
```js ```js
const groupBy = (arr, func) => const groupBy = (arr, fn) =>
arr.map(typeof func === 'function' ? func : val => val[func]).reduce((acc, val, i) => { arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val, i) => {
acc[val] = (acc[val] || []).concat(arr[i]); acc[val] = (acc[val] || []).concat(arr[i]);
return acc; return acc;
}, {}); }, {});
@ -2854,7 +2883,9 @@ Returns the average of an array, after mapping each element to a value using the
Use `Array.map()` to map each element to the value returned by `fn`, `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`, divide by the `length` of the array. Use `Array.map()` to map each element to the value returned by `fn`, `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`, divide by the `length` of the array.
```js ```js
const averageBy = (arr, fn) => arr.map(fn).reduce((acc, val) => acc + val, 0) / arr.length; const averageBy = (arr, fn) =>
arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => acc + val, 0) /
arr.length;
``` ```
<details> <details>
@ -2862,6 +2893,7 @@ const averageBy = (arr, fn) => arr.map(fn).reduce((acc, val) => acc + val, 0) /
```js ```js
averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 5 averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 5
averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n'); // 5
``` ```
</details> </details>
@ -3302,7 +3334,7 @@ Returns the maximum value of an array, after mapping each element to a value usi
Use `Array.map()` to map each element to the value returned by `fn`, `Math.max()` to get the maximum value. Use `Array.map()` to map each element to the value returned by `fn`, `Math.max()` to get the maximum value.
```js ```js
const maxBy = (arr, fn) => Math.max(...arr.map(fn)); const maxBy = (arr, fn) => Math.max(...arr.map(typeof fn === 'function' ? fn : val => val[fn]));
``` ```
<details> <details>
@ -3310,6 +3342,7 @@ const maxBy = (arr, fn) => Math.max(...arr.map(fn));
```js ```js
maxBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 8 maxBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 8
maxBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n'); // 8
``` ```
</details> </details>
@ -3351,7 +3384,7 @@ Returns the minimum value of an array, after mapping each element to a value usi
Use `Array.map()` to map each element to the value returned by `fn`, `Math.min()` to get the maximum value. Use `Array.map()` to map each element to the value returned by `fn`, `Math.min()` to get the maximum value.
```js ```js
const minBy = (arr, fn) => Math.min(...arr.map(fn)); const minBy = (arr, fn) => Math.min(...arr.map(typeof fn === 'function' ? fn : val => val[fn]));
``` ```
<details> <details>
@ -3359,6 +3392,7 @@ const minBy = (arr, fn) => Math.min(...arr.map(fn));
```js ```js
minBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 8 minBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 8
minBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n'); // 8
``` ```
</details> </details>
@ -3595,7 +3629,8 @@ Returns the sum of an array, after mapping each element to a value using the pro
Use `Array.map()` to map each element to the value returned by `fn`, `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`. Use `Array.map()` to map each element to the value returned by `fn`, `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`.
```js ```js
const sumBy = (arr, fn) => arr.map(fn).reduce((acc, val) => acc + val, 0); const sumBy = (arr, fn) =>
arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => acc + val, 0);
``` ```
<details> <details>
@ -3603,6 +3638,7 @@ const sumBy = (arr, fn) => arr.map(fn).reduce((acc, val) => acc + val, 0);
```js ```js
sumBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 20 sumBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 20
sumBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n'); // 20
``` ```
</details> </details>
@ -5302,6 +5338,7 @@ const httpPost = (url, callback, data = null, err = console.error) => {
const newPost = { const newPost = {
"userId": 1, "userId": 1,
"id": 1337, "id": 1337,

File diff suppressed because one or more lines are too long

View File

@ -6,7 +6,8 @@ Use `Array.map()` to map each element to the value returned by `fn`, `Array.redu
```js ```js
const averageBy = (arr, fn) => const averageBy = (arr, fn) =>
arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => acc + val, 0) / arr.length; arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => acc + val, 0) /
arr.length;
``` ```
```js ```js

View File

@ -31,6 +31,7 @@ const httpPost = (url, callback, data = null, err = console.error) => {
const newPost = { const newPost = {
"userId": 1, "userId": 1,
"id": 1337, "id": 1337,

View File

@ -5,11 +5,10 @@ Returns the maximum value of an array, after mapping each element to a value usi
Use `Array.map()` to map each element to the value returned by `fn`, `Math.max()` to get the maximum value. Use `Array.map()` to map each element to the value returned by `fn`, `Math.max()` to get the maximum value.
```js ```js
const maxBy = (arr, fn) => const maxBy = (arr, fn) => Math.max(...arr.map(typeof fn === 'function' ? fn : val => val[fn]));
Math.max(...arr.map(typeof fn === 'function' ? fn : val => val[fn]));
``` ```
```js ```js
maxBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 8 maxBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 8
maxBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }],'n'); // 8 maxBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n'); // 8
``` ```

View File

@ -5,8 +5,7 @@ Returns the minimum value of an array, after mapping each element to a value usi
Use `Array.map()` to map each element to the value returned by `fn`, `Math.min()` to get the maximum value. Use `Array.map()` to map each element to the value returned by `fn`, `Math.min()` to get the maximum value.
```js ```js
const minBy = (arr, fn) => const minBy = (arr, fn) => Math.min(...arr.map(typeof fn === 'function' ? fn : val => val[fn]));
Math.min(...arr.map(typeof fn === 'function' ? fn : val => val[fn]));
``` ```
```js ```js