Add 26 example projects and learnings from running them unaided
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Changed files
69 files changed412 additions0 deletionsagainst dcef8e1
- Added.gitignore+3−0
- AddedLEARNINGS.md+29−0
- Addedexamples/01-hello/example.json+1−0
- Addedexamples/01-hello/src/main.tsus+3−0
- Addedexamples/02-option-basics/example.json+1−0
- Addedexamples/02-option-basics/src/main.tsus+14−0
- Addedexamples/03-result-basics/example.json+1−0
- Addedexamples/03-result-basics/src/main.tsus+9−0
- Addedexamples/04-match-option/example.json+1−0
- Addedexamples/04-match-option/src/main.tsus+13−0
- Addedexamples/05-match-result/example.json+1−0
- Addedexamples/05-match-result/src/main.tsus+22−0
- Addedexamples/06-tagged-union/example.json+1−0
- Addedexamples/06-tagged-union/src/main.tsus+21−0
- Addedexamples/07-banned-null/example.json+1−0
- Addedexamples/07-banned-null/src/main.tsus+3−0
- Addedexamples/08-banned-class/example.json+1−0
- Addedexamples/08-banned-class/src/main.tsus+5−0
- Addedexamples/09-banned-var/example.json+1−0
- Addedexamples/09-banned-var/src/main.tsus+4−0
- Addedexamples/10-banned-undefined/example.json+1−0
- Addedexamples/10-banned-undefined/src/main.tsus+3−0
- Addedexamples/11-banned-throw/example.json+1−0
- Addedexamples/11-banned-throw/src/main.tsus+5−0
- Addedexamples/12-tsus-imports-tsus/example.json+1−0
- Addedexamples/12-tsus-imports-tsus/src/main.tsus+4−0
- Addedexamples/12-tsus-imports-tsus/src/math.tsus+6−0
- Addedexamples/13-tsus-imports-ts/example.json+1−0
- Addedexamples/13-tsus-imports-ts/src/legacy.ts+19−0
- Addedexamples/13-tsus-imports-ts/src/main.tsus+11−0
- Addedexamples/14-ts-imports-tsus/example.json+1−0
- Addedexamples/14-ts-imports-tsus/src/lib.tsus+9−0
- Addedexamples/14-ts-imports-tsus/src/main.ts+9−0
- Addedexamples/15-npm-dependency/example.json+1−0
- Addedexamples/15-npm-dependency/src/main.tsus+26−0
- Addedexamples/16-boundary-fs/example.json+1−0
- Addedexamples/16-boundary-fs/src/data.txt+1−0
- Addedexamples/16-boundary-fs/src/main.tsus+13−0
- Addedexamples/17-nested-match/example.json+1−0
- Addedexamples/17-nested-match/src/main.tsus+24−0
- Addedexamples/18-generics/example.json+1−0
- Addedexamples/18-generics/src/main.tsus+25−0
- Addedexamples/19-async-result/example.json+1−0
- Addedexamples/19-async-result/src/main.tsus+26−0
- Addedexamples/20-deep-chain/example.json+1−0
- Addedexamples/20-deep-chain/src/b.tsus+4−0
- Addedexamples/20-deep-chain/src/c.tsus+3−0
- Addedexamples/20-deep-chain/src/d.tsus+1−0
- Addedexamples/20-deep-chain/src/main.tsus+6−0
- Addedexamples/21-circular-imports/example.json+1−0
- Addedexamples/21-circular-imports/src/even.tsus+3−0
- Addedexamples/21-circular-imports/src/main.tsus+6−0
- Addedexamples/21-circular-imports/src/odd.tsus+3−0
- Addedexamples/22-mixed-project/example.json+1−0
- Addedexamples/22-mixed-project/src/main.tsus+14−0
- Addedexamples/22-mixed-project/src/repo.tsus+7−0
- Addedexamples/22-mixed-project/src/store.ts+8−0
- Addedexamples/22-mixed-project/src/validate.tsus+11−0
- Addedexamples/23-cloudflare-worker/example.json+9−0
- Addedexamples/23-cloudflare-worker/src/router.tsus+8−0
- Addedexamples/23-cloudflare-worker/src/worker.tsus
- Addedexamples/24-hostile-syntax/example.json
- Addedexamples/24-hostile-syntax/src/main.tsus
- Addedexamples/25-shadowed-match/example.json
- Addedexamples/25-shadowed-match/src/main.tsus
- Addedexamples/26-json-boundary/example.json
- Addedexamples/26-json-boundary/src/main.tsus
- Addedpackage-lock.json
- Addedpackage.json
Added.gitignore+3−0
| @@ -0,0 +1,3 @@ | ||
| +node_modules/ | ||
| +dist/ | ||
| +*.log | ||
AddedLEARNINGS.md+29−0
| @@ -0,0 +1,29 @@ | ||
| +# Learnings from running the examples with no toolchain | ||
| + | ||
| +Phase 1 was writing 26 example projects under `examples/` and running them with stock tooling. Everything failed, in three distinct ways, and each failure dictated a piece of the MVP design. | ||
| + | ||
| +## Failure 1: the extension is a hard wall | ||
| + | ||
| +`node examples/01-hello/src/main.tsus` and `node --experimental-strip-types` both die with `ERR_UNKNOWN_FILE_EXTENSION` before reading a single byte. Node's type-stripping only accepts `.ts`/`.mts`, so a new extension means we own the compile step end to end. There is no incremental "mostly works" path. | ||
| + | ||
| +Consequence for the MVP: `tsus build` must exist and must emit plain `.js` before anything runs. Compiled output has to be standard ESM so Node and Cloudflare Workers can consume it with zero knowledge of `.tsus`. | ||
| + | ||
| +## Failure 2: `match` blocks are not parseable TypeScript | ||
| + | ||
| +Renaming `04-match-option/src/main.tsus` to `.ts` and running `tsc --noEmit` produces `TS1005: ',' expected` and `TS1109: Expression expected` at the match arms. The TS parser cannot recover into anything useful, so we cannot implement `match` as a tsc plugin or an AST transform over the original source. | ||
| + | ||
| +Consequence: the MVP needs a preprocessor that runs before the TypeScript parser, scanning raw text and rewriting `match (expr) { Pattern(x) => body }` into a plain function call (`__tsus_match`). The scanner has to be a real tokenizer that understands strings, template literals (including nested `${}`), and comments, because example 24 deliberately plants fake `match` blocks inside all of those. A regex will not survive it. | ||
| + | ||
| +## Failure 3: import specifiers with `.tsus` extensions break resolution | ||
| + | ||
| +Even with compiled output, `import { safeSqrt } from "./math.tsus"` would still fail at runtime because nothing on disk has that name after compilation. The examples with import chains (12, 20, 21, 22) all hit this. | ||
| + | ||
| +Consequence: the build step rewrites `.tsus` and `.ts` specifiers to `.js` in emitted code, and compiles sibling `.ts` files too, so a mixed `ts`+`tsus` project comes out as one coherent dist tree. | ||
| + | ||
| +## Smaller findings baked into the design | ||
| + | ||
| +- `await` inside a match arm: arms compile to arrow functions, so an arm whose body contains `await` must become an `async` arrow, and `__tsus_match` then returns a promise the caller awaits. Example 19 forces this. | ||
| +- `"abc".match(/\d+/)` and a user function literally named `match` (example 25) must not trigger the macro. The rewrite only fires on `match (expr) {` where the token is not preceded by `.` or an identifier character, and only when a `{` follows the closing paren. | ||
| +- Bare string-literal patterns (`"/" => ...` in the worker example) mean patterns are not just variant tags; the runtime match needs literal equality patterns plus a `_` wildcard. | ||
| +- Circular `.tsus` imports (example 21) are fine as long as the cycle is function-level, because compiled output is ESM and ESM handles cycles with hoisted bindings. The compiler does not need to detect cycles for the MVP. | ||
| +- The checker rules (no class, no null, no undefined, no var, no throw) apply to `.tsus` files only. Examples 13 and 22 have `.ts` dependencies that use classes, null, and undefined on purpose; the checker must scope itself by extension or mixed projects are impossible. | ||
Addedexamples/01-hello/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "run", "output": ["Hello, tsus!"] } | ||
Addedexamples/01-hello/src/main.tsus+3−0
| @@ -0,0 +1,3 @@ | ||
| +const greet = (name: string): string => `Hello, ${name}!`; | ||
| + | ||
| +console.log(greet("tsus")); | ||
Addedexamples/02-option-basics/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "run", "output": ["2", "-1", "20"] } | ||
Addedexamples/02-option-basics/src/main.tsus+14−0
| @@ -0,0 +1,14 @@ | ||
| +import { Some, None, type Option } from "@tsus/runtime"; | ||
| + | ||
| +const findFirst = (xs: number[], pred: (x: number) => boolean): Option<number> => { | ||
| + for (const x of xs) { | ||
| + if (pred(x)) { | ||
| + return Some(x); | ||
| + } | ||
| + } | ||
| + return None; | ||
| +}; | ||
| + | ||
| +console.log(findFirst([1, 2, 3], (x) => x > 1).unwrapOr(-1)); | ||
| +console.log(findFirst([1, 2, 3], (x) => x > 9).unwrapOr(-1)); | ||
| +console.log(findFirst([1, 2, 3], (x) => x === 2).map((x) => x * 10).unwrapOr(0)); | ||
Addedexamples/03-result-basics/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "run", "output": ["5", "-1", "4", "true"] } | ||
Addedexamples/03-result-basics/src/main.tsus+9−0
| @@ -0,0 +1,9 @@ | ||
| +import { Ok, Err, type Result } from "@tsus/runtime"; | ||
| + | ||
| +const divide = (a: number, b: number): Result<number, string> => | ||
| + b === 0 ? Err("division by zero") : Ok(a / b); | ||
| + | ||
| +console.log(divide(10, 2).unwrapOr(-1)); | ||
| +console.log(divide(10, 0).unwrapOr(-1)); | ||
| +console.log(divide(9, 3).map((x) => x + 1).unwrapOr(-1)); | ||
| +console.log(divide(1, 0).mapErr((e) => `error: ${e}`).isErr()); | ||
Addedexamples/04-match-option/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "run", "output": ["Reach me at justin@example.com", "No email on file"] } | ||
Addedexamples/04-match-option/src/main.tsus+13−0
| @@ -0,0 +1,13 @@ | ||
| +import { Some, None, type Option } from "@tsus/runtime"; | ||
| + | ||
| +const email: Option<string> = Some("justin@example.com"); | ||
| +const missing: Option<string> = None; | ||
| + | ||
| +const describe = (opt: Option<string>): string => | ||
| + match (opt) { | ||
| + Some(addr) => `Reach me at ${addr}`, | ||
| + None => "No email on file", | ||
| + }; | ||
| + | ||
| +console.log(describe(email)); | ||
| +console.log(describe(missing)); | ||
Addedexamples/05-match-result/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "run", "output": ["listening on 8080", "bad port (not a number: banana)", "bad port (out of range: 99999)"] } | ||
Addedexamples/05-match-result/src/main.tsus+22−0
| @@ -0,0 +1,22 @@ | ||
| +import { Ok, Err, type Result } from "@tsus/runtime"; | ||
| + | ||
| +const parsePort = (raw: string): Result<number, string> => { | ||
| + const n = Number(raw); | ||
| + if (Number.isNaN(n)) { | ||
| + return Err(`not a number: ${raw}`); | ||
| + } | ||
| + if (n < 1 || n > 65535) { | ||
| + return Err(`out of range: ${n}`); | ||
| + } | ||
| + return Ok(n); | ||
| +}; | ||
| + | ||
| +const report = (raw: string): string => | ||
| + match (parsePort(raw)) { | ||
| + Ok(port) => `listening on ${port}`, | ||
| + Err(reason) => `bad port (${reason})`, | ||
| + }; | ||
| + | ||
| +console.log(report("8080")); | ||
| +console.log(report("banana")); | ||
| +console.log(report("99999")); | ||
Addedexamples/06-tagged-union/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "run", "output": ["13", "12", "0"] } | ||
Addedexamples/06-tagged-union/src/main.tsus+21−0
| @@ -0,0 +1,21 @@ | ||
| +import { tag, type Tagged } from "@tsus/runtime"; | ||
| + | ||
| +type Shape = | ||
| + | Tagged<"Circle", { r: number }> | ||
| + | Tagged<"Rect", { w: number; h: number }> | ||
| + | Tagged<"Point", {}>; | ||
| + | ||
| +const circle: Shape = tag("Circle", { r: 2 }); | ||
| +const rect: Shape = tag("Rect", { w: 3, h: 4 }); | ||
| +const point: Shape = tag("Point", {}); | ||
| + | ||
| +const area = (s: Shape): number => | ||
| + match (s) { | ||
| + Circle(c) => Math.round(Math.PI * c.r * c.r), | ||
| + Rect(d) => d.w * d.h, | ||
| + _ => 0, | ||
| + }; | ||
| + | ||
| +console.log(area(circle)); | ||
| +console.log(area(rect)); | ||
| +console.log(area(point)); | ||
Addedexamples/07-banned-null/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "check-fail", "codes": ["TSUS002"] } | ||
Addedexamples/07-banned-null/src/main.tsus+3−0
| @@ -0,0 +1,3 @@ | ||
| +const legacy: string | null = null; | ||
| + | ||
| +console.log(legacy); | ||
Addedexamples/08-banned-class/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "check-fail", "codes": ["TSUS001"] } | ||
Addedexamples/08-banned-class/src/main.tsus+5−0
| @@ -0,0 +1,5 @@ | ||
| +class User { | ||
| + constructor(public name: string) {} | ||
| +} | ||
| + | ||
| +console.log(new User("nope").name); | ||
Addedexamples/09-banned-var/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "check-fail", "codes": ["TSUS004"] } | ||
Addedexamples/09-banned-var/src/main.tsus+4−0
| @@ -0,0 +1,4 @@ | ||
| +var count = 0; | ||
| +count = count + 1; | ||
| + | ||
| +console.log(count); | ||
Addedexamples/10-banned-undefined/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "check-fail", "codes": ["TSUS003"] } | ||
Addedexamples/10-banned-undefined/src/main.tsus+3−0
| @@ -0,0 +1,3 @@ | ||
| +const maybe: string | undefined = undefined; | ||
| + | ||
| +console.log(typeof maybe); | ||
Addedexamples/11-banned-throw/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "check-fail", "codes": ["TSUS005"] } | ||
Addedexamples/11-banned-throw/src/main.tsus+5−0
| @@ -0,0 +1,5 @@ | ||
| +const explode = (msg: string): never => { | ||
| + throw new Error(msg); | ||
| +}; | ||
| + | ||
| +console.log(explode("boom")); | ||
Addedexamples/12-tsus-imports-tsus/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "run", "output": ["8", "-1"] } | ||
Addedexamples/12-tsus-imports-tsus/src/main.tsus+4−0
| @@ -0,0 +1,4 @@ | ||
| +import { safeSqrt, double } from "./math.tsus"; | ||
| + | ||
| +console.log(safeSqrt(16).map(double).unwrapOr(-1)); | ||
| +console.log(safeSqrt(-4).map(double).unwrapOr(-1)); | ||
Addedexamples/12-tsus-imports-tsus/src/math.tsus+6−0
| @@ -0,0 +1,6 @@ | ||
| +import { Some, None, type Option } from "@tsus/runtime"; | ||
| + | ||
| +export const safeSqrt = (x: number): Option<number> => | ||
| + x < 0 ? None : Some(Math.sqrt(x)); | ||
| + | ||
| +export const double = (x: number): number => x * 2; | ||
Addedexamples/13-tsus-imports-ts/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "run", "output": ["1", "2", "justin", "nobody"] } | ||
Addedexamples/13-tsus-imports-ts/src/legacy.ts+19−0
| @@ -0,0 +1,19 @@ | ||
| +// Plain TypeScript: the strict rules do NOT apply here. | ||
| +// This file deliberately uses a class and null to prove that. | ||
| +class Counter { | ||
| + private n: number = 0; | ||
| + bump(): number { | ||
| + this.n += 1; | ||
| + return this.n; | ||
| + } | ||
| +} | ||
| + | ||
| +const shared = new Counter(); | ||
| + | ||
| +export function nextId(): number { | ||
| + return shared.bump(); | ||
| +} | ||
| + | ||
| +export function findUser(id: number): string | null { | ||
| + return id === 1 ? "justin" : null; | ||
| +} | ||
Addedexamples/13-tsus-imports-ts/src/main.tsus+11−0
| @@ -0,0 +1,11 @@ | ||
| +import { fromNullable } from "@tsus/runtime/boundary"; | ||
| +import { nextId, findUser } from "./legacy.ts"; | ||
| + | ||
| +console.log(nextId()); | ||
| +console.log(nextId()); | ||
| + | ||
| +const user = fromNullable(findUser(1)); | ||
| +const ghost = fromNullable(findUser(42)); | ||
| + | ||
| +console.log(user.unwrapOr("nobody")); | ||
| +console.log(ghost.unwrapOr("nobody")); | ||
Addedexamples/14-ts-imports-tsus/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "run", "output": ["30", "-1", "true"] } | ||
Addedexamples/14-ts-imports-tsus/src/lib.tsus+9−0
| @@ -0,0 +1,9 @@ | ||
| +import { Ok, Err, type Result } from "@tsus/runtime"; | ||
| + | ||
| +export const parseAge = (raw: string): Result<number, string> => { | ||
| + const n = Number(raw); | ||
| + if (Number.isNaN(n) || n < 0 || n > 150) { | ||
| + return Err(`invalid age: ${raw}`); | ||
| + } | ||
| + return Ok(n); | ||
| +}; | ||
Addedexamples/14-ts-imports-tsus/src/main.ts+9−0
| @@ -0,0 +1,9 @@ | ||
| +// Plain TypeScript entrypoint consuming a .tsus module. | ||
| +import { parseAge } from "./lib.tsus"; | ||
| + | ||
| +const good = parseAge("30"); | ||
| +const bad = parseAge("banana"); | ||
| + | ||
| +console.log(good.unwrapOr(-1)); | ||
| +console.log(bad.unwrapOr(-1)); | ||
| +console.log(bad.isErr()); | ||
Addedexamples/15-npm-dependency/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "run", "output": ["justin is 30", "rejected"] } | ||
Addedexamples/15-npm-dependency/src/main.tsus+26−0
| @@ -0,0 +1,26 @@ | ||
| +import { z } from "zod"; | ||
| +import { Ok, Err, type Result } from "@tsus/runtime"; | ||
| + | ||
| +const UserSchema = z.object({ | ||
| + name: z.string(), | ||
| + age: z.number().int().min(0), | ||
| +}); | ||
| + | ||
| +type User = z.infer<typeof UserSchema>; | ||
| + | ||
| +const parseUser = (input: unknown): Result<User, string> => { | ||
| + const outcome = UserSchema.safeParse(input); | ||
| + return outcome.success ? Ok(outcome.data) : Err(outcome.error.issues[0].message); | ||
| +}; | ||
| + | ||
| +const good = parseUser({ name: "justin", age: 30 }); | ||
| +const bad = parseUser({ name: "justin", age: "old" }); | ||
| + | ||
| +console.log(match (good) { | ||
| + Ok(u) => `${u.name} is ${u.age}`, | ||
| + Err(e) => `rejected: ${e}`, | ||
| +}); | ||
| +console.log(match (bad) { | ||
| + Ok(u) => `${u.name} is ${u.age}`, | ||
| + Err(e) => "rejected", | ||
| +}); | ||
Addedexamples/16-boundary-fs/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "run", "output": ["hello from disk", "<read failed>"], "assets": ["src/data.txt"] } | ||
Addedexamples/16-boundary-fs/src/data.txt+1−0
| @@ -0,0 +1 @@ | ||
| +hello from disk | ||
Addedexamples/16-boundary-fs/src/main.tsus+13−0
| @@ -0,0 +1,13 @@ | ||
| +import { readFile } from "node:fs/promises"; | ||
| +import { wrapAsync } from "@tsus/runtime/boundary"; | ||
| + | ||
| +const safeRead = wrapAsync(readFile); | ||
| + | ||
| +const here = new URL("data.txt", import.meta.url).pathname; | ||
| +const nowhere = new URL("missing.txt", import.meta.url).pathname; | ||
| + | ||
| +const found = await safeRead(here, "utf8"); | ||
| +const missing = await safeRead(nowhere, "utf8"); | ||
| + | ||
| +console.log(found.map((text) => text.trim()).unwrapOr("<read failed>")); | ||
| +console.log(missing.map((text) => text.trim()).unwrapOr("<read failed>")); | ||
Addedexamples/17-nested-match/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "run", "output": ["retries=3", "retries=default", "broken: empty config"] } | ||
Addedexamples/17-nested-match/src/main.tsus+24−0
| @@ -0,0 +1,24 @@ | ||
| +import { Some, None, Ok, Err, type Option, type Result } from "@tsus/runtime"; | ||
| + | ||
| +type Config = { retries: Option<number> }; | ||
| + | ||
| +const loadConfig = (raw: string): Result<Config, string> => { | ||
| + if (raw === "") { | ||
| + return Err("empty config"); | ||
| + } | ||
| + const n = Number(raw); | ||
| + return Ok({ retries: Number.isNaN(n) ? None : Some(n) }); | ||
| +}; | ||
| + | ||
| +const describe = (raw: string): string => | ||
| + match (loadConfig(raw)) { | ||
| + Ok(cfg) => match (cfg.retries) { | ||
| + Some(n) => `retries=${n}`, | ||
| + None => "retries=default", | ||
| + }, | ||
| + Err(e) => `broken: ${e}`, | ||
| + }; | ||
| + | ||
| +console.log(describe("3")); | ||
| +console.log(describe("lots")); | ||
| +console.log(describe("")); | ||
Addedexamples/18-generics/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "run", "output": ["7", "-1", "x:1", "true"] } | ||
Addedexamples/18-generics/src/main.tsus+25−0
| @@ -0,0 +1,25 @@ | ||
| +import { Some, None, Ok, Err, type Option, type Result } from "@tsus/runtime"; | ||
| + | ||
| +const firstOk = <T, E>(results: Result<T, E>[]): Option<T> => { | ||
| + for (const r of results) { | ||
| + if (r.isOk()) { | ||
| + return r.ok(); | ||
| + } | ||
| + } | ||
| + return None; | ||
| +}; | ||
| + | ||
| +const zip = <A, B>(a: Option<A>, b: Option<B>): Option<[A, B]> => | ||
| + a.andThen((va) => b.map((vb): [A, B] => [va, vb])); | ||
| + | ||
| +const attempts: Result<number, string>[] = [Err("cold"), Err("colder"), Ok(7), Ok(9)]; | ||
| + | ||
| +console.log(firstOk(attempts).unwrapOr(-1)); | ||
| +console.log(firstOk<number, string>([Err("a"), Err("b")]).unwrapOr(-1)); | ||
| + | ||
| +const pair = zip(Some("x"), Some(1)); | ||
| +console.log(match (pair) { | ||
| + Some(p) => `${p[0]}:${p[1]}`, | ||
| + None => "no pair", | ||
| +}); | ||
| +console.log(zip(Some("x"), None).isNone()); | ||
Addedexamples/19-async-result/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "run", "output": ["PAYLOAD", "failed: 404 /nope"] } | ||
Addedexamples/19-async-result/src/main.tsus+26−0
| @@ -0,0 +1,26 @@ | ||
| +import { Ok, Err, type Result } from "@tsus/runtime"; | ||
| + | ||
| +const fakeFetch = async (path: string): Promise<Result<string, string>> => { | ||
| + await Promise.resolve(); | ||
| + return path === "/ok" ? Ok("payload") : Err(`404 ${path}`); | ||
| +}; | ||
| + | ||
| +const enrich = async (body: string): Promise<string> => { | ||
| + await Promise.resolve(); | ||
| + return body.toUpperCase(); | ||
| +}; | ||
| + | ||
| +const good = await fakeFetch("/ok"); | ||
| +const bad = await fakeFetch("/nope"); | ||
| + | ||
| +const goodMsg = await match (good) { | ||
| + Ok(body) => await enrich(body), | ||
| + Err(e) => `failed: ${e}`, | ||
| +}; | ||
| +const badMsg = await match (bad) { | ||
| + Ok(body) => await enrich(body), | ||
| + Err(e) => `failed: ${e}`, | ||
| +}; | ||
| + | ||
| +console.log(goodMsg); | ||
| +console.log(badMsg); | ||
Addedexamples/20-deep-chain/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "run", "output": ["chain says 5"] } | ||
Addedexamples/20-deep-chain/src/b.tsus+4−0
| @@ -0,0 +1,4 @@ | ||
| +import { squared } from "./c.tsus"; | ||
| +import { Some, type Option } from "@tsus/runtime"; | ||
| + | ||
| +export const wrapped = (): Option<number> => Some(squared() + 1); | ||
Addedexamples/20-deep-chain/src/c.tsus+3−0
| @@ -0,0 +1,3 @@ | ||
| +import { base } from "./d.tsus"; | ||
| + | ||
| +export const squared = (): number => base() * base(); | ||
Addedexamples/20-deep-chain/src/d.tsus+1−0
| @@ -0,0 +1 @@ | ||
| +export const base = (): number => 2; | ||
Addedexamples/20-deep-chain/src/main.tsus+6−0
| @@ -0,0 +1,6 @@ | ||
| +import { wrapped } from "./b.tsus"; | ||
| + | ||
| +console.log(match (wrapped()) { | ||
| + Some(n) => `chain says ${n}`, | ||
| + None => "chain broke", | ||
| +}); | ||
Addedexamples/21-circular-imports/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "run", "output": ["true", "true", "false"] } | ||
Addedexamples/21-circular-imports/src/even.tsus+3−0
| @@ -0,0 +1,3 @@ | ||
| +import { isOdd } from "./odd.tsus"; | ||
| + | ||
| +export const isEven = (n: number): boolean => (n === 0 ? true : isOdd(n - 1)); | ||
Addedexamples/21-circular-imports/src/main.tsus+6−0
| @@ -0,0 +1,6 @@ | ||
| +import { isEven } from "./even.tsus"; | ||
| +import { isOdd } from "./odd.tsus"; | ||
| + | ||
| +console.log(isEven(10)); | ||
| +console.log(isOdd(7)); | ||
| +console.log(isEven(3)); | ||
Addedexamples/21-circular-imports/src/odd.tsus+3−0
| @@ -0,0 +1,3 @@ | ||
| +import { isEven } from "./even.tsus"; | ||
| + | ||
| +export const isOdd = (n: number): boolean => (n === 0 ? false : isEven(n - 1)); | ||
Addedexamples/22-mixed-project/example.json+1−0
| @@ -0,0 +1 @@ | ||
| +{ "expect": "run", "output": ["ada (36)", "grace (45)", "not found"] } | ||
Addedexamples/22-mixed-project/src/main.tsus+14−0
| @@ -0,0 +1,14 @@ | ||
| +import { loadRow } from "./repo.tsus"; | ||
| + | ||
| +const show = (id: number): string => | ||
| + match (loadRow(id)) { | ||
| + Some(validated) => match (validated) { | ||
| + Ok(row) => `${row.name} (${row.age})`, | ||
| + Err(e) => `bad data: ${e}`, | ||
| + }, | ||
| + None => "not found", | ||
| + }; | ||
| + | ||
| +console.log(show(1)); | ||
| +console.log(show(2)); | ||
| +console.log(show(99)); | ||
Addedexamples/22-mixed-project/src/repo.tsus+7−0
| @@ -0,0 +1,7 @@ | ||
| +import { fromNullable } from "@tsus/runtime/boundary"; | ||
| +import { type Option, type Result } from "@tsus/runtime"; | ||
| +import { getRow } from "./store.ts"; | ||
| +import { validateRow, type Row } from "./validate.tsus"; | ||
| + | ||
| +export const loadRow = (id: number): Option<Result<Row, string>> => | ||
| + fromNullable(getRow(id)).map(validateRow); | ||
Addedexamples/22-mixed-project/src/store.ts+8−0
| @@ -0,0 +1,8 @@ | ||
| +// Plain TS in-memory store. Uses undefined internally: allowed here, not in .tsus. | ||
| +const rows = new Map<number, { name: string; age: number }>(); | ||
| +rows.set(1, { name: "ada", age: 36 }); | ||
| +rows.set(2, { name: "grace", age: 45 }); | ||
| + | ||
| +export function getRow(id: number): { name: string; age: number } | undefined { | ||
| + return rows.get(id); | ||
| +} | ||
Addedexamples/22-mixed-project/src/validate.tsus+11−0
| @@ -0,0 +1,11 @@ | ||
| +import { z } from "zod"; | ||
| +import { Ok, Err, type Result } from "@tsus/runtime"; | ||
| + | ||
| +const RowSchema = z.object({ name: z.string().min(1), age: z.number().min(18) }); | ||
| + | ||
| +export type Row = z.infer<typeof RowSchema>; | ||
| + | ||
| +export const validateRow = (input: unknown): Result<Row, string> => { | ||
| + const outcome = RowSchema.safeParse(input); | ||
| + return outcome.success ? Ok(outcome.data) : Err("invalid row"); | ||
| +}; | ||
Addedexamples/23-cloudflare-worker/example.json+9−0
| @@ -0,0 +1,9 @@ | ||
| +{ | ||
| + "expect": "worker", | ||
| + "entry": "worker.js", | ||
| + "requests": [ | ||
| + { "path": "/", "status": 200, "body": "home sweet home" }, | ||
| + { "path": "/health", "status": 200, "body": "ok" }, | ||
| + { "path": "/nope", "status": 404, "body": "not found" } | ||
| + ] | ||
| +} | ||
Addedexamples/23-cloudflare-worker/src/router.tsus+8−0
| @@ -0,0 +1,8 @@ | ||
| +import { Some, None, type Option } from "@tsus/runtime"; | ||
| + | ||
| +export const route = (path: string): Option<string> => | ||
| + match (path) { | ||
| + "/" => Some("home sweet home"), | ||
| + "/health" => Some("ok"), | ||
| + _ => None, | ||
| + }; | ||
Addedexamples/23-cloudflare-worker/src/worker.tsus
Not shown: this page diffs the first 60 files. View the file or the raw file.
Addedexamples/24-hostile-syntax/example.json
Not shown: this page diffs the first 60 files. View the file or the raw file.
Addedexamples/24-hostile-syntax/src/main.tsus
Not shown: this page diffs the first 60 files. View the file or the raw file.
Addedexamples/25-shadowed-match/example.json
Not shown: this page diffs the first 60 files. View the file or the raw file.
Addedexamples/25-shadowed-match/src/main.tsus
Not shown: this page diffs the first 60 files. View the file or the raw file.
Addedexamples/26-json-boundary/example.json
Not shown: this page diffs the first 60 files. View the file or the raw file.
Addedexamples/26-json-boundary/src/main.tsus
Not shown: this page diffs the first 60 files. View the file or the raw file.
Addedpackage-lock.json
Not shown: this page diffs the first 60 files. View the file or the raw file.
Addedpackage.json
Not shown: this page diffs the first 60 files. View the file or the raw file.
Checks
No checks reported for this commit.
Runs
No runs for this commit.