Skip to content

Instantly share code, notes, and snippets.

View bendc's full-sized avatar

Benjamin De Cock bendc

View GitHub Profile
@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 "#":
@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 / 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 / innerHTMLToDom.js
Last active February 24, 2017 13:24
Create DOM node from innerHTML-like string
const createNode = html =>
new Range().createContextualFragment(html).firstElementChild;
@bendc
bendc / functional-inheritance.js
Last active July 9, 2016 16:03
Functional inheritance
function car() {
return {
start: function() {
console.log("Engine on.")
},
accelerate: function() {
console.log("Let's go!")
}
}
}
@bendc
bendc / functional-utils.js
Last active September 15, 2023 12:12
A set of pure ES2015 functions aimed to make functional JavaScript more idiomatic.
// array utils
// =================================================================================================
const combine = (...arrays) => [].concat(...arrays);
const compact = arr => arr.filter(Boolean);
const contains = (() => Array.prototype.includes
? (arr, value) => arr.includes(value)
: (arr, value) => arr.some(el => el === value)
@bendc
bendc / nodelist-iteration.js
Created January 13, 2015 14:39
ES6: Iterating over a NodeList
var elements = document.querySelectorAll("div"),
callback = (el) => { console.log(el); };
// Spread operator
[...elements].forEach(callback);
// Array.from()
Array.from(elements).forEach(callback);
// for...of statement
@bendc
bendc / random-color.js
Last active February 7, 2024 03:19
Generate nice random colors
const randomColor = (() => {
"use strict";
const randomInt = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
};
return () => {
var h = randomInt(0, 360);
var s = randomInt(42, 98);
@bendc
bendc / alphabet.js
Created January 30, 2015 21:38
Array of uppercase and lowercase letters
const letters = (() => {
const caps = [...Array(26)].map((val, i) => String.fromCharCode(i + 65));
return caps.concat(caps.map(letter => letter.toLowerCase()));
})();