This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const htmlSpecialChars = [ | |
["&", "&"], | |
[">", ">"], | |
["<", "<"], | |
]; | |
const encode = (data: string) => | |
htmlSpecialChars.reduce( | |
(data, [char, entity]) => data.replaceAll(char, entity), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(""); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* magnifying glass for images | |
* @licence MIT | |
*/ | |
(function () { | |
/** avoid subpixel rendering */ | |
function px (n) { | |
return Math.floor(n) + 'px' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aiohttp import web | |
web.Application(middlewares=[cors_factory]) | |
ALLOWED_HEADERS = ','.join(( | |
'content-type', | |
'accept', | |
'origin', | |
'authorization', | |
'x-requested-with', |
NewerOlder