/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-unsafe-return */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ // eslint-disable-next-line import/no-unassigned-import import "./builtin.js" import { getOrUndefined as value, type Some } from "effect/Option" import * as Option from "effect/Option" export * from "effect/Option" export const getOrUndefined = value export function omitableToNullable(om: Option.Option | undefined) { return om ?? Option.fromNullishOr(om) } export const toBool = Option.match({ onNone: () => false, onSome: () => true }) export const fromBool = (b: boolean) => (b ? Option.some(true) : Option.none()) /** * Access property, unwrapping Options along the path */ export function p>>( k: K ): (v: Option.Option) => Option.Option<_A> export function p(k: K): (v: Option.Option) => Option.Option export function p(k: any) { return (v: Option.Option) => Option.flatMap(v, (a) => convert((a as any)[k])) } function convert(a: any) { return Option.isSome(a) || Option.isNone(a) ? a : Option.fromNullishOr(a) } export type _A = A extends Some ? Y : never type KeysMatching = { [K in keyof T]: T[K] extends V ? K : never }[keyof T] export const PartialExceptionTypeId = Symbol() export type PartialExceptionTypeId = typeof PartialExceptionTypeId export class PartialException { readonly _typeId: PartialExceptionTypeId = PartialExceptionTypeId } function raisePartial(): X { throw new PartialException() } /** * Simulates a partial function */ export function partial( f: (miss: () => X) => (...args: ARGS) => A ): (...args: ARGS) => Option.Option { return (...args) => { try { return Option.some(f(raisePartial)(...args)) } catch (e) { if (e instanceof PartialException) { return Option.none() } throw e } } }