mirror of
https://github.com/anomalyco/opencode.git
synced 2026-02-01 22:48:16 +00:00
feat: add @opencode-ai/util package with utility functions
This commit is contained in:
18
packages/util/package.json
Normal file
18
packages/util/package.json
Normal 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
11
packages/util/src/fn.ts
Normal 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
|
||||
}
|
||||
3
packages/util/src/iife.ts
Normal file
3
packages/util/src/iife.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export function iife<T>(fn: () => T) {
|
||||
return fn()
|
||||
}
|
||||
11
packages/util/src/lazy.ts
Normal file
11
packages/util/src/lazy.ts
Normal 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
|
||||
}
|
||||
}
|
||||
14
packages/util/tsconfig.json
Normal file
14
packages/util/tsconfig.json
Normal 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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user