diff --git a/README.md b/README.md index de995c6ce..934c385cd 100644 --- a/README.md +++ b/README.md @@ -670,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) })(); ``` @@ -2318,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); + }, []) : []; ``` @@ -4511,7 +4513,7 @@ const tomorrow = () => { Examples ```js -tomorrow(); // 2018-10-18 (if current date is 2018-10-18) +tomorrow(); // 2018-10-19 (if current date is 2018-10-18) ``` @@ -5504,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); diff --git a/dist/_30s.esm.js b/dist/_30s.esm.js index 40acf50c8..6e1057466 100644 --- a/dist/_30s.esm.js +++ b/dist/_30s.esm.js @@ -338,8 +338,8 @@ const extendHex = shortHex => 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,9 +985,9 @@ 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) => diff --git a/dist/_30s.js b/dist/_30s.js index a41ba2528..92e6c5619 100644 --- a/dist/_30s.js +++ b/dist/_30s.js @@ -344,8 +344,8 @@ 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); @@ -991,9 +991,9 @@ 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) => diff --git a/docs/adapter.html b/docs/adapter.html index 002459fc2..cdf450d31 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/date.html b/docs/date.html
index 3a87fe765..05dfc1a8a 100644
--- a/docs/date.html
+++ b/docs/date.html
@@ -156,5 +156,5 @@
   t.setDate(t.getDate() + 1);
   return t.toISOString().split('T')[0];
 };
-
tomorrow(); // 2018-10-18 (if current date is 2018-10-18)
+
tomorrow(); // 2018-10-19 (if current date is 2018-10-18)
 
\ No newline at end of file diff --git a/docs/index.html b/docs/index.html index 4197de212..586b16feb 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 94c8388f7..ea60b10ff 100644
--- a/docs/math.html
+++ b/docs/math.html
@@ -152,8 +152,8 @@ 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 =>
   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/snippet_data/snippetList.json b/snippet_data/snippetList.json
