diff options
| author | Dax Raad <[email protected]> | 2025-11-18 20:02:10 -0500 |
|---|---|---|
| committer | Dax Raad <[email protected]> | 2025-11-18 20:02:10 -0500 |
| commit | 21b6e5404eb56f4b003f8cb582646e2132e1b660 (patch) | |
| tree | 84af1ce87e379d63f61e99cd329ed469a582fddf | |
| parent | a0fe59ab75b7363d0c8735821ce67001f09aa487 (diff) | |
| download | opencode-21b6e5404eb56f4b003f8cb582646e2132e1b660.tar.gz opencode-21b6e5404eb56f4b003f8cb582646e2132e1b660.zip | |
feat: add @opencode-ai/util package with utility functions
| -rw-r--r-- | packages/util/package.json | 18 | ||||
| -rw-r--r-- | packages/util/src/fn.ts | 11 | ||||
| -rw-r--r-- | packages/util/src/iife.ts | 3 | ||||
| -rw-r--r-- | packages/util/src/lazy.ts | 11 | ||||
| -rw-r--r-- | packages/util/tsconfig.json | 14 |
5 files changed, 57 insertions, 0 deletions
diff --git a/packages/util/package.json b/packages/util/package.json new file mode 100644 index 000000000..199bd2f20 --- /dev/null +++ b/packages/util/package.json @@ -0,0 +1,18 @@ +{ + "name": "@opencode-ai/util", + "version": "0.0.0", + "private": true, + "type": "module", + "exports": { + "./*": "./src/*.ts" + }, + "scripts": { + "typecheck": "tsc --noEmit" + }, + "dependencies": { + "zod": "catalog:" + }, + "devDependencies": { + "typescript": "catalog:" + } +} diff --git a/packages/util/src/fn.ts b/packages/util/src/fn.ts new file mode 100644 index 000000000..9efe4622f --- /dev/null +++ b/packages/util/src/fn.ts @@ -0,0 +1,11 @@ +import { z } from "zod" + +export function fn<T extends z.ZodType, Result>(schema: T, cb: (input: z.infer<T>) => Result) { + const result = (input: z.infer<T>) => { + const parsed = schema.parse(input) + return cb(parsed) + } + result.force = (input: z.infer<T>) => cb(input) + result.schema = schema + return result +} diff --git a/packages/util/src/iife.ts b/packages/util/src/iife.ts new file mode 100644 index 000000000..ca9ae6c10 --- /dev/null +++ b/packages/util/src/iife.ts @@ -0,0 +1,3 @@ +export function iife<T>(fn: () => T) { + return fn() +} diff --git a/packages/util/src/lazy.ts b/packages/util/src/lazy.ts new file mode 100644 index 000000000..935ebe0f9 --- /dev/null +++ b/packages/util/src/lazy.ts @@ -0,0 +1,11 @@ +export function lazy<T>(fn: () => T) { + let value: T | undefined + let loaded = false + + return (): T => { + if (loaded) return value as T + loaded = true + value = fn() + return value as T + } +} diff --git a/packages/util/tsconfig.json b/packages/util/tsconfig.json new file mode 100644 index 000000000..528dcd91d --- /dev/null +++ b/packages/util/tsconfig.json @@ -0,0 +1,14 @@ +{ + "compilerOptions": { + "target": "ESNext", + "module": "ESNext", + "moduleResolution": "bundler", + "skipLibCheck": true, + "allowSyntheticDefaultImports": true, + "esModuleInterop": true, + "allowJs": true, + "noEmit": true, + "strict": true, + "isolatedModules": true + } +} |
