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 delegate(context, type, selector, fn) { | |
context.addEventListener(type, (e) => fn(e, e.target.closest(selector))); | |
} |
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 hashmap(...props) { | |
const map = Object.create(null, { | |
[Symbol.iterator]: { | |
enumerable: false, | |
writable: false, | |
configurable: false, | |
value: () => ({ | |
items: Object.entries(map), | |
next: function next() { | |
return { |
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 logError(error, fatal = false) { | |
const description = typeof error === 'string' ? error : error.message; | |
gtag('event', 'exception', { | |
description, | |
fatal | |
}); | |
} |
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 stacktrace(error = new Error()) { | |
if (Error.captureStackTrace) { | |
Error.captureStackTrace(error, stacktrace); | |
return error.stack; | |
} | |
return error.stack; | |
} |
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)); |
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 inherit(subclass, superclass) { | |
subclass.prototype = Object.create(superclass.prototype, { | |
constructor: { | |
value: subclass | |
} | |
}); | |
} |
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 isArrayLike(value) { | |
return value | |
&& typeof value === 'object' | |
&& typeof value.length === 'number' | |
&& value.length >= 0 | |
&& value.length % 1 === 0; | |
} |
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 isPlainObject(value) { | |
if (!value || typeof value !== 'object') { | |
return false; | |
} | |
const prototype = Object.getPrototypeOf(value); | |
return prototype === null || prototype === Object.getPrototypeOf({}); | |
} | |
// Usage: |
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 query(selector, root = document) { | |
let context = [root]; | |
const tokens = selector.trim().split(/\s*(>|\+(?!\d)|~(?!=)|\s)\s*/).filter(n => !!n); | |
while (tokens.length && context.length) { | |
let token = tokens.shift(), combinator; | |
if (/>|\+(?!\d)|~(?!=)|\s/.test(token)) { | |
combinator = token; | |
token = tokens.shift(); | |
} | |
context = context.reduce((nodes, el) => { |
OlderNewer