Skip to content

Instantly share code, notes, and snippets.

View espretto's full-sized avatar

Marius espretto

  • Oldenburg
View GitHub Profile
@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.
@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 / 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 / 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 / measureText.ts
Last active October 9, 2020 08:01
ts: measure text with offline canvas
const canvasContext = document
.createElement("canvas")
.getContext("2d") as CanvasRenderingContext2D;
/**
* measures text with the [canvas API](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/measureText)
* [avoiding expensive reflows](https://gist.github.com/Yukiniro/876826e1450b1f8cf755d2cea83cda65).
*/
export function measureText(text: string, font: string = "normal 16px serif") {
canvasContext.font = font;
@espretto
espretto / nginx.conf
Created May 19, 2020 21:03
nginx:reverse-proxy
# example nginx config
# ------------------------------------------------------------------------------
# serve static files from volume mounted at /public
# serve backend api expsosed by container "backend" on port 5000
# ------------------------------------------------------------------------------
user nginx;
worker_processes 1;
events {
@espretto
espretto / zoomer.js
Last active January 8, 2020 12:54
js: magnifying glass for images
/**
* magnifying glass for images
* @licence MIT
*/
(function () {
/** avoid subpixel rendering */
function px (n) {
return Math.floor(n) + 'px'
@espretto
espretto / slugify.js
Last active July 28, 2023 06:54
js: slugify
const latin1 = (
'A,A,A,A,Ae,A,AE,C,E,E,E,E,I,I,I,I,D,N,O,O,O,O,Oe,-,Oe,U,U,U,Ue,Y,Th,ss,' +
'a,a,a,a,ae,a,ae,c,e,e,e,e,i,i,i,i,d,n,o,o,o,o,oe,-,oe,u,u,u,ue,y,th,y'
).split(',');
function slugifyChar(chr) {
return latin1[chr.charCodeAt(0) - 0xC0];
}
function slugify(str) {
@espretto
espretto / bash-snippets.sh
Last active September 21, 2022 16:02
bash: snippets
#!/bin/bash
# ------------------------------------------------------------------------------
# strict mode
set -euo pipefail; IFS=$'\n\t'
# ------------------------------------------------------------------------------
# notes
#
# - `[` represents the command `test`, use `[[` builtin instead.
# - `-a` and `-o` are deprecated, use `&&` and `||` instead.
@espretto
espretto / asyncio_cors.py
Created April 28, 2017 18:45
cors middleware for asyncio
from aiohttp import web
web.Application(middlewares=[cors_factory])
ALLOWED_HEADERS = ','.join((
'content-type',
'accept',
'origin',
'authorization',
'x-requested-with',