Merge remote-tracking branch 'origin/master'
This commit is contained in:
@ -9,12 +9,10 @@ Use `Array.prototype.reduce()` to map unique values to an object's keys, adding
|
||||
|
||||
```js
|
||||
const frequencies = arr =>
|
||||
arr.reduce(
|
||||
(a, v) => {
|
||||
a[v] = a[v] ? a[v] + 1 : 1;
|
||||
return a;
|
||||
}, {}
|
||||
);
|
||||
arr.reduce((a, v) => {
|
||||
a[v] = a[v] ? a[v] + 1 : 1;
|
||||
return a;
|
||||
}, {});
|
||||
```
|
||||
|
||||
```js
|
||||
|
||||
@ -17,8 +17,8 @@ const join = (arr, separator = ',', end = separator) =>
|
||||
i === arr.length - 2
|
||||
? acc + val + end
|
||||
: i === arr.length - 1
|
||||
? acc + val
|
||||
: acc + val + separator,
|
||||
? acc + val
|
||||
: acc + val + separator,
|
||||
''
|
||||
);
|
||||
```
|
||||
|
||||
@ -10,12 +10,12 @@ Use `Object.entries()` on the result in combination with `Array.prototype.reduce
|
||||
|
||||
```js
|
||||
const mostFrequent = arr =>
|
||||
Object.entries(arr.reduce(
|
||||
(a, v) => {
|
||||
Object.entries(
|
||||
arr.reduce((a, v) => {
|
||||
a[v] = a[v] ? a[v] + 1 : 1;
|
||||
return a;
|
||||
}, {}
|
||||
)).reduce((a, v) => v[1] >= a[1] ? v : a, [null, 0])[0];
|
||||
}, {})
|
||||
).reduce((a, v) => (v[1] >= a[1] ? v : a), [null, 0])[0];
|
||||
```
|
||||
|
||||
```js
|
||||
|
||||
@ -10,13 +10,14 @@ Determine the `symbol` to be either `?` or `&` based on the `length` of `querySt
|
||||
Return the `queryString` or an empty string when the `queryParameters` are falsy.
|
||||
|
||||
```js
|
||||
|
||||
const objectToQueryString = queryParameters => {
|
||||
return queryParameters
|
||||
? Object.entries(queryParameters).reduce((queryString, [key, val], index) => {
|
||||
const symbol = queryString.length === 0 ? '?' : '&';
|
||||
queryString += typeof val === 'string' ? `${symbol}${key}=${val}` : '';
|
||||
return queryString;
|
||||
}, '')
|
||||
const symbol = queryString.length === 0 ? '?' : '&';
|
||||
queryString += typeof val === 'string' ? `${symbol}${key}=${val}` : '';
|
||||
return queryString;
|
||||
}, '')
|
||||
: '';
|
||||
};
|
||||
```
|
||||
|
||||
@ -14,13 +14,14 @@ const pipeAsyncFunctions = (...fns) => arg => fns.reduce((p, f) => p.then(f), Pr
|
||||
```
|
||||
|
||||
```js
|
||||
|
||||
const sum = pipeAsyncFunctions(
|
||||
x => x + 1,
|
||||
x => new Promise(resolve => setTimeout(() => resolve(x + 2), 1000)),
|
||||
x => x + 3,
|
||||
async x => (await x) + 4
|
||||
);
|
||||
(async () => {
|
||||
(async() => {
|
||||
console.log(await sum(5)); // 15 (after one second)
|
||||
})();
|
||||
```
|
||||
@ -9,13 +9,12 @@ Use `Array.prototype.filter()` to find array elements that return truthy values
|
||||
The `func` is invoked with three arguments (`value, index, array`).
|
||||
|
||||
```js
|
||||
|
||||
const remove = (arr, func) =>
|
||||
Array.isArray(arr)
|
||||
? arr.filter(func).reduce((acc, val) => {
|
||||
arr.splice(arr.indexOf(val), 1);
|
||||
return acc.concat(val);
|
||||
}, [])
|
||||
arr.splice(arr.indexOf(val), 1);
|
||||
return acc.concat(val);
|
||||
}, [])
|
||||
: [];
|
||||
```
|
||||
|
||||
|
||||
@ -12,14 +12,15 @@ Use `size` of a [`Blob` object](https://developer.mozilla.org/en-US/docs/Web/API
|
||||
Split strings into array of characters with `split('')` and return its length.
|
||||
|
||||
```js
|
||||
|
||||
const size = val =>
|
||||
Array.isArray(val)
|
||||
? val.length
|
||||
: val && typeof val === 'object'
|
||||
? val.size || val.length || Object.keys(val).length
|
||||
: typeof val === 'string'
|
||||
? new Blob([val]).size
|
||||
: 0;
|
||||
? val.size || val.length || Object.keys(val).length
|
||||
: typeof val === 'string'
|
||||
? new Blob([val]).size
|
||||
: 0;
|
||||
```
|
||||
|
||||
```js
|
||||
|
||||
Reference in New Issue
Block a user