Skip to content

Instantly share code, notes, and snippets.

View pinkhominid's full-sized avatar

John Ericson pinkhominid

View GitHub Profile
@pinkhominid
pinkhominid / closestElementComposed.js
Created December 6, 2020 03:35
Element.closest() across shadow DOM boundaries
// https://stackoverflow.com/questions/54520554/custom-element-getrootnode-closest-function-crossing-multiple-parent-shadowd
export function closestElementComposed(selector, base = this) {
function __closestFrom(el) {
if (!el || el === document || el === window) return null;
let found = el.closest(selector);
return found ? found : __closestFrom(el.getRootNode().host);
}
return __closestFrom(base);
}
@pinkhominid
pinkhominid / index.js
Created September 24, 2019 10:15
Upload a file with node-fetch and form-data
const fs = require('fs');
const fetch = require('node-fetch');
const FormData = require('form-data');
const filePath = `path/to/file.ext`;
const form = new FormData();
const stats = fs.statSync(filePath);
const fileSizeInBytes = stats.size;
const fileStream = fs.createReadStream(filePath);
form.append('field-name', fileStream, { knownLength: fileSizeInBytes });
@pinkhominid
pinkhominid / node-env-hack.js
Last active October 8, 2022 11:13
Browser workaround for missing process.env.NODE_ENV
// Hack for immer esm (see https://github.com/immerjs/immer/issues/557)
// and others that suffer the same
window.process = { ...window.process, env: { ...window?.process?.env, NODE_ENV: 'production' } };
@pinkhominid
pinkhominid / my-component.js
Last active June 5, 2022 13:15
Web Component starter
customElements.define('my-component', class extends HTMLElement {
static get observedAttributes() {
return [
// attrs
];
}
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.render();
@pinkhominid
pinkhominid / clear-style.css
Last active April 2, 2021 03:00
Clear/reset default styles of common HTML elements
/* Anchors */
a.clear-style {
color: inherit;
text-decoration: none;
}
/* Buttons */
button.clear-style,
input[type=button].clear-style,
input[type=reset].clear-style {
@pinkhominid
pinkhominid / case-conversion-utils.js
Created March 3, 2021 14:21
Case conversion utils
// Derived from https://stackoverflow.com/a/52551910
export function camelize(str) {
return str.toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase());
}
export function pascalize(str) {
return (' ' + str).toLowerCase().replace(/[^a-zA-Z0-9]+(.)/g, (m, chr) => chr.toUpperCase());
}
@pinkhominid
pinkhominid / eventPathFindTargetMatch.js
Last active March 3, 2021 13:46
browser event path find target match
export function eventPathFindTargetMatch(event, selector) {
return event.composedPath().find(target => target.matches?.(selector));
}
@pinkhominid
pinkhominid / history-event.js
Last active February 12, 2021 20:13
Custom historychangestate event for web apps that use history.pushState or history.replaceState
export const HISTORY_STATE_CHANGE_EVENT = 'historystatechange';
history.pushState = changeState(history.pushState)
history.replaceState = changeState(history.replaceState)
window.addEventListener('popstate', fireStateChange)
setTimeout(fireStateChange);
function changeState(orig) {
return (...args) => {
@pinkhominid
pinkhominid / fetch-json-retry.js
Last active February 10, 2021 19:52
fetch json with retry
function fetchJSON(url, options, retries = 0) {
return fetch(url, { cache: 'no-store', ...options })
.then(res => {
if (res.ok) return res.json();
if (retries > 0) return fetchJSON(url, options, --retries);
throw res;
});
}
@pinkhominid
pinkhominid / package.json
Last active February 6, 2021 00:15
URL fun
{
"version": "0.3.0",
"name": "url-fun",
"description": "URL fun",
"main": "url-fun.js",
"author": "pinkhominid <pinkhominid@birdbomb.com>",
"license": "MIT",
"homepage": "https://gist.github.com/pinkhominid/742ff38688f7638d4eb795048b620e8e"
}