Skip to content

Instantly share code, notes, and snippets.

View electerious's full-sized avatar

Tobias Reich electerious

View GitHub Profile
@electerious
electerious / limit.js
Created November 13, 2015 10:45
Limit a number between a min and max value
const limit = (min, max, num) => Math.min(Math.max(num, min), max)
@electerious
electerious / stopEvent.js
Last active January 24, 2017 09:04
Stop event propagation and prevent the default
const stopEvent = (e = {}) => {
if (typeof e.stopPropagation==='function') e.stopPropagation()
if (typeof e.preventDefault==='function') e.preventDefault()
}
@electerious
electerious / html.js
Last active January 24, 2017 09:13
html = (literalSections, ...substs) => {
// Use raw literal sections: We don’t want
// backslashes (\n etc.) to be interpreted
let raw = literalSections.raw,
let result = ''
substs.forEach((subst, i) => {
// Retrieve the literal section preceding
const type = (data) => {
return Object.prototype.toString.call(data).replace(/^\[object (.+)\]$/, "$1").toLowerCase()
}
@electerious
electerious / each.js
Last active January 24, 2017 09:14
forEach for Objects, Arrays and NodeLists
const each = (data, fn) => {
if (data==null) return false
if ((data).constructor===Object) return Array.prototype.forEach.call(Object.keys(data), (key) => fn(data[key], key, data))
else return Array.prototype.forEach.call(data, (item, i) => fn(item, i, data))
}
@electerious
electerious / clearfix.scss
Created January 26, 2017 16:00
SASS clearfix placeholder
%clearfix {
&::before,
&::after {
content: ' ';
display: table;
}
&::after {
clear: both;
@electerious
electerious / duplicate.js
Created May 10, 2017 14:25
Duplicate array items based on an array
const duplicate = (arr, duplications) => arr.reduce((acc, val, i) => {
const duplication = duplications[i]
for (let i = 0; i<duplication; i++) acc.push(val)
return acc
}, [])
@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)
@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 / 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)
}