This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function compileTaggedTemplate(strings, values, callback) { | |
return strings.raw.reduce((acc, str, i) => acc + (callback(values[i - 1])) + str); | |
} | |
// Usage: | |
const tag = (strings, ...values) => compileTaggedTemplate(strings, values, (val) => val.toUpperCase()); | |
console.log(tag`Capitalize ${'all'} ${'substitutions'}`); //=> Capitalize ALL SUBSTITUTIONS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let raf = 0; | |
const animations = []; | |
function remove(animation) { | |
const index = animations.indexOf(animation); | |
if (index > -1) { | |
animations.splice(index, 1); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function forEachMatch(regex, str, callback) { | |
if (regex.global) { | |
regex.lastIndex = 0; | |
} | |
let i = -1, match; | |
while ((match = regex.exec(str))) { | |
callback(match, i++, str, regex); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function attempt(fn) { | |
return new Promise((resolve, reject) => { | |
try { | |
resolve(fn()); | |
} catch(e) { | |
reject(e); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getStyle(el, prop) { | |
const style = getComputedStyle(el); | |
return prop.includes('-') ? style.getPropertyValue(prop) : style[prop]; | |
} | |
function setStyle(el, prop, value) { | |
if (prop.includes('-')) { | |
el.style.setProperty(prop, value); | |
} else { | |
el.style[prop] = value; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Courtesy of: https://github.com/jorgebucaran/hyperapp | |
function createClass(value) { | |
if (typeof value === 'string') { | |
return value; | |
} | |
let output = ''; | |
if (Array.isArray(value)) { | |
for (let i = 0, len = value.length, tmp; i < len; i++) { | |
if ((tmp = createClass(value[i])) !== '') { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// An aggressive means to detect if the user's device is touch only | |
// Probably bad practice: https://css-tricks.com/interaction-media-features-and-their-potential-for-incorrect-assumptions/ | |
function isTouch() { | |
return matchMedia('(hover: none) and (pointer: coarse)').matches; | |
} | |
// A more passive approach is to detect if the user's device is merely touch-enabled | |
function hasTouch() { | |
return matchMedia('(any-pointer:coarse)').matches; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function compareDefault(a, b) { | |
return a - b; | |
} | |
function compareStrings(a, b) { | |
a = String(a).toUpperCase(); | |
b = String(b).toUpperCase(); | |
if (a > b) { | |
return 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getClass(obj) { | |
return {}.toString.call(obj).slice(8, -1); | |
} | |
// Usage: | |
getClass([]); //=> "Array" | |
getClass('foo'); //=> "String" | |
getClass(123); //=> "Number" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function port(fn) { | |
return Function.prototype.bind.call(Function.call, fn); | |
} | |
// Usage: | |
// port the `forEach` method of the Array prototype | |
const each = port([].forEach); | |
// Use standalone by passing the contextual object as the first argument | |
each(document.querySelectorAll('div'), (el) => console.log(el)); |
NewerOlder