Skip to content

Instantly share code, notes, and snippets.

View electerious's full-sized avatar

Tobias Reich electerious

View GitHub Profile
<input autocapitalize="off" ...>
<input autocorrect="off" ...>
@electerious
electerious / hasClassNames.js
Created September 16, 2017 13:57
Checks if an element has any of multiple classes
const hasClassNames = (elem, classNames) => classNames.filter((className) => elem.classList.contains(className)).length>0
@electerious
electerious / createArray.js
Created September 16, 2017 13:54
Create an array with a specified length
const createArray = (length) => Array.apply(null, Array(length))
@electerious
electerious / repeat.js
Last active August 17, 2017 13:25
Repeat a function for a specified amount of times
const repeat = (fn, times) => {
fn()
--times && repeat(fn, times)
}
@electerious
electerious / fifo.js
Last active August 13, 2017 18:12
Array with a max number of items
const fifo = (length) => {
const arr = []
return (value) => {
if (value===undefined) return arr
if (arr.length>=length) arr.shift()
arr.push(value)
@electerious
electerious / randomColor.js
Created August 11, 2017 16:25
Generate a random hsl color
const randomColor = () => {
const random = (min, max) => Math.random() * (max - min) + min
const h = Math.floor(random(0, 360))
const s = Math.floor(random(50, 100))
const l = Math.floor(random(50, 100))
return `hsl(${ h }, ${ s }%, ${ l }%)`
@electerious
electerious / loop.js
Created July 27, 2017 12:24
Loop an async function with a delay
const loop = (fn, delay) => {
const next = () => setTimeout(
() => loop(fn, delay),
delay
)
fn(next)
}
@electerious
electerious / queue.js
Created May 12, 2017 18:13
Execute an array of functions with a delay between each execution
const queue = (query, delay, next) => {
if (query.length===0) return next()
query[0]()
const trimmed = query.slice(1)
setTimeout(() => exec(trimmed, delay, next), delay)
@electerious
electerious / mousePosition.js
Created February 22, 2017 16:16
Get the position of the mouse
const mousePosition = (() => {
let pos = {}
const position = (e) => ({
x: e.pageX,
y: e.pageY
})
document.addEventListener('mousemove', (e) => pos = position(e), false)