Skip to content

Instantly share code, notes, and snippets.

View colinhacks's full-sized avatar

Colin McDonnell colinhacks

View GitHub Profile
@colinhacks
colinhacks / .zshrc
Last active October 25, 2024 03:54
`howto` shell alias for recommending terminal commands
alias howto="gh copilot suggest -t shell"
@colinhacks
colinhacks / keybindings.json
Created August 12, 2024 20:25
`goToDefinition` and `navigateBack`
[
{
"key": "cmd+;",
"command": "editor.action.goToDeclaration"
},
{
"key": "shift+cmd+;",
"command": "workbench.action.navigateBack",
"when": "canNavigateBack"
}
gh repo clone https://github.com/colinhacks/live-typescript-monorepo
cd live-typescript-monorepo
cd project-references
pnpm i
code .
# open packages/pkg-b/index.ts
# see Cannot find module 'pkg-a' or its corresponding type declarations
pnpm build # runs `tsc -b`
# restart ts server, "Cannot find module" goes away
pnpm clean # deletes node_modules in all packages

Adapted from this recommendation by @jandockx

Cyclical objects

Despite supporting recursive schemas, passing cyclical data into Zod will cause an infinite loop in some cases.

You can protect against cyclical objects starting an infinite loop (at a performance cost) with the following approach (using the above jsonSchema as an example):

@colinhacks
colinhacks / script.sh
Last active April 23, 2024 19:55
Repro portability error
gh repo clone https://github.com/t3-oss/t3-env t3env
cd t3env
git checkout @t3-oss/[email protected]
pnpm i
pnpm update zod --latest --recursive
pnpm run build
import Benchmark from "benchmark";
const datetimeValidationSuite = new Benchmark.Suite("datetime");
const DATA = "2020-01-01";
const MONTHS_31 = new Set([1, 3, 5, 7, 8, 10, 12]);
const MONTHS_30 = new Set([4, 6, 9, 11]);
const simpleDatetimeRegex = /^(\d{4})-(\d{2})-(\d{2})$/;
const datetimeRegexNoLeapYearValidation =
@colinhacks
colinhacks / index.tsx
Created September 14, 2023 20:52
CrappyNext.js
import { renderToString } from "react-dom/server";
const router = new Bun.FileSystemRouter({
style: "nextjs",
dir: "./pages",
});
Bun.serve({
port: 3000,
development: true,
@colinhacks
colinhacks / package.json
Last active September 8, 2023 16:55
Script runner benchmark
{
"name": "sayhi",
"module": "index.ts",
"type": "module",
"dependencies": {
"cowsay": "^1.5.0"
},
"scripts": {
"hi": "echo 'hi'"
}
@colinhacks
colinhacks / bench.sh
Last active January 5, 2024 09:37
Benchmark testing libraries against Zod's test suite
# requires hyperfine and Bun v0.7 or later
# https://github.com/sharkdp/hyperfine
git clone [email protected]:colinhacks/zod.git
cd zod
bun install
hyperfine --warmup 3 --runs 10 \
"bun test src" \
"npx vitest --config configs/vitest.config.ts" \
"npx jest -c configs/babel-jest.config.json" \
@colinhacks
colinhacks / index.html
Created May 13, 2022 04:13
Minimal docsify index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>user/repo</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta
name="description"
content="This is a description of user/repo"
/>