Skip to content

Instantly share code, notes, and snippets.

@P4
P4 / Your language sucks.md
Last active May 18, 2023 09:29
Your programming language sucks

You appear to be advocating a new...

  • Functional
  • Imperative
  • Object-oriented
  • Procedural
  • Stack-based
  • "Multi-paradigm"
  • Lazy
  • Eager
interface Inner<T> {
value: T;
}
interface Numeric {
content: Inner<number>;
}
interface Text {
content: Inner<string>;
@P4
P4 / synology-reload.sh
Created May 8, 2019 21:26
acme.sh reloadcmd for Synology NAS; updates the certificate copies used by services with the renewed certificate, then reloads the service.
#!/bin/bash
# Let's Encrypt Certificate reload on Synology NAS
# Services configured through DSM to use a given certificate create their own copies of the certificate files.
# This script will update those copies after the original certificate is renewed.
#
# Install and configure acme.sh on the Synology NAS by following the tutorial:
# https://github.com/Neilpang/acme.sh/wiki/Synology-NAS-Guide
CERT_DIR=/usr/syno/etc/certificate
@P4
P4 / coerce-boolean.ts
Created April 7, 2020 19:26
coerceBooleanProperty as property decorator
function coerceBooleanProperty(prop: unknown): boolean {
return !!prop;
}
const CoerceBoolean: PropertyDecorator = (proto, prop) => {
const values = new WeakMap();
Object.defineProperty(proto, prop, {
get() {
return values.get(this)
},
@P4
P4 / user-script.util.js
Created July 10, 2020 14:56
UserScript utilities
/** returns a promise that resolves when element becomes available */
function waitForElement(selector, timeout=1000) {
return new Promise((resolve, reject) => {
let start, element;
requestAnimationFrame(function wait(timestamp) {
if (start == null) start = timestamp;
element = document.querySelector(selector);
if (element) {
resolve(element);
} else {
@P4
P4 / immutable.ts
Created June 17, 2021 16:53
Make a TypeScript type readonly, recursively
type AnyFunction = (...args: any[]) => any;
type ImmutablePrimitive = undefined | null | boolean | string | number | AnyFunction;
type Immutable<T> =
T extends ImmutablePrimitive ? T :
T extends Map<infer K, infer V> ? ReadonlyMap<Immutable<K>, Immutable<V>> :
T extends Set<infer E> ? ReadonlySet<Immutable<E>> :
{ readonly [P in keyof T]: Immutable<T[P]> }
@P4
P4 / konami.ts
Created July 15, 2021 16:54
Konami code in 7 lines of RxJS
import {find, fromEvent, scan} from 'rxjs';
const up = "ArrowUp", down = "ArrowDown", left = "ArrowLeft", right = "ArrowRight";
const sequence = [up, up, down, down, left, right, left, right, "b", "a"];
fromEvent<KeyboardEvent>(document, 'keyup').pipe(
scan((i, {key}) => (sequence[i] == key) ? i+1 : 0, 0),
find(i => i == sequence.length)
).subscribe(() => console.log("unlocked"))