Skip to content

Instantly share code, notes, and snippets.

View ryangoree's full-sized avatar
🌎

Ryan Goree ryangoree

🌎
View GitHub Profile
export type AnyObject = Record<PropertyKey, any>;
/**
* A boolean indicating whether `T` is a plain, record-like object (e.g., `{ a:
* string }`), excluding functions, arrays/tuples, and common built-ins (`Map`,
* `Set`, `Date`, etc.).
*
* @example
* ```ts
* type A = IsPlainObject<{ a: string }>; // true
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type AnyObject = Record<PropertyKey, any>;
/**
* Detect whether `T` is a plain, record-like object (e.g., `{ a: string }`),
* excluding functions, arrays/tuples, and common built-ins (`Map`, `Set`,
* `Date`, etc.).
*
* @example
* ```ts
@ryangoree
ryangoree / distributive-conditional-types.ts
Last active March 31, 2026 21:53
Notes on when TypeScript conditional types become distributive
// A conditional type only distributes over unions that are:
// - On the left side of `extends` (the checked type).
// - Generic type variables (introduced via `infer` or generic type parameters).
// - Naked (not wrapped in another type).
//
// See:
// - https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
// - https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#inferring-within-conditional-types
type U = 1 | "a";
@ryangoree
ryangoree / ansi.sh
Created March 30, 2026 21:13
POSIX-compliant formatting functions.
# ANSI Select Graphic Rendition (SGR) Formatting Syntax:
#
# ESC [ Ps [; Ps]* m
#
# Where:
# ESC = '\033' (octal) / '\u001b' (unicode) / '\x1b' (hex)
# '[' = Control Sequence Introducer (CSI)
# Ps = one or more numeric parameters Values(e.g. '1', '2', '31', '44')
#
# Example: '\033[4;31mHello\033[0m' → underlined (4) red (31) 'Hello'
@ryangoree
ryangoree / mathEvaluator.test.ts
Last active August 20, 2025 21:10
A quick and dirty math expression evaluator.
import assert from "node:assert";
import { describe, it } from "node:test";
import { evaluate } from "./mathEvaluator.ts";
describe("evaluate", () => {
it("evaluates single numbers", () => {
assert.strictEqual(evaluate("1"), 1);
assert.strictEqual(evaluate("12"), 12);
assert.strictEqual(evaluate("12.34"), 12.34);
assert.strictEqual(evaluate(".12"), 0.12);
@ryangoree
ryangoree / IndexableKey.ts
Created July 30, 2025 05:13
Get a union of all possible keys for `T` without losing type inference for literal keys.
/**
* One of the *explicit* property names of {@linkcode T}, with any `string` or
* `number` index signatures stripped out.
*
* @example
* ```ts
* type A = LiteralKey<{
* [k: string]: any;
* id: string;
@ryangoree
ryangoree / ParseNumberish.ts
Created July 7, 2025 17:40
🚧 WIP 🚧 - Parse a number-like value in TypeScript types.
//--- Tuple operations ---//
type BuildTuple<
L extends number,
T extends any[] = [],
F = unknown,
> = `${L}` extends `-${number}`
? never
: T["length"] extends L
? T
@ryangoree
ryangoree / recursiveRead.ts
Last active June 27, 2025 16:12
Recursively read a directory with callbacks for each file and directory.
import { readdirSync, readFileSync, statSync } from "node:fs";
import { join } from "node:path";
export function recursiveRead({
entryPath,
onDirectory,
onDirectoryExit,
onFile,
onError,
}: {
@ryangoree
ryangoree / ANSI.md
Created June 3, 2025 20:16 — forked from fnky/ANSI.md
ANSI Escape Codes

ANSI Escape Sequences

Standard escape codes are prefixed with Escape:

  • Ctrl-Key: ^[
  • Octal: \033
  • Unicode: \u001b
  • Hexadecimal: \x1B
  • Decimal: 27
@ryangoree
ryangoree / KeyMap.ts
Last active May 18, 2025 01:03
A utility type to ensure a type is defined with all given keys.
/**
* Get {@linkcode T}, ensuring that all keys from {@linkcode K} are present. If
* any keys are missing, a type error will be thrown.
*
* @typeParam K - The keys to ensure are present.
* @typeParam T - An object that should contain all keys from {@linkcode K}.
* @typeParam TDefault - The default value to use for missing keys.
*
* @example
* ```ts