Skip to content

Instantly share code, notes, and snippets.

View bendc's full-sized avatar

Benjamin De Cock bendc

View GitHub Profile
@bendc
bendc / each.js
Created August 27, 2014 13:23
each() for NodeLists
function each(nodelist, callback) {
var i = -1,
l = nodelist.length
while (++i < l)
callback.call(nodelist.item(i), i)
}
// Usage:
var divs = document.querySelectorAll("div")
each(divs, function(index) {
set nocompatible
set encoding=utf-8 nobomb
" enable syntax highlighting and line numbers
syntax on
set number
" color scheme
let g:solarized_termtrans=1
set background=dark
@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 / 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 / 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 / scrollRoot.js
Created January 5, 2016 09:20
Determines the scrolling element
const scrollRoot = (() => {
if ("scrollingElement" in document)
return document.scrollingElement;
const initial = document.documentElement.scrollTop;
document.documentElement.scrollTop = initial + 1;
const updated = document.documentElement.scrollTop;
document.documentElement.scrollTop = initial;
return updated > initial
@bendc
bendc / isObject.js
Created January 6, 2016 08:50
Check if something is an object
const isObject = obj => Object(obj) === obj;
@bendc
bendc / getPathBoundingBox.js
Created June 9, 2016 18:24
SVG path bounding box
const getPathBoundingBox = (() => {
const toPolygon = (path, points = 50) => {
const polygon = document.createElementNS("http://www.w3.org/2000/svg", "polygon");
const total = path.getTotalLength();
const step = total / points;
const getPoints = (dist = 0, arr = []) => {
const {x, y} = path.getPointAtLength(dist);
arr.push(`${x},${y}`);
const next = step + dist;
@bendc
bendc / duplicate.js
Created June 14, 2016 20:22
Deep object copy
const duplicate = obj => JSON.parse(JSON.stringify(obj));