feat: add @opencode-ai/util package with utility functions

This commit is contained in:
Dax Raad
2025-11-18 20:02:10 -05:00
parent a0fe59ab75
commit 21b6e5404e
5 changed files with 57 additions and 0 deletions

View File

@@ -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:"
}
}

11
packages/util/src/fn.ts Normal file
View File

@@ -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
}

View File

@@ -0,0 +1,3 @@
export function iife<T>(fn: () => T) {
return fn()
}

11
packages/util/src/lazy.ts Normal file
View File

@@ -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
}
}

View File

@@ -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
}
}