diff --git a/README.md b/README.md index cf3774c2e..aa89db221 100644 --- a/README.md +++ b/README.md @@ -333,6 +333,7 @@ _30s.average(1, 2, 3); * [`sumBy`](#sumby) * [`sumPower`](#sumpower) * [`toSafeInteger`](#tosafeinteger) +* [`vectorDistance`](#vectordistance) @@ -669,13 +670,14 @@ const pipeAsyncFunctions = (...fns) => arg => fns.reduce((p, f) => p.then(f), Pr Examples ```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) => Examples ```js - mapNumRange(5, 0, 10, 0, 100); // 50 ``` @@ -6257,6 +6259,33 @@ toSafeInteger(Infinity); // 9007199254740991
[⬆ 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); +}; +``` + +
+Examples + +```js +vectorDistance(10, 0, 5, 20, 0, 10); // 11.180339887498949 +``` + +
+ +
[⬆ 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); ```
diff --git a/docs/about.html b/docs/about.html index 297a8a76a..abed5ddcb 100644 --- a/docs/about.html +++ b/docs/about.html @@ -358,6 +358,7 @@
  • sumBy
  • sumPower
  • toSafeInteger
  • +
  • vectorDistance
  • Node

    Node