Merge remote-tracking branch 'origin/master'
This commit is contained in:
@ -18,7 +18,7 @@ lengthIs4([1, 2, 3, 4]); // true
|
||||
lengthIs4(new Set([1, 2, 3, 4])); // false (Set uses Size, not length)
|
||||
|
||||
const session = { user: {} };
|
||||
const validUserSession = checkProps(u => u.active && !u.disabled, 'user');
|
||||
const validUserSession = checkProp(u => u.active && !u.disabled, 'user');
|
||||
|
||||
validUserSession(session); // false
|
||||
|
||||
|
||||
@ -11,7 +11,6 @@ Use `Object.assign()` and an empty object (`{}`) to create a shallow clone of th
|
||||
Use `Object.keys()` and `Array.prototype.forEach()` to determine which key-value pairs need to be deep cloned.
|
||||
|
||||
```js
|
||||
|
||||
const deepClone = obj => {
|
||||
if (obj === null) return null;
|
||||
let clone = Object.assign({}, obj);
|
||||
|
||||
@ -10,7 +10,6 @@ Use `Object.keys(obj)` to iterate over the object's keys.
|
||||
Use `Array.prototype.reduce()` to create a new object with the same values and mapped keys using `fn`.
|
||||
|
||||
```js
|
||||
|
||||
const deepMapKeys = (obj, f) =>
|
||||
Array.isArray(obj)
|
||||
? obj.map(val => deepMapKeys(val, f))
|
||||
|
||||
@ -9,14 +9,13 @@ Use the `in` operator to check if `target` exists in `obj`.
|
||||
If found, return the value of `obj[target]`, otherwise use `Object.values(obj)` and `Array.prototype.reduce()` to recursively call `dig` on each nested object until the first matching key/value pair is found.
|
||||
|
||||
```js
|
||||
|
||||
const dig = (obj, target) =>
|
||||
target in obj
|
||||
? obj[target]
|
||||
: Object.values(obj).reduce((acc, val) => {
|
||||
if (acc !== undefined) return acc;
|
||||
if (typeof val === 'object') return dig(val, target);
|
||||
}, undefined);
|
||||
if (acc !== undefined) return acc;
|
||||
if (typeof val === 'object') return dig(val, target);
|
||||
}, undefined);
|
||||
```
|
||||
|
||||
```js
|
||||
|
||||
@ -11,15 +11,14 @@ Otherwise, return the product of `n` and the factorial of `n - 1`.
|
||||
Throws an exception if `n` is a negative number.
|
||||
|
||||
```js
|
||||
|
||||
const factorial = n =>
|
||||
n < 0
|
||||
? (() => {
|
||||
throw new TypeError('Negative numbers are not allowed!');
|
||||
})()
|
||||
throw new TypeError('Negative numbers are not allowed!');
|
||||
})()
|
||||
: n <= 1
|
||||
? 1
|
||||
: n * factorial(n - 1);
|
||||
? 1
|
||||
: n * factorial(n - 1);
|
||||
```
|
||||
|
||||
```js
|
||||
|
||||
@ -8,15 +8,14 @@ Converts an integer to a suffixed string, adding `am` or `pm` based on its value
|
||||
Use the modulo operator (`%`) and conditional checks to transform an integer to a stringified 12-hour format with meridiem suffix.
|
||||
|
||||
```js
|
||||
|
||||
const getMeridiemSuffixOfInteger = num =>
|
||||
num === 0 || num === 24
|
||||
? 12 + 'am'
|
||||
: num === 12
|
||||
? 12 + 'pm'
|
||||
: num < 12
|
||||
? (num % 12) + 'am'
|
||||
: (num % 12) + 'pm';
|
||||
? 12 + 'pm'
|
||||
: num < 12
|
||||
? (num % 12) + 'am'
|
||||
: (num % 12) + 'pm';
|
||||
```
|
||||
|
||||
```js
|
||||
|
||||
@ -15,15 +15,10 @@ const weightedSample = (arr, weights) => {
|
||||
let roll = Math.random();
|
||||
return arr[
|
||||
weights
|
||||
.reduce((acc, w, i) =>
|
||||
i === 0 ? [w] : [...acc, acc[acc.length - 1] + w],
|
||||
[]
|
||||
)
|
||||
.findIndex((v, i, s) =>
|
||||
roll >= (i === 0 ? 0 : s[i - 1]) && roll < v
|
||||
)
|
||||
.reduce((acc, w, i) => (i === 0 ? [w] : [...acc, acc[acc.length - 1] + w]), [])
|
||||
.findIndex((v, i, s) => roll >= (i === 0 ? 0 : s[i - 1]) && roll < v)
|
||||
];
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
```js
|
||||
|
||||
Reference in New Issue
Block a user