Skip to content

Instantly share code, notes, and snippets.

View ryanmorr's full-sized avatar

Ryan Morr ryanmorr

View GitHub Profile
@ryanmorr
ryanmorr / delegate.js
Created July 26, 2016 17:56
Event delegation in 3 lines
function delegate(context, type, selector, fn) {
context.addEventListener(type, (e) => fn(e, e.target.closest(selector)));
}
@ryanmorr
ryanmorr / hashmap.js
Created August 3, 2016 04:59
A superior alternative to object literals for basic hash maps
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 {
@ryanmorr
ryanmorr / inherit.js
Created August 3, 2016 05:44
Inherit the prototype of a function
function inherit(subclass, superclass) {
subclass.prototype = Object.create(superclass.prototype, {
constructor: {
value: subclass
}
});
}
@ryanmorr
ryanmorr / is-array-like.js
Last active August 4, 2016 23:54
Check if an object has the characteristics of an array-like object
function isArrayLike(value) {
return value
&& typeof value === 'object'
&& typeof value.length === 'number'
&& value.length >= 0
&& value.length % 1 === 0;
}
@ryanmorr
ryanmorr / debug.css
Created March 7, 2018 19:10
Debug layouts
* {
outline: 1px solid #fff !important;
background-color: rgba(0,2,54,.1) !important;
}
@ryanmorr
ryanmorr / query.js
Last active April 29, 2018 20:18
A simple, context-aware selector engine
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) => {
@ryanmorr
ryanmorr / stacktrace.js
Last active December 28, 2023 17:33
Create a stacktrace
function stacktrace(error = new Error()) {
if (Error.captureStackTrace) {
Error.captureStackTrace(error, stacktrace);
return error.stack;
}
return error.stack;
}
@ryanmorr
ryanmorr / log-error.js
Last active December 28, 2023 17:50
Log an error with Google Analytics 4
function logError(error, fatal = false) {
const description = typeof error === 'string' ? error : error.message;
gtag('event', 'exception', {
description,
fatal
});
}
@ryanmorr
ryanmorr / proxy-detection.js
Created January 15, 2024 02:57
The only way to detect proxies without mutating the object
// Courtesy of: https://www.reddit.com/r/javascript/comments/4wrdxd/comment/d69j04s/
const proxies = new WeakSet();
function createProxy(target, handler) {
const proxy = new Proxy(target, handler);
proxies.add(proxy);
return proxy;
}
function isProxy(obj) {
@ryanmorr
ryanmorr / for-each-child.js
Last active January 15, 2024 06:49
Fastest way to iterate through each child node of an element and call a function
// Traversing with firstChild/nextSibling is much faster than childNodes: https://jsperf.app/nextsibling-vs-childnodes
function forEachChild(element, callback) {
let node = element.firstChild;
while (node) {
callback(node);
node = node.nextSibling;
}
}