This file contains 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
// Imperative style | |
const createDivs = num => { | |
for (var i = 0; i < num; i++) | |
document.body.insertAdjacentHTML("beforeend", "<div></div>"); | |
}; | |
createDivs(10); | |
// Functional style: recursion | |
const createDivs = num => { |
This file contains 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
// ease-in | |
const easeInQuad = (t, b, c, d) => | |
c*(t/=d)*t + b; | |
const easeInCubic = (t, b, c, d) => | |
c*(t/=d)*t*t + b; | |
const easeInQuart = (t, b, c, d) => | |
c*(t/=d)*t*t*t + b; |
This file contains 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 toRGB = (() => { | |
const expand = hex => | |
hex.length < 7 ? hex.split("").reduce((a, b) => a + b + b) : hex; | |
const convert = hex => | |
hex.match(/[\d\w]{2}/g).map(val => parseInt(val, 16)); | |
return hex => { | |
const [r, g, b] = convert(expand(hex)); | |
return `rgb(${r}, ${g}, ${b})`; | |
}; | |
})(); |
This file contains 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 loop = (() => { | |
const recur = (callback, count, i=0) => { | |
if (i == count-1) return callback(i); | |
callback(i); | |
return recur(callback, count, i+1); | |
}; | |
return (callback, count) => { | |
if (count > 0) return recur(callback, count); | |
}; | |
})(); |
This file contains 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 cloneSet = (() => | |
new Set(new Set().add(1)).has(1) | |
? set => new Set(set) | |
: set => { | |
const clone = new Set(); | |
set.forEach(item => clone.add(item)); | |
return clone; | |
})(); |
This file contains 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 arr = [["foo", "bar"], ["hello", "world"]]; | |
// Create object from array of tuples | |
const obj = {}; arr.forEach(el => obj[el[0]] = el[1]); | |
const map = new Map(arr); | |
// Get object size | |
const objSize = Object.keys(obj).length; | |
const mapSize = map.size; |
This file contains 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 isNumber = x => | |
!Number.isNaN(parseInt(x)) && Number.isInteger(Math.trunc(x)); |
This file contains 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 set = new Set(["foo", "bar"]); | |
const iterate = iterator => { | |
const iteration = iterator.next(); | |
if (iteration.done) return; | |
console.log(iteration.value); | |
return iterate(iterator); | |
}; | |
iterate(set.values()); // => foo, bar |
This file contains 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 fib = (n, seq = [0, 1]) => | |
n - 2 ? fib(n - 1, [...seq, seq.slice(-2).reduce((a, b) => a + b)]) : seq; | |
fib(10); // 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 |
This file contains 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 toMap = (() => { | |
const convert = (obj, map = new Map(), keys = Object.keys(obj).values()) => { | |
const state = keys.next(); | |
if (state.done) return map; | |
const key = state.value; | |
map.set(key, obj[key]); | |
return convert(obj, map, keys); | |
}; | |
return obj => obj instanceof Map ? obj : convert(obj); | |
})(); |