feat: add @opencode-ai/util package with utility functions
This commit is contained in:
parent
a0fe59ab75
commit
21b6e5404e
5 changed files with 57 additions and 0 deletions
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
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue