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
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 [$] |
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 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
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 data = [1, [2, [3], 4], 5]; | |
const sum = (data) => { | |
return data.reduce((res, el) => { | |
if (el.length) { | |
return res + sum(el); | |
} | |
return res + el; | |
}, 0); | |
} |
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 a = [...Array(5000000).keys()]; | |
var chunk = (data, center) => { | |
const chunks = []; | |
while (data.length) | |
chunks.push(data.splice(0, center)); | |
return chunks; | |
} | |
var treeBuild = (array, tree = []) => { | |
if (!array.length) return tree; |
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
function Queue() { | |
var collection = []; | |
this.dequeue = () => collection.shift(); | |
this.enqueue = (el) => collection.push(el); | |
this.peek = () => collection[0]; | |
this.size = () => collection.length; | |
this.get = () => collection.slice(0); | |
} |
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 Stack = new function() { | |
let data = []; | |
this.pop = () => data.pop(); | |
this.push = (el) => data.push(el); | |
this.peek = () => data[data.length - 1]; | |
}; | |
Stack.push(1); |
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
// Promise | |
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); | |
// or pseudo Promise | |
const delay = (time) => ({ | |
then: (cb) => setTimeout(cb, time) | |
}); | |
// usage | |
delay(5000).then(…); |
NewerOlder