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 / 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 / get-class.js
Last active March 29, 2024 06:40
Get the class of an object for type detection
function getClass(obj) {
return {}.toString.call(obj).slice(8, -1);
}
// Usage:
getClass([]); //=> "Array"
getClass('foo'); //=> "String"
getClass(123); //=> "Number"
@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 / port.js
Last active March 29, 2024 06:40
Port a method to be used standalone
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));
@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 / is-plain-object.js
Last active March 29, 2024 06:40
Check if an object is an object literal or bare object
function isPlainObject(value) {
if (!value || typeof value !== 'object') {
return false;
}
const prototype = Object.getPrototypeOf(value);
return prototype === null || prototype === Object.getPrototypeOf({});
}
// Usage:
@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) => {