Skip to content

Instantly share code, notes, and snippets.

View bendc's full-sized avatar

Benjamin De Cock bendc

View GitHub Profile
@bendc
bendc / loops.js
Last active August 29, 2015 14:14
Create 10 divs
// 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 => {
@bendc
bendc / easing.js
Last active November 15, 2022 11:54
Penner easing equations as arrow functions
// 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;
@bendc
bendc / toRGB.js
Created May 11, 2015 20:16
Hexadecimal to RGB converter
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})`;
};
})();
@bendc
bendc / recursion.js
Created May 21, 2015 08:17
Functional loop
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);
};
})();
@bendc
bendc / cloneSet.js
Created July 31, 2015 00:05
Cross-browser cloneSet function
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;
})();
@bendc
bendc / hashmaps.js
Created August 7, 2015 19:30
Objects and maps compared
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;
@bendc
bendc / isNumber.js
Last active October 26, 2015 08:29
isNumber() checks if its argument represents a numeric value
const isNumber = x =>
!Number.isNaN(parseInt(x)) && Number.isInteger(Math.trunc(x));
@bendc
bendc / recurIterable.js
Created December 9, 2015 12:46
Iterate over an Iterator object using recursion
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
@bendc
bendc / fib.js
Created December 15, 2015 17:15
Fibonacci Series
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
@bendc
bendc / toMap.js
Created December 17, 2015 10:39
Object to Map, recursive-style
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);
})();