Skip to content

Instantly share code, notes, and snippets.

View electerious's full-sized avatar

Tobias Reich electerious

View GitHub Profile
@electerious
electerious / counter.js
Last active January 24, 2017 09:04
Count up and down between two numbers
const counter = (min, max, initial) => {
let index = initial - min
let length = max - min + 1
return (modifier = 0) => {
index = (index + modifier) % length
if (index>=0) index = 0 + index
@electerious
electerious / toggler.js
Last active January 24, 2017 09:04
Toggle between true and false
const toggler = (initState) => {
initState = initState!==false
let state = initState
return {
get : () => state,
reset : () => state = initState,
toggle : () => state = !state,
@electerious
electerious / select.js
Last active January 24, 2017 09:04
Select multiple elements and receive an array
const select = (query, elem = null) => {
elem = (elem==null ? document : elem)
let elems = elem.querySelectorAll(query)
if (elems==null) return []
else return Array.prototype.slice.call(elems, 0)
}
@electerious
electerious / debounce.js
Last active December 12, 2018 08:18 — forked from jasonwyatt/debouncer.js
How to **correctly** debounce an event that will be triggered many times with identical arguments.
const debounce = function(fn, duration) {
let timeout = null
return (...args) => {
clearTimeout(timeout)
timeout = setTimeout(() => fn(...args), duration)
const hasTouchscreen = () => {
const mobileDevice = (/Android|iPhone|iPad|iPod/i).test(navigator.userAgent || navigator.vendor || window.opera)
const onTouchEnd = ('ontouchend' in document.documentElement)
return (mobileDevice && onTouchEnd)
}
@electerious
electerious / nicetry.js
Last active July 30, 2016 14:47
Try to execute a function and discard any error that occurs
const nicetry = (fn) => {
try { return fn() }
catch (e) {}
}
const decodeHTML = (str) => {
const elem = document.createElement('div')
elem.innerHTML = str
return (elem.childNodes.length===0 ? '' : elem.childNodes[0].nodeValue)
}
@electerious
electerious / hasLocalStorage.js
Last active July 21, 2016 15:33
Detect if browser supports localStorage
const hasLocalStorage = () => {
const tmp = 'tmp'
try {
localStorage.setItem(tmp, tmp)
localStorage.removeItem(tmp)
return true
@electerious
electerious / Caddyfile
Created August 20, 2016 18:15
Most complete list of mime types in the correct format for the Caddy mime directive
mime {
.atom application/atom+xml
.json application/json
.map application/json
.topojson application/json
.jsonld application/ld+json
.rss application/rss+xml
.geojson application/vnd.geo+json
.rdf application/xml
.xml application/xml
@electerious
electerious / formatNumber.js
Created September 30, 2016 10:47
Format a number
const formatNumber = (num, delimiter = ',') => num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, delimiter)