index 7a5cb02d6..ba1a98bb7 100644
--- a/snippet_data/snippetList.json
+++ b/snippet_data/snippetList.json
@@ -1162,7 +1162,7 @@
         "archived": false
       },
       "meta": {
-        "hash": "319e1a8fb41490965ee6e28db3e139e65c4ea5b7f43e332bc7216cd790e5d409"
+        "hash": "383ed61e69b8f63ae42d0746a1995057f4f65b4af6ca7778d8f1771144802acd"
       }
     },
     {
@@ -3391,7 +3391,7 @@
         "archived": false
       },
       "meta": {
-        "hash": "dcdf66e8d0eb4a1761c6b767b8cc350757087ae817ec371436faab0fff7c0051"
+        "hash": "0b04f5fe668888db0dc360535cd999669258019f0682eb6e4ad3a1164e408d13"
       }
     },
     {
@@ -3763,7 +3763,7 @@
         "archived": false
       },
       "meta": {
-        "hash": "2fd54c9fc1fb5b0a981df69501b518d5830ea77544d4d5290c7cc13745ca00ea"
+        "hash": "536833a64ce0c000b82327ed1bb9bcb82e35b237f75aefcca0e0d2def4e9cb63"
       }
     },
     {
@@ -4545,7 +4545,7 @@
         "archived": false
       },
       "meta": {
-        "hash": "ab65540435058b1e9dbe3f42bed312ad9095254065f8d711d4211e20fed371a7"
+        "hash": "8ffc5004edab7e91d1af653a07c820c82720dc81ee2233f2c43839c25f58acda"
       }
     },
     {
diff --git a/snippet_data/snippets.json b/snippet_data/snippets.json
index cb7accd99..c9f3d3c65 100644
--- a/snippet_data/snippets.json
+++ b/snippet_data/snippets.json
@@ -1703,7 +1703,7 @@
         "fileName": "factorial.md",
         "text": "Calculates the factorial of a number.\n\nUse recursion.\nIf `n` is less than or equal to `1`, return `1`.\nOtherwise, return the product of `n` and the factorial of `n - 1`.\nThrows an exception if `n` is a negative number.",
         "codeBlocks": {
-          "es6": "const factorial = n =>\n  n < 0\n    ? (() => {\n        throw new TypeError('Negative numbers are not allowed!');\n      })()\n    : n <= 1\n      ? 1\n      : n * factorial(n - 1);",
+          "es6": "const factorial = n =>\n  n < 0\n    ? (() => {\n      throw new TypeError('Negative numbers are not allowed!');\n    })()\n    : n <= 1\n      ? 1\n      : n * factorial(n - 1);",
           "es5": "var factorial = function factorial(n) {\n  return n < 0 ? function () {\n    throw new TypeError('Negative numbers are not allowed!');\n  }() : n <= 1 ? 1 : n * factorial(n - 1);\n};",
           "example": "factorial(6); // 720"
         },
@@ -1715,7 +1715,7 @@
       },
       "meta": {
         "archived": false,
-        "hash": "319e1a8fb41490965ee6e28db3e139e65c4ea5b7f43e332bc7216cd790e5d409"
+        "hash": "383ed61e69b8f63ae42d0746a1995057f4f65b4af6ca7778d8f1771144802acd"
       }
     },
     {
@@ -4983,7 +4983,7 @@
         "codeBlocks": {
           "es6": "const pipeAsyncFunctions = (...fns) => arg => fns.reduce((p, f) => p.then(f), Promise.resolve(arg));",
           "es5": "var pipeAsyncFunctions = function pipeAsyncFunctions() {\n  for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) {\n    fns[_key] = arguments[_key];\n  }\n\n  return function (arg) {\n    return fns.reduce(function (p, f) {\n      return p.then(f);\n    }, Promise.resolve(arg));\n  };\n};",
-          "example": "const sum = pipeAsyncFunctions(\n  x => x + 1,\n  x => new Promise(resolve => setTimeout(() => resolve(x + 2), 1000)),\n  x => x + 3,\n  async x => (await x) + 4\n);\n(async () => {\n  console.log(await sum(5)); // 15 (after one second)\n})();"
+          "example": "const sum = pipeAsyncFunctions(\n  x => x + 1,\n  x => new Promise(resolve => setTimeout(() => resolve(x + 2), 1000)),\n  x => x + 3,\n  async x => (await x) + 4\n);\n(async() => {\n  console.log(await sum(5)); // 15 (after one second)\n})();"
         },
         "tags": [
           "adapter",
@@ -4994,7 +4994,7 @@
       },
       "meta": {
         "archived": false,
-        "hash": "dcdf66e8d0eb4a1761c6b767b8cc350757087ae817ec371436faab0fff7c0051"
+        "hash": "0b04f5fe668888db0dc360535cd999669258019f0682eb6e4ad3a1164e408d13"
       }
     },
     {
@@ -5530,7 +5530,7 @@
         "fileName": "remove.md",
         "text": "Removes elements from an array for which the given function returns `false`.\n\nUse `Array.prototype.filter()` to find array elements that return truthy values and `Array.prototype.reduce()` to remove elements using `Array.prototype.splice()`.\nThe `func` is invoked with three arguments (`value, index, array`).",
         "codeBlocks": {
-          "es6": "const remove = (arr, func) =>\n  Array.isArray(arr)\n    ? arr.filter(func).reduce((acc, val) => {\n        arr.splice(arr.indexOf(val), 1);\n        return acc.concat(val);\n      }, [])\n    : [];",
+          "es6": "const remove = (arr, func) =>\n  Array.isArray(arr)\n    ? arr.filter(func).reduce((acc, val) => {\n      arr.splice(arr.indexOf(val), 1);\n      return acc.concat(val);\n    }, [])\n    : [];",
           "es5": "var remove = function remove(arr, func) {\n  return Array.isArray(arr) ? arr.filter(func).reduce(function (acc, val) {\n    arr.splice(arr.indexOf(val), 1);\n    return acc.concat(val);\n  }, []) : [];\n};",
           "example": "remove([1, 2, 3, 4], n => n % 2 === 0); // [2, 4]"
         },
@@ -5541,7 +5541,7 @@
       },
       "meta": {
         "archived": false,
-        "hash": "2fd54c9fc1fb5b0a981df69501b518d5830ea77544d4d5290c7cc13745ca00ea"
+        "hash": "536833a64ce0c000b82327ed1bb9bcb82e35b237f75aefcca0e0d2def4e9cb63"
       }
     },
     {
@@ -6685,7 +6685,7 @@
         "codeBlocks": {
           "es6": "const tomorrow = () => {\n  let t = new Date();\n  t.setDate(t.getDate() + 1);\n  return t.toISOString().split('T')[0];\n};",
           "es5": "var tomorrow = function tomorrow() {\n  var t = new Date();\n  t.setDate(t.getDate() + 1);\n  return t.toISOString().split('T')[0];\n};",
-          "example": "tomorrow(); // 2018-10-18 (if current date is 2018-10-18)"
+          "example": "tomorrow(); // 2018-10-19 (if current date is 2018-10-18)"
         },
         "tags": [
           "date",
@@ -6694,7 +6694,7 @@
       },
       "meta": {
         "archived": false,
-        "hash": "ab65540435058b1e9dbe3f42bed312ad9095254065f8d711d4211e20fed371a7"
+        "hash": "8ffc5004edab7e91d1af653a07c820c82720dc81ee2233f2c43839c25f58acda"
       }
     },
     {
diff --git a/snippets/factorial.md b/snippets/factorial.md
index 86ebe69b6..cf3acc9cc 100644
--- a/snippets/factorial.md
+++ b/snippets/factorial.md
@@ -11,8 +11,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);
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/snippets_archive/README.md b/snippets_archive/README.md
index 9ade00a86..6dae41d35 100644
--- a/snippets_archive/README.md
+++ b/snippets_archive/README.md
@@ -519,7 +519,7 @@ Calculates the [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_
 Calculates the number of changes (substitutions, deletions or additions) required to convert `string1` to `string2`. 
 Can also be used to compare two strings as shown in the second example.
 
-``` js
+```js
 const levenshteinDistance = (string1, string2) => {
   if (string1.length === 0) return string2.length;
   if (string2.length === 0) return string1.length;
diff --git a/test/_30s.js b/test/_30s.js
index 117a62318..887984cc8 100644
--- a/test/_30s.js
+++ b/test/_30s.js
@@ -341,8 +341,8 @@ const extendHex = shortHex =>
 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);
@@ -987,12 +987,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) =>
diff --git a/vscode_snippets/snippets.json b/vscode_snippets/snippets.json
index b3c3b3250..450284885 100644
--- a/vscode_snippets/snippets.json
+++ b/vscode_snippets/snippets.json
@@ -778,8 +778,8 @@
       "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);"
@@ -2490,9 +2490,9 @@
       "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);",
+      "    }, [])",
       "    : [];"
     ],
     "description": "Removes elements from an array for which the given function returns `false`.\n\nUse `Array.prototype.filter()` to find array elements that return truthy values and `Array.prototype.reduce()` to remove elements using `Array.prototype.splice()`.\nThe `func` is invoked with three arguments (`value, index, array`)"