Travis build: 1019
This commit is contained in:
57
README.md
57
README.md
@ -333,6 +333,7 @@ _30s.average(1, 2, 3);
|
||||
* [`sumBy`](#sumby)
|
||||
* [`sumPower`](#sumpower)
|
||||
* [`toSafeInteger`](#tosafeinteger)
|
||||
* [`vectorDistance`](#vectordistance)
|
||||
|
||||
</details>
|
||||
|
||||
@ -669,13 +670,14 @@ const pipeAsyncFunctions = (...fns) => arg => fns.reduce((p, f) => p.then(f), Pr
|
||||
<summary>Examples</summary>
|
||||
|
||||
```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)
|
||||
})();
|
||||
```
|
||||
@ -2317,12 +2319,13 @@ 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);
|
||||
}, [])
|
||||
: [];
|
||||
```
|
||||
|
||||
@ -5503,8 +5506,8 @@ Throws an exception if `n` is a negative number.
|
||||
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);
|
||||
@ -5822,7 +5825,6 @@ const mapNumRange = (num, inMin, inMax, outMin, outMax) =>
|
||||
<summary>Examples</summary>
|
||||
|
||||
```js
|
||||
|
||||
mapNumRange(5, 0, 10, 0, 100); // 50
|
||||
```
|
||||
|
||||
@ -6257,6 +6259,33 @@ toSafeInteger(Infinity); // 9007199254740991
|
||||
|
||||
<br>[⬆ Back to top](#contents)
|
||||
|
||||
### vectorDistance
|
||||
|
||||
Returns the distance between two vectors.
|
||||
|
||||
Use `Array.prototype.reduce()`, `Math.pow()` and `Math.sqrt()` to calculate the Euclidean distance between two vectors.
|
||||
|
||||
```js
|
||||
const vectorDistance = (...coords) => {
|
||||
let pointLength = Math.trunc(coords.length / 2);
|
||||
let sum = coords
|
||||
.slice(0, pointLength)
|
||||
.reduce((acc, val, i) => acc + Math.pow(val - coords[pointLength + i], 2), 0);
|
||||
return Math.sqrt(sum);
|
||||
};
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Examples</summary>
|
||||
|
||||
```js
|
||||
vectorDistance(10, 0, 5, 20, 0, 10); // 11.180339887498949
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<br>[⬆ Back to top](#contents)
|
||||
|
||||
|
||||
---
|
||||
|
||||
@ -6782,11 +6811,11 @@ const deepMapKeys = (obj, f) =>
|
||||
? obj.map(val => deepMapKeys(val, f))
|
||||
: typeof obj === 'object'
|
||||
? Object.keys(obj).reduce((acc, current) => {
|
||||
const val = obj[current];
|
||||
acc[f(current)] =
|
||||
const val = obj[current];
|
||||
acc[f(current)] =
|
||||
val !== null && typeof val === 'object' ? deepMapKeys(val, f) : (acc[f(current)] = val);
|
||||
return acc;
|
||||
}, {})
|
||||
return acc;
|
||||
}, {})
|
||||
: obj;
|
||||
```
|
||||
|
||||
@ -6860,9 +6889,9 @@ 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);
|
||||
```
|
||||
|
||||
<details>
|
||||
|
||||
Reference in New Issue
Block a user