Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

Commit 21b6e54

Browse files
committed
feat: add @opencode-ai/util package with utility functions
1 parent a0fe59a commit 21b6e54

5 files changed

Lines changed: 57 additions & 0 deletions

File tree

packages/util/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "@opencode-ai/util",
3+
"version": "0.0.0",
4+
"private": true,
5+
"type": "module",
6+
"exports": {
7+
"./*": "./src/*.ts"
8+
},
9+
"scripts": {
10+
"typecheck": "tsc --noEmit"
11+
},
12+
"dependencies": {
13+
"zod": "catalog:"
14+
},
15+
"devDependencies": {
16+
"typescript": "catalog:"
17+
}
18+
}

packages/util/src/fn.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { z } from "zod"
2+
3+
export function fn<T extends z.ZodType, Result>(schema: T, cb: (input: z.infer<T>) => Result) {
4+
const result = (input: z.infer<T>) => {
5+
const parsed = schema.parse(input)
6+
return cb(parsed)
7+
}
8+
result.force = (input: z.infer<T>) => cb(input)
9+
result.schema = schema
10+
return result
11+
}

packages/util/src/iife.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function iife<T>(fn: () => T) {
2+
return fn()
3+
}

packages/util/src/lazy.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function lazy<T>(fn: () => T) {
2+
let value: T | undefined
3+
let loaded = false
4+
5+
return (): T => {
6+
if (loaded) return value as T
7+
loaded = true
8+
value = fn()
9+
return value as T
10+
}
11+
}

packages/util/tsconfig.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ESNext",
4+
"module": "ESNext",
5+
"moduleResolution": "bundler",
6+
"skipLibCheck": true,
7+
"allowSyntheticDefaultImports": true,
8+
"esModuleInterop": true,
9+
"allowJs": true,
10+
"noEmit": true,
11+
"strict": true,
12+
"isolatedModules": true
13+
}
14+
}

0 commit comments

Comments
 (0)