diff --git a/README.md b/README.md index 029e65dac..8d6d9000b 100644 --- a/README.md +++ b/README.md @@ -668,13 +668,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) })(); ``` @@ -2316,12 +2317,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); + }, []) : []; ``` @@ -5499,11 +5501,12 @@ 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); @@ -6753,16 +6756,17 @@ 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)) : 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; ``` @@ -6832,13 +6836,14 @@ 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); ```
diff --git a/docs/adapter.html b/docs/adapter.html index 10b4fce41..a2652d1eb 100644 --- a/docs/adapter.html +++ b/docs/adapter.html @@ -127,13 +127,14 @@ Object.assig const fn = overArgs((x, y) => [x, y], [square, double]); fn(9, 3); // [81, 6]

pipeAsyncFunctions

Performs left-to-right function composition for asynchronous functions.

Use Array.prototype.reduce() with the spread operator (...) to perform left-to-right function composition using Promise.then(). The functions can return a combination of: simple values, Promise's, or they can be defined as async ones returning through await. All functions must be unary.

const pipeAsyncFunctions = (...fns) => arg => fns.reduce((p, f) => p.then(f), Promise.resolve(arg));
-
const sum = pipeAsyncFunctions(
+
+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)
 })();
 

Recommended Resource - ES6: The Right Parts

Learn new ES6 JavaScript language features like arrow function, destructuring, generators & more to write cleaner and more productive, readable programs.

pipeFunctions

Performs left-to-right function composition.

Use Array.prototype.reduce() with the spread operator (...) to perform left-to-right function composition. The first (leftmost) function can accept one or more arguments; the remaining functions must be unary.

const pipeFunctions = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args)));
diff --git a/docs/index.html b/docs/index.html
index c95dab963..23615fa84 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -407,12 +407,13 @@
 

reject

Takes a predicate and array, like Array.prototype.filter(), but only keeps x if pred(x) === false.

const reject = (pred, array) => array.filter((...args) => !pred(...args));
 
reject(x => x % 2 === 0, [1, 2, 3, 4, 5]); // [1, 3, 5]
 reject(word => word.length > 4, ['Apple', 'Pear', 'Kiwi', 'Banana']); // ['Pear', 'Kiwi']
-

remove

Removes elements from an array for which the given function returns false.

Use Array.prototype.filter() to find array elements that return truthy values and Array.prototype.reduce() to remove elements using Array.prototype.splice(). The func is invoked with three arguments (value, index, array).

const remove = (arr, func) =>
+

remove

Removes elements from an array for which the given function returns false.

Use Array.prototype.filter() to find array elements that return truthy values and Array.prototype.reduce() to remove elements using Array.prototype.splice(). The func is invoked with three arguments (value, index, array).

+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);
+    }, [])
     : [];
 
remove([1, 2, 3, 4], n => n % 2 === 0); // [2, 4]
 

sample

Returns a random element from an array.

Use Math.random() to generate a random number, multiply it by length and round it off to the nearest whole number using Math.floor(). This method also works with strings.

const sample = arr => arr[Math.floor(Math.random() * arr.length)];
diff --git a/docs/math.html b/docs/math.html
index 9d264ace2..5fee58fa0 100644
--- a/docs/math.html
+++ b/docs/math.html
@@ -149,11 +149,12 @@ For teams, each rating can adjusted based on own team's average rating vs.
 average rating of opposing team, with the score being added to their
 own individual rating by supplying it as the third argument.
 */
-

factorial

Calculates the factorial of a number.

Use recursion. If n is less than or equal to 1, return 1. Otherwise, return the product of n and the factorial of n - 1. Throws an exception if n is a negative number.

const factorial = n =>
+

factorial

Calculates the factorial of a number.

Use recursion. If n is less than or equal to 1, return 1. Otherwise, return the product of n and the factorial of n - 1. 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);
diff --git a/docs/object.html b/docs/object.html
index 32ddb34ab..a668920c7 100644
--- a/docs/object.html
+++ b/docs/object.html
@@ -134,16 +134,17 @@
 
 o[0] = 3; // not allowed
 o[1][0] = 4; // not allowed as well
-

deepMapKeys

Deep maps an object keys.

