Skip to content

Instantly share code, notes, and snippets.

View dperuo's full-sized avatar

Derek Peruo dperuo

View GitHub Profile
/*
* https://www.voiceandcode.com/our-insights/2019/7/12/ux-best-practices-minimum-target-size-on-mobile-devices
*/
:root {
--min-touch-target: 48px;
--min-touch-spacing: 10px;
}
@dperuo
dperuo / memoize.ts
Last active January 16, 2025 15:07
export const memoize = <F extends (...args: any) => any>(fn: F) => {
const cache = new Map();
return (...args: Parameters<F>): ReturnType<F> => {
const key = args.join("-");
if (cache.has(key)) return cache.get(key);
const result = fn(...args);
/** @see https://web.dev/articles/optimize-long-tasks */
export const yieldToMainThread = async (): Promise<void> =>
globalThis.scheduler?.yield ? globalThis.scheduler.yield() : new Promise(resolve => setTimeout(resolve, 0));
export const digit = () => (crypto.getRandomValues(new Uint8Array(1)).at(0) ?? 0) % 10;
export const batch = (grouping = 5) => [...Array(grouping)].map(() => digit()).join('');
export const otp = (capacity = 10_000, grouping = 5) => [...Array(capacity / grouping)].map(() => batch()).join(' ');
const token = (): string => {
const valid = (id: string) => '1234567890'.split('').every(item => id.at(0) !== item);
const id = crypto.randomUUID().split('-').join('');
return valid(id) ? id : token()
}
enum Size {
Small = 1,
fixture.componentRef.setInput()
// https://kyleshevlin.com/pattern-matching/
const compass = (x: number): string => {
switch (true) {
case x < 0: return compass(x + 360);
case x === 0: return 'N';
case x === 90: return 'E';
case x === 180: return 'S';
case x === 270: return 'W';
// https://www.typescriptlang.org/docs/handbook/2/functions.html#call-signatures
interface Lazy<T> {
(): T
}
interface WritableLazy<T> extends Lazy<T> {
asReadonly: () => Lazy<T>
set: (value: T) => void;
}
import { Buffer } from 'node:buffer';
import { encode, decode } from '@msgpack/msgpack';
export const serialize = <T>(data: T, kind: BufferEncoding = 'base64') => {
return typeof Buffer === 'undefined'
? btoa(typeof data === 'string' ? data : JSON.stringify(data))
: Buffer.from(encode(data)).toString(kind);
};
export const deserialize = <R>(data: string, kind: BufferEncoding = 'base64'): R => {