const compose = (...fns) =>
fns.reduce((prevFn, nextFn) =>
(...args) => prevFn(nextFn(...args)),
(value) => value);
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var factorial = function(num) { | |
| return !num ? 1 : num *= factorial(num - 1); | |
| } | |
| // Shorter ES6 variant | |
| // const factorial = num => !num ? 1 : num *= factorial(num-1); | |
| alert(factorial(6)); // 720 | |
| // https://jsfiddle.net/2ary59t0/157/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const newArray = (size) => [...Array(size).keys()]; | |
| newArray(5); // [0, 1, 2, 3, 4] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const memoize = function(fn) { | |
| const cache = {}; | |
| return function(...args) { | |
| const key = args.join(); | |
| if (cache[key]) { | |
| console.log('from cache!'); | |
| return cache[key]; | |
| } | |
| return cache[key] = fn.apply(this, args); | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const highlightText = (str, match) => { | |
| if (!str) return null; | |
| if (!match) return str; | |
| const regex = new RegExp(match, 'mig'); | |
| return `>${str}<`.replace(/>[^<]+</gmi, (match) => match.replace(regex, '[$]')).slice(1, -1); | |
| }; | |
| console.log(highlightText('lorem 0 erty o', 'o')); | |
| console.log(highlightText('<p class="lorem">lorem ipsum dolor sit amet</p>', 'o')); | |
| // l[$]rem 0 erty [$] |
OlderNewer