Creates an object with the same values as the provided object and keys generated by running the provided function for each key.

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.

const deepMapKeys = (obj, f) =>
+

deepMapKeys

Deep maps an object keys.

Creates an object with the same values as the provided object and keys generated by running the provided function for each key.

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.

+const deepMapKeys = (obj, f) =>
   Array.isArray(obj)
     ? 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;
 
const obj = {
   foo: '1',
@@ -174,13 +175,14 @@ o[1
 

defaults

Assigns default values for all properties in an object that are undefined.

Use Object.assign() to create a new empty object and copy the original one to maintain key order, use Array.prototype.reverse() and the spread operator ... to combine the default values from left to right, finally use obj again to overwrite properties that originally had a value.

const defaults = (obj, ...defs) => Object.assign({}, obj, ...defs.reverse(), obj);
 
defaults({ a: 1 }, { b: 2 }, { b: 6 }, { a: 3 }); // { a: 1, b: 2 }
-

dig

Returns the target value in a nested JSON object, based on the given key.

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.

const dig = (obj, target) =>
+

dig

Returns the target value in a nested JSON object, based on the given key.

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.

+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);
 
const data = {
   level1: {
     level2: {
diff --git a/snippets/deepMapKeys.md b/snippets/deepMapKeys.md
index 970453bdc..e0223cb81 100644
--- a/snippets/deepMapKeys.md
+++ b/snippets/deepMapKeys.md
@@ -8,16 +8,17 @@ 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))
     : 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;
 ```
 
diff --git a/snippets/dig.md b/snippets/dig.md
index c97559833..a349ef2d3 100644
--- a/snippets/dig.md
+++ b/snippets/dig.md
@@ -6,13 +6,14 @@ 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
diff --git a/snippets/factorial.md b/snippets/factorial.md
index 86ebe69b6..2266247b4 100644
--- a/snippets/factorial.md
+++ b/snippets/factorial.md
@@ -8,11 +8,12 @@ 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);
diff --git a/snippets/pipeAsyncFunctions.md b/snippets/pipeAsyncFunctions.md
index 8c93add3b..61b850a39 100644
--- a/snippets/pipeAsyncFunctions.md
+++ b/snippets/pipeAsyncFunctions.md
@@ -11,13 +11,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)
 })();
 ```
diff --git a/snippets/remove.md b/snippets/remove.md
index a8c472774..1c457d2d9 100644
--- a/snippets/remove.md
+++ b/snippets/remove.md
@@ -6,12 +6,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);
+    }, [])
     : [];
 ```
 
diff --git a/test/_30s.js b/test/_30s.js
index 670d752fc..0441f4c90 100644
--- a/test/_30s.js
+++ b/test/_30s.js
@@ -235,16 +235,17 @@ const deepFreeze = obj =>
     prop =>
       !(obj[prop] instanceof Object) || Object.isFrozen(obj[prop]) ? null : deepFreeze(obj[prop])
   ) || Object.freeze(obj);
+
 const deepMapKeys = (obj, f) =>
   Array.isArray(obj)
     ? 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;
 const defaults = (obj, ...defs) => Object.assign({}, obj, ...defs.reverse(), obj);
 const defer = (fn, ...args) => setTimeout(fn, 1, ...args);
@@ -263,13 +264,14 @@ const differenceBy = (a, b, fn) => {
   return a.filter(x => !s.has(fn(x)));
 };
 const differenceWith = (arr, val, comp) => arr.filter(a => val.findIndex(b => comp(a, b)) === -1);
+
 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);
 const digitize = n => [...`${n}`].map(i => parseInt(i));
 const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0);
 const drop = (arr, n = 1) => arr.slice(n);
@@ -338,11 +340,12 @@ const extendHex = shortHex =>
     .split('')
     .map(x => x + x)
     .join('');
+
 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);
@@ -985,12 +988,13 @@ const reducedFilter = (data, keys, fn) =>
     }, {})
   );
 const reject = (pred, array) => array.filter((...args) => !pred(...args));
+
 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);
+    }, [])
     : [];
 const removeNonASCII = str => str.replace(/[^\x20-\x7E]/g, '');
 const renameKeys = (keysMap, obj) =>