Skip to content

Instantly share code, notes, and snippets.

View espretto's full-sized avatar

Marius espretto

  • Oldenburg
View GitHub Profile
@espretto
espretto / cssText.ts
Created November 8, 2022 09:35
ts: stringify css declaration
const kebabCase = (camelCased: string) =>
camelCased.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
export const toCssText = (styles: object) =>
Object.keys(styles)
// @ts-ignore: trust input is a CSSStyleDeclaration
.flatMap(prop => [kebabCase(prop), ":", styles[prop], ";"])
.join("");
@espretto
espretto / dataIsland.ts
Last active February 1, 2023 12:44
ts: drop json into html script tags
const htmlSpecialChars = [
["&", "&"],
[">", ">"],
["<", "&lt;"],
];
const encode = (data: string) =>
htmlSpecialChars.reduce(
(data, [char, entity]) => data.replaceAll(char, entity),
@espretto
espretto / onNode.js
Last active February 7, 2023 12:29
js: listen to dom changes
export function onNode(root, selector, handler) {
const target = root.querySelector(selector);
if (target) return handler(target);
new MutationObserver((records, observer) => {
const target = records
.filter(record => record.type === "childList")
.flatMap(record => record.addedNodes)
.find(node => node.nodeType === 1 && node.matches(selector));
@espretto
espretto / jsonMerge.ts
Last active July 27, 2023 18:29
ts: deep merge json serializable objects
function isObject(obj: any) {
return typeof obj === "object" && obj !== null;
}
function isPlainObject(obj: any) {
return Object.getPrototypeOf(obj).constructor === Object;
}
/**
* used to deep merge json serializable objects.