diff --git a/.travis.yml b/.travis.yml index 665b33716..751243fbe 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,6 +14,7 @@ script: - npm run packager - npm run tester - npm run extractor +- npm run vscoder - npm run glossary:keymaker - npm run webber - npm run builder diff --git a/package.json b/package.json index 10736c25b..ddd6ee780 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "webber": "node ./scripts/web.js", "tester": "node ./scripts/tdd.js", "extractor": "node ./scripts/extract.js", + "vscoder": "node ./scripts/vscodegen.js", "packager": "node ./scripts/module.js", "localizer": "node ./scripts/localize.js", "test": "jest --verbose" diff --git a/scripts/extract.js b/scripts/extract.js index aab30cda0..0f6514fc0 100644 --- a/scripts/extract.js +++ b/scripts/extract.js @@ -90,5 +90,5 @@ let listingData = { fs.writeFileSync(path.join(OUTPUT_PATH, 'snippets.json'), JSON.stringify(completeData, null, 2)); fs.writeFileSync(path.join(OUTPUT_PATH, 'snippetList.json'), JSON.stringify(listingData, null, 2)); // Display messages and time -console.log(`${chalk.green('SUCCESS!')} snippets.json and snippetsArchive.json files generated!`); +console.log(`${chalk.green('SUCCESS!')} snippets.json and snippetList.json files generated!`); console.timeEnd('Extractor'); diff --git a/scripts/util.js b/scripts/util.js index cf25b1ad9..f3808a8dc 100644 --- a/scripts/util.js +++ b/scripts/util.js @@ -131,7 +131,7 @@ const getCodeBlocks = str => { results = results.map(v => v.replace(/```js([\s\S]*?)```/g, '$1').trim()); return { es6: results[0], - es5: babel.transformSync(results[0], { presets: ['@babel/preset-env'] }).code, + es5: babel.transformSync(results[0], { presets: ['@babel/preset-env'] }).code.replace('"use strict";\n\n',''), example: results[1] } }; diff --git a/scripts/vscodegen.js b/scripts/vscodegen.js new file mode 100644 index 000000000..3a89490cc --- /dev/null +++ b/scripts/vscodegen.js @@ -0,0 +1,33 @@ +/* + This is the VSCode generator script that generates the vscode_snippets/snippets.json file. + Run using `npm run vscoder`. +*/ +// Load modules +const fs = require('fs-extra'); +const path = require('path'); +const chalk = require('chalk'); +let snippetsData = require('../snippet_data/snippets.json'); +// Paths +const OUTPUT_PATH = './vscode_snippets'; +console.time('VSCoder'); +// Read and format data +let vscodeData = snippetsData.data.filter(v => !v.meta.archived ).reduce((acc,v) => { + acc[v.id] = { + prefix: `30s_${v.id}`, + body: v.attributes.codeBlocks.es6.replace(/\r/g,'').split('\n'), + description: v.attributes.text.slice(0, v.attributes.text.indexOf('\r\n\r\n')) + }; + return acc; +}, {}); +// Write data +fs.writeFileSync( + path.join(OUTPUT_PATH, 'snippets.json'), + JSON.stringify(vscodeData, null, 2) +); +// Display messages and time +console.log( + `${chalk.green( + 'SUCCESS!' + )} vscode_snippets/snippets.json file generated!` +); +console.timeEnd('VSCoder'); \ No newline at end of file diff --git a/snippet_data/snippetList.json b/snippet_data/snippetList.json index eb4bbef82..1941e4704 100644 --- a/snippet_data/snippetList.json +++ b/snippet_data/snippetList.json @@ -897,7 +897,7 @@ "archived": false }, "meta": { - "hash": "91e05fa632509c91fad4770339e0f8a4ffc5d0c3a90694f2fd59af2321cac9fb" + "hash": "9bbacd07c7a1d66b521b98d192f21964ab2cc33a6a62e18314d9dae5ec51a270" } }, { @@ -1118,7 +1118,7 @@ "archived": false }, "meta": { - "hash": "683bb22450b09dc717ef83b36ffcc6729a48891fcc47bab587b389f232597563" + "hash": "50b2980119026b898646420c2dc3cf2f394f5162f1a336cf64bc8d0244e54f26" } }, { @@ -3289,7 +3289,7 @@ "archived": false }, "meta": { - "hash": "12276cbe7dde6659e4ab67ed3fbe81ac57d1e7766b41dc879f946f226b7b76ab" + "hash": "6a7d303a9761c57b32f977b8068de4fd9bd9ea72167df268220d13250d1483e5" } }, { @@ -3661,7 +3661,7 @@ "archived": false }, "meta": { - "hash": "5aac190c69a2a7d42dd4f17caabe760270ac8c82b8b54b760d3d6b1de86362d7" + "hash": "7a8643d6d71023c773ccb63efe25e4c9fe0bdd976a8a8f76f6f6a0abb928eaf1" } }, { diff --git a/snippet_data/snippets.json b/snippet_data/snippets.json index 787b4a287..dd8fc2ab5 100644 --- a/snippet_data/snippets.json +++ b/snippet_data/snippets.json @@ -8,7 +8,7 @@ "text": "Returns `true` if the provided predicate function returns `true` for all elements in a collection, `false` otherwise.\r\n\r\nUse `Array.prototype.every()` to test if all elements in the collection return `true` based on `fn`.\r\nOmit the second argument, `fn`, to use `Boolean` as a default.", "codeBlocks": { "es6": "const all = (arr, fn = Boolean) => arr.every(fn);", - "es5": "\"use strict\";\n\nvar all = function all(arr) {\n var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Boolean;\n return arr.every(fn);\n};", + "es5": "var all = function all(arr) {\n var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Boolean;\n return arr.every(fn);\n};", "example": "all([4, 2, 3], x => x > 1); // true\r\nall([1, 2, 3]); // true" }, "tags": [ @@ -30,7 +30,7 @@ "text": "Check if all elements in an array are equal.\r\n\r\nUse `Array.prototype.every()` to check if all the elements of the array are the same as the first one.", "codeBlocks": { "es6": "const allEqual = arr => arr.every(val => val === arr[0]);", - "es5": "\"use strict\";\n\nvar allEqual = function allEqual(arr) {\n return arr.every(function (val) {\n return val === arr[0];\n });\n};", + "es5": "var allEqual = function allEqual(arr) {\n return arr.every(function (val) {\n return val === arr[0];\n });\n};", "example": "allEqual([1, 2, 3, 4, 5, 6]); // false\r\nallEqual([1, 1, 1, 1]); // true" }, "tags": [ @@ -52,7 +52,7 @@ "text": "Returns `true` if the provided predicate function returns `true` for at least one element in a collection, `false` otherwise.\r\n\r\nUse `Array.prototype.some()` to test if any elements in the collection return `true` based on `fn`.\r\nOmit the second argument, `fn`, to use `Boolean` as a default.", "codeBlocks": { "es6": "const any = (arr, fn = Boolean) => arr.some(fn);", - "es5": "\"use strict\";\n\nvar any = function any(arr) {\n var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Boolean;\n return arr.some(fn);\n};", + "es5": "var any = function any(arr) {\n var fn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Boolean;\n return arr.some(fn);\n};", "example": "any([0, 1, 2, 0], x => x >= 2); // true\r\nany([0, 0, 1, 0]); // true" }, "tags": [ @@ -74,7 +74,7 @@ "text": "Checks if two numbers are approximately equal to each other.\r\n\r\nUse `Math.abs()` to compare the absolute difference of the two values to `epsilon`.\r\nOmit the third parameter, `epsilon`, to use a default value of `0.001`.", "codeBlocks": { "es6": "const approximatelyEqual = (v1, v2, epsilon = 0.001) => Math.abs(v1 - v2) < epsilon;", - "es5": "\"use strict\";\n\nvar approximatelyEqual = function approximatelyEqual(v1, v2) {\n var epsilon = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0.001;\n return Math.abs(v1 - v2) < epsilon;\n};", + "es5": "var approximatelyEqual = function approximatelyEqual(v1, v2) {\n var epsilon = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0.001;\n return Math.abs(v1 - v2) < epsilon;\n};", "example": "approximatelyEqual(Math.PI / 2.0, 1.5708); // true" }, "tags": [ @@ -95,7 +95,7 @@ "text": "Converts a 2D array to a comma-separated values (CSV) string.\r\n\r\nUse `Array.prototype.map()` and `Array.prototype.join(delimiter)` to combine individual 1D arrays (rows) into strings.\r\nUse `Array.prototype.join('\\n')` to combine all rows into a CSV string, separating each row with a newline.\r\nOmit the second argument, `delimiter`, to use a default delimiter of `,`.", "codeBlocks": { "es6": "const arrayToCSV = (arr, delimiter = ',') =>\r\n arr.map(v => v.map(x => `\"${x}\"`).join(delimiter)).join('\\n');", - "es5": "\"use strict\";\n\nvar arrayToCSV = function arrayToCSV(arr) {\n var delimiter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ',';\n return arr.map(function (v) {\n return v.map(function (x) {\n return \"\\\"\".concat(x, \"\\\"\");\n }).join(delimiter);\n }).join('\\n');\n};", + "es5": "var arrayToCSV = function arrayToCSV(arr) {\n var delimiter = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ',';\n return arr.map(function (v) {\n return v.map(function (x) {\n return \"\\\"\".concat(x, \"\\\"\");\n }).join(delimiter);\n }).join('\\n');\n};", "example": "arrayToCSV([['a', 'b'], ['c', 'd']]); // '\"a\",\"b\"\\n\"c\",\"d\"'\r\narrayToCSV([['a', 'b'], ['c', 'd']], ';'); // '\"a\";\"b\"\\n\"c\";\"d\"'" }, "tags": [ @@ -118,7 +118,7 @@ "text": "Converts the given array elements into `
  • ` tags and appends them to the list of the given id.\r\n\r\nUse `Array.prototype.map()`, `document.querySelector()`, and an anonymous inner closure to create a list of html tags.", "codeBlocks": { "es6": "const arrayToHtmlList = (arr, listID) =>\r\n (el => (\r\n (el = document.querySelector('#' + listID)),\r\n (el.innerHTML += arr.map(item => `
  • ${item}
  • `).join(''))\r\n ))();", - "es5": "\"use strict\";\n\nvar arrayToHtmlList = function arrayToHtmlList(arr, listID) {\n return function (el) {\n return el = document.querySelector('#' + listID), el.innerHTML += arr.map(function (item) {\n return \"
  • \".concat(item, \"
  • \");\n }).join('');\n }();\n};", + "es5": "var arrayToHtmlList = function arrayToHtmlList(arr, listID) {\n return function (el) {\n return el = document.querySelector('#' + listID), el.innerHTML += arr.map(function (item) {\n return \"
  • \".concat(item, \"
  • \");\n }).join('');\n }();\n};", "example": "arrayToHtmlList(['item 1', 'item 2'], 'myListID');" }, "tags": [ @@ -140,7 +140,7 @@ "text": "Creates a function that accepts up to `n` arguments, ignoring any additional arguments.\r\n\r\nCall the provided function, `fn`, with up to `n` arguments, using `Array.prototype.slice(0,n)` and the spread operator (`...`).", "codeBlocks": { "es6": "const ary = (fn, n) => (...args) => fn(...args.slice(0, n));", - "es5": "\"use strict\";\n\nfunction _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }\n\nfunction _nonIterableSpread() { throw new TypeError(\"Invalid attempt to spread non-iterable instance\"); }\n\nfunction _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === \"[object Arguments]\") return Array.from(iter); }\n\nfunction _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }\n\nvar ary = function ary(fn, n) {\n return function () {\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n return fn.apply(void 0, _toConsumableArray(args.slice(0, n)));\n };\n};", + "es5": "function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }\n\nfunction _nonIterableSpread() { throw new TypeError(\"Invalid attempt to spread non-iterable instance\"); }\n\nfunction _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === \"[object Arguments]\") return Array.from(iter); }\n\nfunction _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }\n\nvar ary = function ary(fn, n) {\n return function () {\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n return fn.apply(void 0, _toConsumableArray(args.slice(0, n)));\n };\n};", "example": "const firstTwoMax = ary(Math.max, 2);\r\n[[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]" }, "tags": [ @@ -162,7 +162,7 @@ "text": "Decodes a string of data which has been encoded using base-64 encoding.\r\n\r\nCreate a `Buffer` for the given string with base-64 encoding and use `Buffer.toString('binary')` to return the decoded string.", "codeBlocks": { "es6": "const atob = str => Buffer.from(str, 'base64').toString('binary');", - "es5": "\"use strict\";\n\nvar atob = function atob(str) {\n return Buffer.from(str, 'base64').toString('binary');\n};", + "es5": "var atob = function atob(str) {\n return Buffer.from(str, 'base64').toString('binary');\n};", "example": "atob('Zm9vYmFy'); // 'foobar'" }, "tags": [ @@ -185,7 +185,7 @@ "text": "Attempts to invoke a function with the provided arguments, returning either the result or the caught error object.\r\n\r\nUse a `try... catch` block to return either the result of the function or an appropriate error.", "codeBlocks": { "es6": "const attempt = (fn, ...args) => {\r\n try {\r\n return fn(...args);\r\n } catch (e) {\r\n return e instanceof Error ? e : new Error(e);\r\n }\r\n};", - "es5": "\"use strict\";\n\nvar attempt = function attempt(fn) {\n try {\n for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {\n args[_key - 1] = arguments[_key];\n }\n\n return fn.apply(void 0, args);\n } catch (e) {\n return e instanceof Error ? e : new Error(e);\n }\n};", + "es5": "var attempt = function attempt(fn) {\n try {\n for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {\n args[_key - 1] = arguments[_key];\n }\n\n return fn.apply(void 0, args);\n } catch (e) {\n return e instanceof Error ? e : new Error(e);\n }\n};", "example": "var elements = attempt(function(selector) {\r\n return document.querySelectorAll(selector);\r\n}, '>_>');\r\nif (elements instanceof Error) elements = []; // elements = []" }, "tags": [ @@ -206,7 +206,7 @@ "text": "Returns the average of two or more numbers.\r\n\r\nUse `Array.prototype.reduce()` to add each value to an accumulator, initialized with a value of `0`, divide by the `length` of the array.", "codeBlocks": { "es6": "const average = (...nums) => nums.reduce((acc, val) => acc + val, 0) / nums.length;", - "es5": "\"use strict\";\n\nvar average = function average() {\n for (var _len = arguments.length, nums = new Array(_len), _key = 0; _key < _len; _key++) {\n nums[_key] = arguments[_key];\n }\n\n return nums.reduce(function (acc, val) {\n return acc + val;\n }, 0) / nums.length;\n};", + "es5": "var average = function average() {\n for (var _len = arguments.length, nums = new Array(_len), _key = 0; _key < _len; _key++) {\n nums[_key] = arguments[_key];\n }\n\n return nums.reduce(function (acc, val) {\n return acc + val;\n }, 0) / nums.length;\n};", "example": "average(...[1, 2, 3]); // 2\r\naverage(1, 2, 3); // 2" }, "tags": [ @@ -228,7 +228,7 @@ "text": "Returns the average of an array, after mapping each element to a value using the provided function.\r\n\r\nUse `Array.prototype.map()` to map each element to the value returned by `fn`, `Array.prototype.reduce()` to add each value to an accumulator, initialized with a value of `0`, divide by the `length` of the array.", "codeBlocks": { "es6": "const averageBy = (arr, fn) =>\r\n arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => acc + val, 0) /\r\n arr.length;", - "es5": "\"use strict\";\n\nvar averageBy = function averageBy(arr, fn) {\n return arr.map(typeof fn === 'function' ? fn : function (val) {\n return val[fn];\n }).reduce(function (acc, val) {\n return acc + val;\n }, 0) / arr.length;\n};", + "es5": "var averageBy = function averageBy(arr, fn) {\n return arr.map(typeof fn === 'function' ? fn : function (val) {\n return val[fn];\n }).reduce(function (acc, val) {\n return acc + val;\n }, 0) / arr.length;\n};", "example": "averageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], o => o.n); // 5\r\naverageBy([{ n: 4 }, { n: 2 }, { n: 8 }, { n: 6 }], 'n'); // 5" }, "tags": [ @@ -251,7 +251,7 @@ "text": "Splits values into two groups. If an element in `filter` is truthy, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group.\r\n\r\nUse `Array.prototype.reduce()` and `Array.prototype.push()` to add elements to groups, based on `filter`.", "codeBlocks": { "es6": "const bifurcate = (arr, filter) =>\r\n arr.reduce((acc, val, i) => (acc[filter[i] ? 0 : 1].push(val), acc), [[], []]);", - "es5": "\"use strict\";\n\nvar bifurcate = function bifurcate(arr, filter) {\n return arr.reduce(function (acc, val, i) {\n return acc[filter[i] ? 0 : 1].push(val), acc;\n }, [[], []]);\n};", + "es5": "var bifurcate = function bifurcate(arr, filter) {\n return arr.reduce(function (acc, val, i) {\n return acc[filter[i] ? 0 : 1].push(val), acc;\n }, [[], []]);\n};", "example": "bifurcate(['beep', 'boop', 'foo', 'bar'], [true, true, false, true]); // [ ['beep', 'boop', 'bar'], ['foo'] ]" }, "tags": [ @@ -272,7 +272,7 @@ "text": "Splits values into two groups according to a predicate function, which specifies which group an element in the input collection belongs to. If the predicate function returns a truthy value, the collection element belongs to the first group; otherwise, it belongs to the second group.\r\n\r\nUse `Array.prototype.reduce()` and `Array.prototype.push()` to add elements to groups, based on the value returned by `fn` for each element.", "codeBlocks": { "es6": "const bifurcateBy = (arr, fn) =>\r\n arr.reduce((acc, val, i) => (acc[fn(val, i) ? 0 : 1].push(val), acc), [[], []]);", - "es5": "\"use strict\";\n\nvar bifurcateBy = function bifurcateBy(arr, fn) {\n return arr.reduce(function (acc, val, i) {\n return acc[fn(val, i) ? 0 : 1].push(val), acc;\n }, [[], []]);\n};", + "es5": "var bifurcateBy = function bifurcateBy(arr, fn) {\n return arr.reduce(function (acc, val, i) {\n return acc[fn(val, i) ? 0 : 1].push(val), acc;\n }, [[], []]);\n};", "example": "bifurcateBy(['beep', 'boop', 'foo', 'bar'], x => x[0] === 'b'); // [ ['beep', 'boop', 'bar'], ['foo'] ]" }, "tags": [ @@ -294,7 +294,7 @@ "text": "Creates a function that invokes `fn` with a given context, optionally adding any additional supplied parameters to the beginning of the arguments.\r\n\r\nReturn a `function` that uses `Function.prototype.apply()` to apply the given `context` to `fn`.\r\nUse `Array.prototype.concat()` to prepend any additional supplied parameters to the arguments.", "codeBlocks": { "es6": "const bind = (fn, context, ...boundArgs) => (...args) => fn.apply(context, [...boundArgs, ...args]);", - "es5": "\"use strict\";\n\nvar bind = function bind(fn, context) {\n for (var _len = arguments.length, boundArgs = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {\n boundArgs[_key - 2] = arguments[_key];\n }\n\n return function () {\n for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {\n args[_key2] = arguments[_key2];\n }\n\n return fn.apply(context, boundArgs.concat(args));\n };\n};", + "es5": "var bind = function bind(fn, context) {\n for (var _len = arguments.length, boundArgs = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {\n boundArgs[_key - 2] = arguments[_key];\n }\n\n return function () {\n for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {\n args[_key2] = arguments[_key2];\n }\n\n return fn.apply(context, boundArgs.concat(args));\n };\n};", "example": "function greet(greeting, punctuation) {\r\n return greeting + ' ' + this.user + punctuation;\r\n}\r\nconst freddy = { user: 'fred' };\r\nconst freddyBound = bind(greet, freddy);\r\nconsole.log(freddyBound('hi', '!')); // 'hi fred!'" }, "tags": [ @@ -316,7 +316,7 @@ "text": "Binds methods of an object to the object itself, overwriting the existing method.\r\n\r\nUse `Array.prototype.forEach()` to return a `function` that uses `Function.prototype.apply()` to apply the given context (`obj`) to `fn` for each function specified.", "codeBlocks": { "es6": "const bindAll = (obj, ...fns) =>\r\n fns.forEach(\r\n fn => (\r\n (f = obj[fn]),\r\n (obj[fn] = function() {\r\n return f.apply(obj);\r\n })\r\n )\r\n );", - "es5": "\"use strict\";\n\nvar bindAll = function bindAll(obj) {\n for (var _len = arguments.length, fns = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {\n fns[_key - 1] = arguments[_key];\n }\n\n return fns.forEach(function (fn) {\n return f = obj[fn], obj[fn] = function () {\n return f.apply(obj);\n };\n });\n};", + "es5": "var bindAll = function bindAll(obj) {\n for (var _len = arguments.length, fns = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {\n fns[_key - 1] = arguments[_key];\n }\n\n return fns.forEach(function (fn) {\n return f = obj[fn], obj[fn] = function () {\n return f.apply(obj);\n };\n });\n};", "example": "var view = {\r\n label: 'docs',\r\n click: function() {\r\n console.log('clicked ' + this.label);\r\n }\r\n};\r\nbindAll(view, 'click');\r\njQuery(element).on('click', view.click); // Logs 'clicked docs' when clicked." }, "tags": [ @@ -338,7 +338,7 @@ "text": "Creates a function that invokes the method at a given key of an object, optionally adding any additional supplied parameters to the beginning of the arguments.\r\n\r\nReturn a `function` that uses `Function.prototype.apply()` to bind `context[fn]` to `context`.\r\nUse the spread operator (`...`) to prepend any additional supplied parameters to the arguments.", "codeBlocks": { "es6": "const bindKey = (context, fn, ...boundArgs) => (...args) =>\r\n context[fn].apply(context, [...boundArgs, ...args]);", - "es5": "\"use strict\";\n\nvar bindKey = function bindKey(context, fn) {\n for (var _len = arguments.length, boundArgs = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {\n boundArgs[_key - 2] = arguments[_key];\n }\n\n return function () {\n for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {\n args[_key2] = arguments[_key2];\n }\n\n return context[fn].apply(context, boundArgs.concat(args));\n };\n};", + "es5": "var bindKey = function bindKey(context, fn) {\n for (var _len = arguments.length, boundArgs = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {\n boundArgs[_key - 2] = arguments[_key];\n }\n\n return function () {\n for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {\n args[_key2] = arguments[_key2];\n }\n\n return context[fn].apply(context, boundArgs.concat(args));\n };\n};", "example": "const freddy = {\r\n user: 'fred',\r\n greet: function(greeting, punctuation) {\r\n return greeting + ' ' + this.user + punctuation;\r\n }\r\n};\r\nconst freddyBound = bindKey(freddy, 'greet');\r\nconsole.log(freddyBound('hi', '!')); // 'hi fred!'" }, "tags": [ @@ -360,7 +360,7 @@ "text": "Evaluates the binomial coefficient of two integers `n` and `k`.\r\n\r\nUse `Number.isNaN()` to check if any of the two values is `NaN`.\r\nCheck if `k` is less than `0`, greater than or equal to `n`, equal to `1` or `n - 1` and return the appropriate result.\r\nCheck if `n - k` is less than `k` and switch their values accordingly.\r\nLoop from `2` through `k` and calculate the binomial coefficient.\r\nUse `Math.round()` to account for rounding errors in the calculation.", "codeBlocks": { "es6": "const binomialCoefficient = (n, k) => {\r\n if (Number.isNaN(n) || Number.isNaN(k)) return NaN;\r\n if (k < 0 || k > n) return 0;\r\n if (k === 0 || k === n) return 1;\r\n if (k === 1 || k === n - 1) return n;\r\n if (n - k < k) k = n - k;\r\n let res = n;\r\n for (let j = 2; j <= k; j++) res *= (n - j + 1) / j;\r\n return Math.round(res);\r\n};", - "es5": "\"use strict\";\n\nvar binomialCoefficient = function binomialCoefficient(n, k) {\n if (Number.isNaN(n) || Number.isNaN(k)) return NaN;\n if (k < 0 || k > n) return 0;\n if (k === 0 || k === n) return 1;\n if (k === 1 || k === n - 1) return n;\n if (n - k < k) k = n - k;\n var res = n;\n\n for (var j = 2; j <= k; j++) {\n res *= (n - j + 1) / j;\n }\n\n return Math.round(res);\n};", + "es5": "var binomialCoefficient = function binomialCoefficient(n, k) {\n if (Number.isNaN(n) || Number.isNaN(k)) return NaN;\n if (k < 0 || k > n) return 0;\n if (k === 0 || k === n) return 1;\n if (k === 1 || k === n - 1) return n;\n if (n - k < k) k = n - k;\n var res = n;\n\n for (var j = 2; j <= k; j++) {\n res *= (n - j + 1) / j;\n }\n\n return Math.round(res);\n};", "example": "binomialCoefficient(8, 2); // 28" }, "tags": [ @@ -381,7 +381,7 @@ "text": "Returns `true` if the bottom of the page is visible, `false` otherwise.\r\n\r\nUse `scrollY`, `scrollHeight` and `clientHeight` to determine if the bottom of the page is visible.", "codeBlocks": { "es6": "const bottomVisible = () =>\r\n document.documentElement.clientHeight + window.scrollY >=\r\n (document.documentElement.scrollHeight || document.documentElement.clientHeight);", - "es5": "\"use strict\";\n\nvar bottomVisible = function bottomVisible() {\n return document.documentElement.clientHeight + window.scrollY >= (document.documentElement.scrollHeight || document.documentElement.clientHeight);\n};", + "es5": "var bottomVisible = function bottomVisible() {\n return document.documentElement.clientHeight + window.scrollY >= (document.documentElement.scrollHeight || document.documentElement.clientHeight);\n};", "example": "bottomVisible(); // true" }, "tags": [ @@ -402,7 +402,7 @@ "text": "Creates a base-64 encoded ASCII string from a String object in which each character in the string is treated as a byte of binary data.\r\n\r\nCreate a `Buffer` for the given string with binary encoding and use `Buffer.toString('base64')` to return the encoded string.", "codeBlocks": { "es6": "const btoa = str => Buffer.from(str, 'binary').toString('base64');", - "es5": "\"use strict\";\n\nvar btoa = function btoa(str) {\n return Buffer.from(str, 'binary').toString('base64');\n};", + "es5": "var btoa = function btoa(str) {\n return Buffer.from(str, 'binary').toString('base64');\n};", "example": "btoa('foobar'); // 'Zm9vYmFy'" }, "tags": [ @@ -425,7 +425,7 @@ "text": "Returns the length of a string in bytes.\r\n\r\nConvert a given string to a [`Blob` Object](https://developer.mozilla.org/en-US/docs/Web/API/Blob) and find its `size`.", "codeBlocks": { "es6": "const byteSize = str => new Blob([str]).size;", - "es5": "\"use strict\";\n\nvar byteSize = function byteSize(str) {\n return new Blob([str]).size;\n};", + "es5": "var byteSize = function byteSize(str) {\n return new Blob([str]).size;\n};", "example": "byteSize('😀'); // 4\r\nbyteSize('Hello World'); // 11" }, "tags": [ @@ -446,7 +446,7 @@ "text": "Given a key and a set of arguments, call them when given a context. Primarily useful in composition.\r\n\r\nUse a closure to call a stored key with stored arguments.", "codeBlocks": { "es6": "const call = (key, ...args) => context => context[key](...args);", - "es5": "\"use strict\";\n\nvar call = function call(key) {\n for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {\n args[_key - 1] = arguments[_key];\n }\n\n return function (context) {\n return context[key].apply(context, args);\n };\n};", + "es5": "var call = function call(key) {\n for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {\n args[_key - 1] = arguments[_key];\n }\n\n return function (context) {\n return context[key].apply(context, args);\n };\n};", "example": "Promise.resolve([1, 2, 3])\n .then(call('map', x => 2 * x))\n .then(console.log); // [ 2, 4, 6 ]\nconst map = call.bind(null, 'map');\nPromise.resolve([1, 2, 3])\n .then(map(x => 2 * x))\n .then(console.log); // [ 2, 4, 6 ]" }, "tags": [ @@ -468,7 +468,7 @@ "text": "Capitalizes the first letter of a string.\r\n\r\nUse array destructuring and `String.prototype.toUpperCase()` to capitalize first letter, `...rest` to get array of characters after first letter and then `Array.prototype.join('')` to make it a string again.\r\nOmit the `lowerRest` parameter to keep the rest of the string intact, or set it to `true` to convert to lowercase.", "codeBlocks": { "es6": "const capitalize = ([first, ...rest], lowerRest = false) =>\r\n first.toUpperCase() + (lowerRest ? rest.join('').toLowerCase() : rest.join(''));", - "es5": "\"use strict\";\n\nfunction _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _nonIterableRest(); }\n\nfunction _nonIterableRest() { throw new TypeError(\"Invalid attempt to destructure non-iterable instance\"); }\n\nfunction _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === \"[object Arguments]\") return Array.from(iter); }\n\nfunction _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }\n\nvar capitalize = function capitalize(_ref) {\n var _ref2 = _toArray(_ref),\n first = _ref2[0],\n rest = _ref2.slice(1);\n\n var lowerRest = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;\n return first.toUpperCase() + (lowerRest ? rest.join('').toLowerCase() : rest.join(''));\n};", + "es5": "function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _nonIterableRest(); }\n\nfunction _nonIterableRest() { throw new TypeError(\"Invalid attempt to destructure non-iterable instance\"); }\n\nfunction _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === \"[object Arguments]\") return Array.from(iter); }\n\nfunction _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }\n\nvar capitalize = function capitalize(_ref) {\n var _ref2 = _toArray(_ref),\n first = _ref2[0],\n rest = _ref2.slice(1);\n\n var lowerRest = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;\n return first.toUpperCase() + (lowerRest ? rest.join('').toLowerCase() : rest.join(''));\n};", "example": "capitalize('fooBar'); // 'FooBar'\r\ncapitalize('fooBar', true); // 'Foobar'" }, "tags": [ @@ -490,7 +490,7 @@ "text": "Capitalizes the first letter of every word in a string.\r\n\r\nUse `String.prototype.replace()` to match the first character of each word and `String.prototype.toUpperCase()` to capitalize it.", "codeBlocks": { "es6": "const capitalizeEveryWord = str => str.replace(/\\b[a-z]/g, char => char.toUpperCase());", - "es5": "\"use strict\";\n\nvar capitalizeEveryWord = function capitalizeEveryWord(str) {\n return str.replace(/\\b[a-z]/g, function (char) {\n return char.toUpperCase();\n });\n};", + "es5": "var capitalizeEveryWord = function capitalizeEveryWord(str) {\n return str.replace(/\\b[a-z]/g, function (char) {\n return char.toUpperCase();\n });\n};", "example": "capitalizeEveryWord('hello world!'); // 'Hello World!'" }, "tags": [ @@ -512,7 +512,7 @@ "text": "Casts the provided value as an array if it's not one.\r\n\r\nUse `Array.prototype.isArray()` to determine if `val` is an array and return it as-is or encapsulated in an array accordingly.", "codeBlocks": { "es6": "const castArray = val => (Array.isArray(val) ? val : [val]);", - "es5": "\"use strict\";\n\nvar castArray = function castArray(val) {\n return Array.isArray(val) ? val : [val];\n};", + "es5": "var castArray = function castArray(val) {\n return Array.isArray(val) ? val : [val];\n};", "example": "castArray('foo'); // ['foo']\r\ncastArray([1]); // [1]" }, "tags": [ @@ -535,7 +535,7 @@ "text": "Chains asynchronous functions.\r\n\r\nLoop through an array of functions containing asynchronous events, calling `next` when each asynchronous event has completed.", "codeBlocks": { "es6": "const chainAsync = fns => {\r\n let curr = 0;\r\n const next = () => fns[curr++](next);\r\n next();\r\n};", - "es5": "\"use strict\";\n\nvar chainAsync = function chainAsync(fns) {\n var curr = 0;\n\n var next = function next() {\n return fns[curr++](next);\n };\n\n next();\n};", + "es5": "var chainAsync = function chainAsync(fns) {\n var curr = 0;\n\n var next = function next() {\n return fns[curr++](next);\n };\n\n next();\n};", "example": "chainAsync([\r\n next => {\r\n console.log('0 seconds');\r\n setTimeout(next, 1000);\r\n },\r\n next => {\r\n console.log('1 second');\r\n }\r\n]);" }, "tags": [ @@ -556,7 +556,7 @@ "text": "Chunks an array into smaller arrays of a specified size.\r\n\r\nUse `Array.from()` to create a new array, that fits the number of chunks that will be produced.\r\nUse `Array.prototype.slice()` to map each element of the new array to a chunk the length of `size`.\r\nIf the original array can't be split evenly, the final chunk will contain the remaining elements.", "codeBlocks": { "es6": "const chunk = (arr, size) =>\r\n Array.from({ length: Math.ceil(arr.length / size) }, (v, i) =>\r\n arr.slice(i * size, i * size + size)\r\n );", - "es5": "\"use strict\";\n\nvar chunk = function chunk(arr, size) {\n return Array.from({\n length: Math.ceil(arr.length / size)\n }, function (v, i) {\n return arr.slice(i * size, i * size + size);\n });\n};", + "es5": "var chunk = function chunk(arr, size) {\n return Array.from({\n length: Math.ceil(arr.length / size)\n }, function (v, i) {\n return arr.slice(i * size, i * size + size);\n });\n};", "example": "chunk([1, 2, 3, 4, 5], 2); // [[1,2],[3,4],[5]]" }, "tags": [ @@ -577,7 +577,7 @@ "text": "Clamps `num` within the inclusive range specified by the boundary values `a` and `b`.\r\n\r\nIf `num` falls within the range, return `num`.\r\nOtherwise, return the nearest number in the range.", "codeBlocks": { "es6": "const clampNumber = (num, a, b) => Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b));", - "es5": "\"use strict\";\n\nvar clampNumber = function clampNumber(num, a, b) {\n return Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b));\n};", + "es5": "var clampNumber = function clampNumber(num, a, b) {\n return Math.max(Math.min(num, Math.max(a, b)), Math.min(a, b));\n};", "example": "clampNumber(2, 3, 5); // 3\r\nclampNumber(1, -1, -5); // -1" }, "tags": [ @@ -598,7 +598,7 @@ "text": "Clones a regular expression.\r\n\r\nUse `new RegExp()`, `RegExp.source` and `RegExp.flags` to clone the given regular expression.", "codeBlocks": { "es6": "const cloneRegExp = regExp => new RegExp(regExp.source, regExp.flags);", - "es5": "\"use strict\";\n\nvar cloneRegExp = function cloneRegExp(regExp) {\n return new RegExp(regExp.source, regExp.flags);\n};", + "es5": "var cloneRegExp = function cloneRegExp(regExp) {\n return new RegExp(regExp.source, regExp.flags);\n};", "example": "const regExp = /lorem ipsum/gi;\r\nconst regExp2 = cloneRegExp(regExp); // /lorem ipsum/gi" }, "tags": [ @@ -620,7 +620,7 @@ "text": "Returns the first non-null/undefined argument.\r\n\r\nUse `Array.prototype.find()` to return the first non `null`/`undefined` argument.", "codeBlocks": { "es6": "const coalesce = (...args) => args.find(_ => ![undefined, null].includes(_));", - "es5": "\"use strict\";\n\nvar coalesce = function coalesce() {\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n return args.find(function (_) {\n return ![undefined, null].includes(_);\n });\n};", + "es5": "var coalesce = function coalesce() {\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n return args.find(function (_) {\n return ![undefined, null].includes(_);\n });\n};", "example": "coalesce(null, undefined, '', NaN, 'Waldo'); // \"\"" }, "tags": [ @@ -641,7 +641,7 @@ "text": "Returns a customized coalesce function that returns the first argument that returns `true` from the provided argument validation function.\r\n\r\nUse `Array.prototype.find()` to return the first argument that returns `true` from the provided argument validation function.", "codeBlocks": { "es6": "const coalesceFactory = valid => (...args) => args.find(valid);", - "es5": "\"use strict\";\n\nvar coalesceFactory = function coalesceFactory(valid) {\n return function () {\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n return args.find(valid);\n };\n};", + "es5": "var coalesceFactory = function coalesceFactory(valid) {\n return function () {\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n return args.find(valid);\n };\n};", "example": "const customCoalesce = coalesceFactory(_ => ![null, undefined, '', NaN].includes(_));\r\ncustomCoalesce(undefined, null, NaN, '', 'Waldo'); // \"Waldo\"" }, "tags": [ @@ -662,7 +662,7 @@ "text": "Changes a function that accepts an array into a variadic function.\r\n\r\nGiven a function, return a closure that collects all inputs into an array-accepting function.", "codeBlocks": { "es6": "const collectInto = fn => (...args) => fn(args);", - "es5": "\"use strict\";\n\nvar collectInto = function collectInto(fn) {\n return function () {\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n return fn(args);\n };\n};", + "es5": "var collectInto = function collectInto(fn) {\n return function () {\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n return fn(args);\n };\n};", "example": "const Pall = collectInto(Promise.all.bind(Promise));\nlet p1 = Promise.resolve(1);\nlet p2 = Promise.resolve(2);\nlet p3 = new Promise(resolve => setTimeout(resolve, 2000, 3));\nPall(p1, p2, p3).then(console.log); // [1, 2, 3] (after about 2 seconds)" }, "tags": [ @@ -685,7 +685,7 @@ "text": "Add special characters to text to print in color in the console (combined with `console.log()`).\r\n\r\nUse template literals and special characters to add the appropriate color code to the string output.\r\nFor background colors, add a special character that resets the background color at the end of the string.", "codeBlocks": { "es6": "const colorize = (...args) => ({\r\n black: `\\x1b[30m${args.join(' ')}`,\r\n red: `\\x1b[31m${args.join(' ')}`,\r\n green: `\\x1b[32m${args.join(' ')}`,\r\n yellow: `\\x1b[33m${args.join(' ')}`,\r\n blue: `\\x1b[34m${args.join(' ')}`,\r\n magenta: `\\x1b[35m${args.join(' ')}`,\r\n cyan: `\\x1b[36m${args.join(' ')}`,\r\n white: `\\x1b[37m${args.join(' ')}`,\r\n bgBlack: `\\x1b[40m${args.join(' ')}\\x1b[0m`,\r\n bgRed: `\\x1b[41m${args.join(' ')}\\x1b[0m`,\r\n bgGreen: `\\x1b[42m${args.join(' ')}\\x1b[0m`,\r\n bgYellow: `\\x1b[43m${args.join(' ')}\\x1b[0m`,\r\n bgBlue: `\\x1b[44m${args.join(' ')}\\x1b[0m`,\r\n bgMagenta: `\\x1b[45m${args.join(' ')}\\x1b[0m`,\r\n bgCyan: `\\x1b[46m${args.join(' ')}\\x1b[0m`,\r\n bgWhite: `\\x1b[47m${args.join(' ')}\\x1b[0m`\r\n});", - "es5": "\"use strict\";\n\nvar colorize = function colorize() {\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n return {\n black: \"\\x1B[30m\".concat(args.join(' ')),\n red: \"\\x1B[31m\".concat(args.join(' ')),\n green: \"\\x1B[32m\".concat(args.join(' ')),\n yellow: \"\\x1B[33m\".concat(args.join(' ')),\n blue: \"\\x1B[34m\".concat(args.join(' ')),\n magenta: \"\\x1B[35m\".concat(args.join(' ')),\n cyan: \"\\x1B[36m\".concat(args.join(' ')),\n white: \"\\x1B[37m\".concat(args.join(' ')),\n bgBlack: \"\\x1B[40m\".concat(args.join(' '), \"\\x1B[0m\"),\n bgRed: \"\\x1B[41m\".concat(args.join(' '), \"\\x1B[0m\"),\n bgGreen: \"\\x1B[42m\".concat(args.join(' '), \"\\x1B[0m\"),\n bgYellow: \"\\x1B[43m\".concat(args.join(' '), \"\\x1B[0m\"),\n bgBlue: \"\\x1B[44m\".concat(args.join(' '), \"\\x1B[0m\"),\n bgMagenta: \"\\x1B[45m\".concat(args.join(' '), \"\\x1B[0m\"),\n bgCyan: \"\\x1B[46m\".concat(args.join(' '), \"\\x1B[0m\"),\n bgWhite: \"\\x1B[47m\".concat(args.join(' '), \"\\x1B[0m\")\n };\n};", + "es5": "var colorize = function colorize() {\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n return {\n black: \"\\x1B[30m\".concat(args.join(' ')),\n red: \"\\x1B[31m\".concat(args.join(' ')),\n green: \"\\x1B[32m\".concat(args.join(' ')),\n yellow: \"\\x1B[33m\".concat(args.join(' ')),\n blue: \"\\x1B[34m\".concat(args.join(' ')),\n magenta: \"\\x1B[35m\".concat(args.join(' ')),\n cyan: \"\\x1B[36m\".concat(args.join(' ')),\n white: \"\\x1B[37m\".concat(args.join(' ')),\n bgBlack: \"\\x1B[40m\".concat(args.join(' '), \"\\x1B[0m\"),\n bgRed: \"\\x1B[41m\".concat(args.join(' '), \"\\x1B[0m\"),\n bgGreen: \"\\x1B[42m\".concat(args.join(' '), \"\\x1B[0m\"),\n bgYellow: \"\\x1B[43m\".concat(args.join(' '), \"\\x1B[0m\"),\n bgBlue: \"\\x1B[44m\".concat(args.join(' '), \"\\x1B[0m\"),\n bgMagenta: \"\\x1B[45m\".concat(args.join(' '), \"\\x1B[0m\"),\n bgCyan: \"\\x1B[46m\".concat(args.join(' '), \"\\x1B[0m\"),\n bgWhite: \"\\x1B[47m\".concat(args.join(' '), \"\\x1B[0m\")\n };\n};", "example": "console.log(colorize('foo').red); // 'foo' (red letters)\r\nconsole.log(colorize('foo', 'bar').bgBlue); // 'foo bar' (blue background)\r\nconsole.log(colorize(colorize('foo').yellow, colorize('foo').green).bgWhite); // 'foo bar' (first word in yellow letters, second word in green letters, white background for both)" }, "tags": [ @@ -708,7 +708,7 @@ "text": "Removes falsey values from an array.\r\n\r\nUse `Array.prototype.filter()` to filter out falsey values (`false`, `null`, `0`, `\"\"`, `undefined`, and `NaN`).", "codeBlocks": { "es6": "const compact = arr => arr.filter(Boolean);", - "es5": "\"use strict\";\n\nvar compact = function compact(arr) {\n return arr.filter(Boolean);\n};", + "es5": "var compact = function compact(arr) {\n return arr.filter(Boolean);\n};", "example": "compact([0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34]); // [ 1, 2, 3, 'a', 's', 34 ]" }, "tags": [ @@ -729,7 +729,7 @@ "text": "Performs right-to-left function composition.\r\n\r\nUse `Array.prototype.reduce()` to perform right-to-left function composition.\r\nThe last (rightmost) function can accept one or more arguments; the remaining functions must be unary.", "codeBlocks": { "es6": "const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));", - "es5": "\"use strict\";\n\nvar compose = function compose() {\n for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) {\n fns[_key] = arguments[_key];\n }\n\n return fns.reduce(function (f, g) {\n return function () {\n return f(g.apply(void 0, arguments));\n };\n });\n};", + "es5": "var compose = function compose() {\n for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) {\n fns[_key] = arguments[_key];\n }\n\n return fns.reduce(function (f, g) {\n return function () {\n return f(g.apply(void 0, arguments));\n };\n });\n};", "example": "const add5 = x => x + 5;\r\nconst multiply = (x, y) => x * y;\r\nconst multiplyAndAdd5 = compose(\r\n add5,\r\n multiply\r\n);\r\nmultiplyAndAdd5(5, 2); // 15" }, "tags": [ @@ -750,7 +750,7 @@ "text": "Performs left-to-right function composition.\r\n\r\nUse `Array.prototype.reduce()` to perform left-to-right function composition.\r\nThe first (leftmost) function can accept one or more arguments; the remaining functions must be unary.", "codeBlocks": { "es6": "const composeRight = (...fns) => fns.reduce((f, g) => (...args) => g(f(...args)));", - "es5": "\"use strict\";\n\nvar composeRight = function composeRight() {\n for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) {\n fns[_key] = arguments[_key];\n }\n\n return fns.reduce(function (f, g) {\n return function () {\n return g(f.apply(void 0, arguments));\n };\n });\n};", + "es5": "var composeRight = function composeRight() {\n for (var _len = arguments.length, fns = new Array(_len), _key = 0; _key < _len; _key++) {\n fns[_key] = arguments[_key];\n }\n\n return fns.reduce(function (f, g) {\n return function () {\n return g(f.apply(void 0, arguments));\n };\n });\n};", "example": "const add = (x, y) => x + y;\r\nconst square = x => x * x;\r\nconst addAndSquare = composeRight(add, square);\r\naddAndSquare(1, 2); // 9" }, "tags": [ @@ -771,7 +771,7 @@ "text": "Accepts a converging function and a list of branching functions and returns a function that applies each branching function to the arguments and the results of the branching functions are passed as arguments to the converging function.\r\n\r\nUse `Array.prototype.map()` and `Function.prototype.apply()` to apply each function to the given arguments.\r\nUse the spread operator (`...`) to call `coverger` with the results of all other functions.", "codeBlocks": { "es6": "const converge = (converger, fns) => (...args) => converger(...fns.map(fn => fn.apply(null, args)));", - "es5": "\"use strict\";\n\nfunction _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }\n\nfunction _nonIterableSpread() { throw new TypeError(\"Invalid attempt to spread non-iterable instance\"); }\n\nfunction _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === \"[object Arguments]\") return Array.from(iter); }\n\nfunction _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }\n\nvar converge = function converge(converger, fns) {\n return function () {\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n return converger.apply(void 0, _toConsumableArray(fns.map(function (fn) {\n return fn.apply(null, args);\n })));\n };\n};", + "es5": "function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }\n\nfunction _nonIterableSpread() { throw new TypeError(\"Invalid attempt to spread non-iterable instance\"); }\n\nfunction _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === \"[object Arguments]\") return Array.from(iter); }\n\nfunction _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }\n\nvar converge = function converge(converger, fns) {\n return function () {\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n return converger.apply(void 0, _toConsumableArray(fns.map(function (fn) {\n return fn.apply(null, args);\n })));\n };\n};", "example": "const average = converge((a, b) => a / b, [\r\n arr => arr.reduce((a, v) => a + v, 0),\r\n arr => arr.length\r\n]);\r\naverage([1, 2, 3, 4, 5, 6, 7]); // 4" }, "tags": [ @@ -792,7 +792,7 @@ "text": "⚠️ **NOTICE:** The same functionality can be easily implemented by using the new asynchronous Clipboard API, which is still experimental but should be used in the future instead of this snippet. Find out more about it [here](https://github.com/w3c/clipboard-apis/blob/master/explainer.adoc#writing-to-the-clipboard).\r\n\r\nCopy a string to the clipboard. \r\nOnly works as a result of user action (i.e. inside a `click` event listener).\r\n\r\nCreate a new `