Skip to content

Instantly share code, notes, and snippets.

View bendc's full-sized avatar

Benjamin De Cock bendc

View GitHub Profile
@bendc
bendc / randomInterval.js
Created March 9, 2017 21:55
rAF-based random interval
const randomInterval = (() => {
const random = (min, max) => Math.random() * (max - min) + min;
return (callback, min, max) => {
const time = {
start: performance.now(),
total: random(min, max)
};
const tick = now => {
if (time.total <= now - time.start) {
time.start = now;
@bendc
bendc / todo-list-events.js
Created January 6, 2017 11:23
Web Components: todo-list-events
const store = (() => {
let state;
return todos => {
if (todos) {
state = todos;
render("todo-list");
}
return state;
};
})();
@bendc
bendc / merge.js
Created July 21, 2016 14:15
Deep merge of JSON-like objects
const merge = (() => {
const duplicate = object => JSON.parse(JSON.stringify(object));
return (...objects) => Object.assign(...objects.map(duplicate));
})();
@bendc
bendc / simulate-typing.js
Created September 1, 2017 08:57
Fake typing animation
const trackTime = timing => {
const now = performance.now();
if (!timing.startTime) timing.startTime = now;
const elapsed = now - timing.startTime;
const {duration} = timing;
if (duration != null && duration <= elapsed) timing.startTime = null;
return elapsed;
};
const delay = (callback, duration) => {
@bendc
bendc / removeDuplicates.js
Last active February 5, 2021 16:57
Remove duplicates from array
function removeDuplicates(arr) {
var clean = []
var cleanLen = 0
var arrLen = arr.length
for (var i = 0; i < arrLen; i++) {
var el = arr[i]
var duplicate = false
for (var j = 0; j < cleanLen; j++) {
@bendc
bendc / svgo.json
Created August 30, 2016 07:05
Sketch's SVGO Compressor settings
{
"comment": "This is the settings file for the SVGO Compressor Plugin. For more info, please check <https://github.com/BohemianCoding/svgo-compressor>",
"pretty": false,
"indent": 2,
"plugins": [
{
"name": "cleanupAttrs"
},
{
"name": "cleanupEnableBackground"
@bendc
bendc / raf-delay.js
Created August 28, 2017 13:19
rAF tutorial: better setTimeout
const delay = (callback, duration) => {
const tick = () =>
getProgress(time) < 1 ? requestAnimationFrame(tick) : callback();
const time = {
duration,
id: requestAnimationFrame(tick)
};
};
@bendc
bendc / iterable.js
Created January 26, 2016 09:09
Make objects iterable
const iterable = function* (obj) {
yield* Object.keys(obj).map(key => [key, obj[key]]);
};
@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 / fastSelect.js
Created August 26, 2014 21:57
A fast method to select DOM elements
function fastSelect(selector) {
if (selector == "body") {
return document.body
}
else if (selector == "head") {
return document.head
}
else if (/^[\#.]?[\w-]+$/.test(selector)) {
switch (selector[0]) {
case "#":