Skip to content

Instantly share code, notes, and snippets.

{
"rules": {
"color-no-invalid-hex": true,
"function-calc-no-unspaced-operator": true,
"function-linear-gradient-no-nonstandard-direction": true,
"function-max-empty-lines": 0,
"function-parentheses-space-inside": "never",
"function-url-quotes": "always",
"function-whitespace-after": "always",
"function-comma-space-after": "always",
@electerious
electerious / toClassString.js
Created January 21, 2017 15:48
Conditionally convert the keys of an object to a string of class names
const toClassString = (obj) => Object.keys(obj).filter((key) => obj[key]===true).join(' ').trim()
@electerious
electerious / clearfix.scss
Created January 26, 2017 16:00
SASS clearfix placeholder
%clearfix {
&::before,
&::after {
content: ' ';
display: table;
}
&::after {
clear: both;
@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 / 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 / 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)
}
@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 / 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 / single.js
Last active February 15, 2021 02:33
Function that continuously executes the function it has been most recently called with
const single = (max) => {
let id
let iterations
const loop = (_id, fn) => {
if (id!==_id) return
if (max!==undefined && iterations>=max) return