@railway-ts/pipelines 0.1.17 → 0.1.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -12
- package/dist/option/index.cjs +4 -0
- package/dist/option/index.cjs.map +1 -1
- package/dist/option/index.d.cts +1 -1
- package/dist/option/index.d.ts +1 -1
- package/dist/option/index.mjs +4 -1
- package/dist/option/index.mjs.map +1 -1
- package/dist/result/index.cjs +5 -0
- package/dist/result/index.cjs.map +1 -1
- package/dist/result/index.d.cts +31 -1
- package/dist/result/index.d.ts +31 -1
- package/dist/result/index.mjs +5 -1
- package/dist/result/index.mjs.map +1 -1
- package/dist/schema/index.cjs.map +1 -1
- package/dist/schema/index.mjs.map +1 -1
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/option/option.ts","../../src/result/result.ts"],"names":[],"mappings":";AAWO,IAAM,YAAA,GAAe,OAAO,cAAc,CAAA;AA6B1C,SAAS,KAAQ,KAAA,EAAqB;AAC3C,EAAA,OAAO;AAAA,IACL,IAAA,EAAM,IAAA;AAAA,IACN,KAAA;AAAA,IACA,CAAC,YAAY,GAAG;AAAA,GAClB;AACF;AAWO,SAAS,IAAA,GAA6B;AAC3C,EAAA,OAAO;AAAA,IACL,IAAA,EAAM,KAAA;AAAA,IACN,CAAC,YAAY,GAAG;AAAA,GAClB;AACF;;;ACnDO,IAAM,YAAA,GAAe,OAAO,cAAc;AAkC1C,SAAS,GAAiB,KAAA,EAAwB;AACvD,EAAA,OAAO;AAAA,IACL,EAAA,EAAI,IAAA;AAAA,IACJ,KAAA;AAAA,IACA,CAAC,YAAY,GAAG;AAAA,GAClB;AACF;AAWO,SAAS,IAAO,KAAA,EAA4B;AACjD,EAAA,OAAO,EAAE,EAAA,EAAI,KAAA,EAAO,OAAO,CAAC,YAAY,GAAG,OAAA,EAAQ;AACrD;AAiBO,SAAS,KACd,MAAA,EAKA;AACA,EAAA,OAAO,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CAAO,MAAA,EAAQ,WAAW,KAAK,CAAA;AACpD;AA0BO,SAAS,QAAW,EAAA,EAA2F;AACpH,EAAA,OAAO,CAAC,MAAA,KAAW;AACjB,IAAA,IAAI,CAAC,MAAA,CAAO,EAAA,EAAI,OAAO,MAAA;AACvB,IAAA,MAAM,GAAA,GAAM,EAAA,CAAG,MAAA,CAAO,KAAK,CAAA;AAC3B,IAAA,IAAI,eAAe,OAAA,EAAS,OAAO,GAAA,CAAI,IAAA,CAAK,MAAM,MAAM,CAAA;AACxD,IAAA,OAAO,MAAA;AAAA,EACT,CAAA;AACF;AA4BO,SAAS,WACd,EAAA,EACuD;AACvD,EAAA,OAAO,CAAC,MAAA,KAAW;AACjB,IAAA,IAAI,MAAA,CAAO,IAAI,OAAO,MAAA;AACtB,IAAA,MAAM,GAAA,GAAM,EAAA,CAAG,MAAA,CAAO,KAAK,CAAA;AAC3B,IAAA,IAAI,eAAe,OAAA,EAAS,OAAO,GAAA,CAAI,IAAA,CAAK,MAAM,MAAM,CAAA;AACxD,IAAA,OAAO,MAAA;AAAA,EACT,CAAA;AACF;AAwBO,SAAS,WACd,EAAA,EACqD;AACrD,EAAA,OAAO,CAAC,MAAA,KAAW,MAAA,CAAO,MAAA,EAAQ,EAAE,CAAA;AACtC","file":"index.mjs","sourcesContent":["import { type Result, ok, err } from '@/result';\n\nimport type { MaybeAsync } from '@/composition';\n\n// ─── Types & Constructors ───────────────────────────────────\n\n/**\n * Symbol used to identify Option objects.\n *\n * @internal\n */\nexport const OPTION_BRAND = Symbol('OPTION_BRAND');\n\n/**\n * Represents an Option type.\n *\n * @example\n * const some: Option<number> = some(123);\n * const none: Option<number> = none();\n *\n * @param T - The type of the value contained in the Some variant\n * @returns An Option containing a value or nothing\n */\nexport type Option<T> =\n | {\n readonly some: true;\n readonly value: T;\n readonly [OPTION_BRAND]: 'some';\n }\n | {\n readonly some: false;\n readonly [OPTION_BRAND]: 'none';\n };\n\n/**\n * Creates an Option containing a value.\n *\n * @param value - The value to contain\n * @returns An Option containing the value\n */\nexport function some<T>(value: T): Option<T> {\n return {\n some: true,\n value,\n [OPTION_BRAND]: 'some',\n };\n}\n\n/**\n * Creates an Option containing nothing.\n *\n * @example\n * const some: Option<number> = some(123);\n * const none: Option<number> = none();\n *\n * @returns An Option containing nothing\n */\nexport function none<T = never>(): Option<T> {\n return {\n some: false,\n [OPTION_BRAND]: 'none',\n };\n}\n\n// ─── Type Guards ────────────────────────────────────────────\n\n/**\n * Type guard that checks if an Option is a Some variant containing a value.\n *\n * @example\n * const option: Option<number> = some(123);\n * if (isSome(option)) {\n * // TypeScript knows option.value exists here\n * console.log(option.value);\n * }\n *\n * @param option - The option to check\n * @returns A type predicate indicating if the option is a Some variant\n */\nexport function isSome<T>(option: Option<T>): option is {\n readonly some: true;\n readonly value: T;\n readonly [OPTION_BRAND]: 'some';\n} {\n return option.some;\n}\n\n/**\n * Type guard that checks if an Option is a None variant containing no value.\n *\n * @example\n * const option: Option<number> = none();\n * if (isNone(option)) {\n * // TypeScript knows this option has no value\n * // Attempting to access option.value would be a type error\n * }\n *\n * @param option - The option to check\n * @returns A type predicate indicating if the option is a None variant\n */\nexport function isNone<T>(option: Option<T>): option is {\n readonly some: false;\n readonly [OPTION_BRAND]: 'none';\n} {\n return !option.some;\n}\n\n// ─── Transformations ────────────────────────────────────────\n\n/**\n * Maps the value inside an Option using a transformation function.\n *\n * @example\n * const option: Option<number> = some(123);\n * const transformed: Option<string> = map(option, (value) => value.toString());\n *\n * @param option - The Option to transform\n * @param fn - The function to apply to the contained value\n * @returns A new Option containing the transformed value, or none if the input was none\n */\nexport function map<T, U>(option: Option<T>, fn: (value: T) => U): Option<U> {\n return option.some ? some(fn(option.value)) : none();\n}\n\n/**\n * Maps the value inside an Option using a transformation function that returns an Option.\n *\n * @example\n * const option: Option<number> = some(123);\n * const transformed: Option<string> = flatMap(option, (value) => some(value.toString()));\n *\n * @param option - The Option to transform\n * @param fn - The function to apply to the contained value, returning an Option\n * @returns The Option returned by the transformation function, or none if the input was none\n */\nexport function flatMap<T, U>(option: Option<T>, fn: (value: T) => Option<U>): Option<U> {\n return option.some ? fn(option.value) : none();\n}\n\n/**\n * Maps both the Some and None branches of an Option using separate transformation functions.\n * This is similar to combining a match with flatMap: it lets you decide the resulting Option in both cases.\n *\n * @example\n * const o1 = some(123);\n * const r1 = bimap(o1, (n) => some(n.toString()), () => none()); // some(\"123\")\n *\n * const o2 = none<number>();\n * const r2 = bimap(o2, (n) => some(n.toString()), () => some('fallback')); // some(\"fallback\")\n *\n * @param option - The Option to transform\n * @param someFn - The function to apply if the Option is Some, returning a new Option\n * @param noneFn - The function to apply if the Option is None, returning a new Option\n * @returns The Option returned by the appropriate transformation function\n */\nexport function bimap<T, U>(option: Option<T>, someFn: (value: T) => Option<U>, noneFn: () => Option<U>): Option<U> {\n return option.some ? someFn(option.value) : noneFn();\n}\n\n/**\n * Filters an Option based on a predicate function.\n *\n * @example\n * const option: Option<number> = some(123);\n * const filtered: Option<number> = filter(option, value => value > 100); // still Some(123)\n * const filtered2: Option<number> = filter(option, value => value < 100); // None\n *\n * @param option - The Option to filter\n * @param predicate - A function that determines if the value should be kept\n * @returns The original Option if it contains a value that satisfies the predicate, otherwise None\n */\nexport function filter<T>(option: Option<T>, predicate: (value: T) => boolean): Option<T> {\n return option.some && predicate(option.value) ? option : none();\n}\n\n// ─── Side Effects ───────────────────────────────────────────\n\n/**\n * Executes a callback with the value if the Option is Some, without changing the Option.\n * Useful for side effects like logging while maintaining a processing chain.\n *\n * @example\n * const result = pipe(\n * some(123),\n * opt => map(opt, x => x * 2),\n * opt => tap(opt, x => console.log(`Value: ${x}`)), // Logs but doesn't change the Option\n * opt => filter(opt, x => x > 200)\n * );\n *\n * @param option - The Option to tap into\n * @param fn - The function to execute with the value if Some\n * @returns The original Option unchanged\n */\nexport function tap<T>(option: Option<T>, fn: (value: T) => void): Option<T> {\n if (isSome(option)) {\n const callback = fn;\n callback(option.value);\n }\n return option;\n}\n\n// ─── Pattern Matching ───────────────────────────────────────\n\n/**\n * Pattern matches on an Option to handle both Some and None cases.\n *\n * @example\n * const option = some(42);\n * const result = match(option, {\n * some: (value) => `Got value: ${value}`,\n * none: () => \"Got nothing\"\n * }); // \"Got value: 42\"\n *\n * const empty = none<number>();\n * const result2 = match(empty, {\n * some: (value) => `Got value: ${value}`,\n * none: () => \"Got nothing\"\n * }); // \"Got nothing\"\n *\n * @param option - The Option to match against\n * @param patterns - An object containing handler functions for Some and None cases\n * @returns The result of calling the appropriate handler function\n */\nexport function match<T, R1, R2>(\n option: Option<T>,\n patterns: {\n some: (value: T) => R1;\n none: () => R2;\n },\n): R1 | R2 {\n if (isSome(option)) {\n const someFn = patterns.some;\n return someFn(option.value);\n } else {\n const noneFn = patterns.none;\n return noneFn();\n }\n}\n\n// ─── Unwrap ─────────────────────────────────────────────────\n\n/**\n * Unwraps the value inside an Option, throwing an error if the Option is none.\n *\n * @remarks\n * This function is intended for prototyping only and should not be used in production.\n * In production code, prefer using pattern matching or `unwrapOr` to handle both Some and None cases safely.\n *\n * @example\n * const option: Option<number> = some(123);\n * const value: number = unwrap(option);\n *\n * @param option - The Option to unwrap\n * @param [errorMsg] - Optional custom error message to throw when the Option is none\n * @returns The contained value\n */\nexport function unwrap<T>(option: Option<T>, errorMsg?: string): T {\n if (option.some) {\n return option.value;\n } else {\n throw new Error(errorMsg || 'Cannot unwrap a none Option');\n }\n}\n\n/**\n * Unwraps the value inside an Option, providing a default value if the Option is none.\n *\n * @example\n * const option: Option<number> = none();\n * const value: number = unwrapOr(option, 123);\n *\n * @param option - The Option to unwrap\n * @param defaultValue - The value to return if the Option is none\n * @returns The contained value, or the default value if the Option is none\n */\nexport function unwrapOr<T>(option: Option<T>, defaultValue: T): T {\n return option.some ? option.value : defaultValue;\n}\n\n/**\n * Unwraps the value inside an Option, calling a function to generate a default value if the Option is none.\n * Unlike unwrapOr, this only computes the default when needed.\n *\n * @example\n * const option: Option<number> = none();\n * const value = unwrapOrElse(option, () => expensiveComputation());\n *\n * @param option - The Option to unwrap\n * @param defaultFn - A function that returns a default value if the Option is none\n * @returns The contained value, or the result of calling defaultFn if the Option is none\n */\nexport function unwrapOrElse<T>(option: Option<T>, defaultFn: () => T): T {\n return option.some ? option.value : defaultFn();\n}\n\n// ─── Combining ──────────────────────────────────────────────\n\n/**\n * Combines an array of Options into a single Option containing an array of values.\n * Returns None if any Option in the array is None.\n *\n * @example\n * const options = [some(1), some(2), some(3)];\n * const combined = combine(options); // some([1, 2, 3])\n *\n * const withNone = [some(1), none(), some(3)];\n * const result = combine(withNone); // none()\n *\n * @param options - An array of Options to combine\n * @returns An Option containing an array of all values if all inputs are Some, or None if any input is None\n */\n\n// Tuple-preserving overloads\nexport function combine<T1>(options: readonly [Option<T1>]): Option<[T1]>;\nexport function combine<T1, T2>(options: readonly [Option<T1>, Option<T2>]): Option<[T1, T2]>;\nexport function combine<T1, T2, T3>(options: readonly [Option<T1>, Option<T2>, Option<T3>]): Option<[T1, T2, T3]>;\nexport function combine<T1, T2, T3, T4>(\n options: readonly [Option<T1>, Option<T2>, Option<T3>, Option<T4>],\n): Option<[T1, T2, T3, T4]>;\nexport function combine<T1, T2, T3, T4, T5>(\n options: readonly [Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>],\n): Option<[T1, T2, T3, T4, T5]>;\nexport function combine<T1, T2, T3, T4, T5, T6>(\n options: readonly [Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>, Option<T6>],\n): Option<[T1, T2, T3, T4, T5, T6]>;\nexport function combine<T1, T2, T3, T4, T5, T6, T7>(\n options: readonly [Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>, Option<T6>, Option<T7>],\n): Option<[T1, T2, T3, T4, T5, T6, T7]>;\nexport function combine<T1, T2, T3, T4, T5, T6, T7, T8>(\n options: readonly [Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>, Option<T6>, Option<T7>, Option<T8>],\n): Option<[T1, T2, T3, T4, T5, T6, T7, T8]>;\nexport function combine<T1, T2, T3, T4, T5, T6, T7, T8, T9>(\n options: readonly [\n Option<T1>,\n Option<T2>,\n Option<T3>,\n Option<T4>,\n Option<T5>,\n Option<T6>,\n Option<T7>,\n Option<T8>,\n Option<T9>,\n ],\n): Option<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>;\nexport function combine<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(\n options: readonly [\n Option<T1>,\n Option<T2>,\n Option<T3>,\n Option<T4>,\n Option<T5>,\n Option<T6>,\n Option<T7>,\n Option<T8>,\n Option<T9>,\n Option<T10>,\n ],\n): Option<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>;\nexport function combine<T>(options: readonly Option<T>[]): Option<T[]>;\n\nexport function combine<T>(options: readonly Option<T>[]): Option<T[]> {\n const values: T[] = [];\n\n for (const option of options) {\n if (!option.some) return none();\n values.push(option.value);\n }\n\n return some(values);\n}\n\n// ─── Conversions ────────────────────────────────────────────\n\n/**\n * Creates an Option from a nullable value.\n *\n * @example\n * const option = fromNullable(123); // some(123)\n * const option = fromNullable(null); // none()\n *\n * @param value - The value to wrap in the Some variant\n * @returns An Option containing the value (Some) if the value is not null or undefined, or None if the value is null or undefined\n */\nexport const fromNullable = <T>(value: T | null | undefined): Option<T> => {\n return value === null || value === undefined ? none() : some(value);\n};\n\n/**\n * Converts an Option to a Result.\n * If the Option is Some, returns an Ok variant with the value.\n * If the Option is None, returns an Err variant with the provided error.\n *\n * @example\n * const result = mapToResult(some(123), 'Some error message');\n * if (isOk(result)) {\n * console.log('Value:', result.value);\n * } else {\n * console.error('Error:', result.error);\n * }\n *\n * @param option - The Option to convert\n * @param error - The error to return if the Option is None\n * @returns A Result containing either the value (Ok) or the error (Err)\n */\nexport function mapToResult<T, E>(option: Option<T>, error: E): Result<T, E> {\n return option.some ? ok(option.value) : err(error);\n}\n\n// ─── Curried / Point-Free ───────────────────────────────────\n\n/**\n * Curried version of `map`. Returns a function that transforms the Some value using `fn`,\n * passing through None unchanged.\n *\n * @example\n * const double = mapWith((n: number) => n * 2);\n *\n * const option: Option<number> = some(5);\n * const doubled: Option<number> = double(option); // some(10)\n *\n * const empty: Option<number> = none();\n * const stillNone: Option<number> = double(empty); // none()\n *\n * // Point-free in a pipe\n * const result = pipe(some(3), mapWith((n: number) => n + 1)); // some(4)\n *\n * @param fn - The function to apply to the Some value\n * @returns A function that takes an Option and returns a new Option with the Some value transformed\n */\nexport function mapWith<T, U>(fn: (value: T) => U): (option: Option<T>) => Option<U> {\n return (option) => map(option, fn);\n}\n\n/**\n * Curried version of `flatMap`. Returns a function that chains on a Some value using `fn`,\n * passing through None unchanged. Supports both sync and async step functions.\n *\n * @example\n * const safeParse = flatMapWith((s: string) => {\n * const n = parseInt(s, 10);\n * return isNaN(n) ? none() : some(n);\n * });\n *\n * const option: Option<string> = some(\"42\");\n * const parsed: Option<number> = safeParse(option); // some(42)\n *\n * const empty: Option<string> = none();\n * const stillNone: Option<number> = safeParse(empty); // none()\n *\n * // Async step function in a pipe\n * const fetchProfile = flatMapWith(async (id: number) => {\n * const profile = await getProfile(id);\n * return profile ? some(profile) : none();\n * });\n *\n * @param fn - The function to apply to the Some value, returning an Option or Promise<Option>\n * @returns A function that takes an Option and returns a (possibly async) Option\n */\nexport function flatMapWith<T, U>(fn: (value: T) => Promise<Option<U>>): (option: Option<T>) => Promise<Option<U>>;\nexport function flatMapWith<T, U>(fn: (value: T) => Option<U>): (option: Option<T>) => Option<U>;\nexport function flatMapWith<T, U>(\n fn: (value: T) => MaybeAsync<Option<U>>,\n): (option: Option<T>) => MaybeAsync<Option<U>> {\n return (option: Option<T>) => {\n if (!option.some) return option;\n return fn(option.value);\n };\n}\n\n/**\n * Curried version of `filter`. Returns a function that keeps a Some value matching the predicate,\n * or returns None if the predicate fails.\n *\n * @example\n * const isPositive = filterWith((n: number) => n > 0);\n *\n * const option: Option<number> = some(5);\n * const filtered: Option<number> = isPositive(option); // some(5)\n *\n * const negative: Option<number> = some(-1);\n * const gone: Option<number> = isPositive(negative); // none()\n *\n * // Point-free in a pipe\n * const result = pipe(some(10), filterWith((n: number) => n < 100)); // some(10)\n *\n * @param predicate - A function that determines if the Some value should be kept\n * @returns A function that takes an Option and returns the original Option or None\n */\nexport function filterWith<T>(predicate: (value: T) => boolean): (option: Option<T>) => Option<T> {\n return (option) => filter(option, predicate);\n}\n\n/**\n * Curried version of `tap`. Returns a function that performs a side effect on the Some value\n * without changing the Option. None values pass through untouched. Supports both sync and async\n * side effects.\n *\n * @example\n * const log = tapWith((n: number) => console.log(\"value:\", n));\n *\n * const option: Option<number> = some(5);\n * const same: Option<number> = log(option); // logs \"value: 5\", returns some(5)\n *\n * const empty: Option<number> = none();\n * const stillNone: Option<number> = log(empty); // no log, returns none()\n *\n * // Async side effect in a pipe\n * const audit = tapWith(async (user: User) => {\n * await saveAuditLog(user.id);\n * });\n *\n * @param fn - The side-effect function to execute on the Some value\n * @returns A function that takes an Option, performs the side effect if Some, and returns the original Option\n */\nexport function tapWith<T>(fn: (value: T) => Promise<void>): (option: Option<T>) => Promise<Option<T>>;\nexport function tapWith<T>(fn: (value: T) => void): (option: Option<T>) => Option<T>;\nexport function tapWith<T>(fn: (value: T) => MaybeAsync<void>): (option: Option<T>) => MaybeAsync<Option<T>> {\n return (option: Option<T>) => {\n if (!option.some) return option;\n const out = fn(option.value);\n if (out instanceof Promise) return out.then(() => option);\n return option;\n };\n}\n","import { none, some, type Option } from '@/option';\n\nimport type { MaybeAsync } from '@/composition';\n\n// ─── Types & Constructors ───────────────────────────────────\n\n/**\n * Symbol used to identify Result objects.\n *\n * @internal\n */\nexport const RESULT_BRAND = Symbol('RESULT_BRAND');\n\n/**\n * Represents a Result type.\n *\n * @example\n * const result: Result<number, string> = ok(123);\n * const error: Result<number, string> = error(\"An error occurred\");\n *\n * @param T - The type of the value contained in the Ok variant\n * @param E - The type of the error contained in the Error variant\n * @returns A Result containing a value or an error\n */\nexport type Result<T, E> =\n | {\n readonly ok: true;\n readonly value: T;\n readonly [RESULT_BRAND]: 'ok';\n }\n | {\n readonly ok: false;\n readonly error: E;\n readonly [RESULT_BRAND]: 'error';\n };\n\n/**\n * Creates a Result containing a value.\n *\n * @example\n * const result: Result<number, string> = ok(123);\n *\n * @param value - The value to contain\n * @returns A Result containing the value\n */\nexport function ok<T, E = never>(value: T): Result<T, E> {\n return {\n ok: true,\n value,\n [RESULT_BRAND]: 'ok',\n };\n}\n\n/**\n * Creates a Result containing an error.\n *\n * @example\n * const error: Result<number, string> = err(\"An error occurred\");\n *\n * @param error - The error to contain\n * @returns A Result containing the error\n */\nexport function err<E>(error: E): Result<never, E> {\n return { ok: false, error, [RESULT_BRAND]: 'error' };\n}\n\n// ─── Type Guards ────────────────────────────────────────────\n\n/**\n * Type guard that checks if a Result is an Ok variant containing a value.\n *\n * @example\n * const result: Result<number, string> = ok(123);\n * if (isOk(result)) {\n * // TypeScript knows result.value exists here\n * console.log(result.value);\n * }\n *\n * @param result - The result to check\n * @returns A type predicate indicating if the result is an Ok variant\n */\nexport function isOk<T, E>(\n result: Result<T, E>,\n): result is {\n readonly ok: true;\n readonly value: T;\n readonly [RESULT_BRAND]: 'ok';\n} {\n return result.ok;\n}\n\n/**\n * Type guard that checks if a Result is an Error variant.\n *\n * @example\n * const result: Result<number, string> = err(\"error\");\n * if (isErr(result)) {\n * // TypeScript knows result.error exists here\n * console.log(result.error);\n * }\n *\n * @param result - The result to check\n * @returns A type predicate indicating if the result is an Error variant\n */\nexport function isErr<T, E>(\n result: Result<T, E>,\n): result is {\n readonly ok: false;\n readonly error: E;\n readonly [RESULT_BRAND]: 'error';\n} {\n return !result.ok;\n}\n\n// ─── Transformations ────────────────────────────────────────\n\n/**\n * Maps the value inside a Result using a transformation function.\n *\n * @example\n * const result: Result<number, string> = ok(123);\n * const transformed: Result<string, string> = map(result, (value) => value.toString());\n *\n * @param result - The Result to transform\n * @param fn - The function to apply to the contained value\n * @returns A new Result containing the transformed value, or the original error if the input was an error\n */\nexport function map<T, E, U>(result: Result<T, E>, fn: (value: T) => U): Result<U, E> {\n return result.ok ? ok(fn(result.value)) : result;\n}\n\n/**\n * Maps the value inside a Result using a transformation function that returns a Result.\n *\n * @example\n * const result: Result<number, string> = ok(123);\n * const transformed: Result<string, string> = flatMap(result, (value) => ok(value.toString()));\n *\n * @param result - The Result to transform\n * @param fn - The function to apply to the contained value, returning a Result\n * @returns The Result returned by the transformation function, or the original error if the input was an error\n */\nexport function flatMap<T, U, E1, E2>(result: Result<T, E1>, fn: (value: T) => Result<U, E2>): Result<U, E1 | E2> {\n return result.ok ? fn(result.value) : result;\n}\n\n/**\n * Maps the error inside a Result using a transformation function.\n *\n * @example\n * const result: Result<number, string> = err(\"error\");\n * const transformed: Result<number, Error> = mapErr(result, (err) => new Error(err));\n *\n * @param result - The Result to transform\n * @param fn - The function to apply to the contained error\n * @returns A new Result containing the transformed error, or the original value if the input was ok\n */\nexport function mapErr<T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F> {\n return result.ok ? result : err(fn(result.error));\n}\n\n/**\n * Maps both the value and error of a Result using separate transformation functions.\n * This is equivalent to chaining map and mapErr, but more concise.\n *\n * @example\n * const result: Result<number, string> = ok(123);\n * const transformed = bimap(\n * result,\n * (value) => value.toString(),\n * (error) => new Error(error)\n * ); // ok(\"123\")\n *\n * const error: Result<number, string> = err(\"failed\");\n * const transformed2 = bimap(\n * error,\n * (value) => value.toString(),\n * (error) => new Error(error)\n * ); // err(Error(\"failed\"))\n *\n * @param result - The Result to transform\n * @param okFn - The function to apply to the contained value if Ok\n * @param errFn - The function to apply to the contained error if Err\n * @returns A new Result with both branches transformed\n */\nexport function bimap<T, E, U, F>(result: Result<T, E>, okFn: (value: T) => U, errFn: (error: E) => F): Result<U, F> {\n return result.ok ? ok(okFn(result.value)) : err(errFn(result.error));\n}\n\n/**\n * Filters a Result based on a predicate function.\n *\n * @example\n * const result: Result<number, string> = ok(123);\n * const filtered: Result<number, string> = filter(result, value => value > 100, \"Value too small\"); // still Ok(123)\n * const filtered2: Result<number, string> = filter(result, value => value > 200, \"Value too small\"); // Err(\"Value too small\")\n *\n * @param result - The Result to filter\n * @param predicate - A function that determines if the value should be kept\n * @param error - The error to return if the predicate fails\n * @returns The original Result if it contains a value that satisfies the predicate, otherwise an Error\n */\nexport function filter<T, E1, E2>(\n result: Result<T, E1>,\n predicate: (value: T) => boolean,\n error: E2,\n): Result<T, E1 | E2> {\n if (!result.ok) return result;\n return predicate(result.value) ? result : err(error);\n}\n\n// ─── Side Effects ───────────────────────────────────────────\n\n/**\n * Executes a callback with the value if the Result is Ok, without changing the Result.\n * Useful for side effects like logging while maintaining a processing chain.\n *\n * @example\n * const finalResult = pipe(\n * ok(123),\n * r => map(r, x => x * 2),\n * r => tap(r, x => console.log(`Value: ${x}`)), // Logs but doesn't change the Result\n * r => filter(r, x => x > 200, \"Value too small\")\n * );\n *\n * @param result - The Result to tap into\n * @param fn - The function to execute with the value if Ok\n * @returns The original Result unchanged\n */\nexport function tap<T, E>(result: Result<T, E>, fn: (value: T) => void): Result<T, E> {\n if (isOk(result)) {\n const callback = fn;\n callback(result.value);\n }\n return result;\n}\n\n/**\n * Executes a callback with the error if the Result is an Error, without changing the Result.\n * Useful for side effects like logging while maintaining a processing chain.\n *\n * @example\n * const finalResult = pipe(\n * err(\"something went wrong\"),\n * r => tapErr(r, e => console.error(`Error: ${e}`)), // Logs but doesn't change the Result\n * r => mapErr(r, e => new Error(e))\n * );\n *\n * @param result - The Result to tap into\n * @param fn - The function to execute with the error if Error\n * @returns The original Result unchanged\n */\nexport function tapErr<T, E>(result: Result<T, E>, fn: (error: E) => void): Result<T, E> {\n if (isErr(result)) {\n const callback = fn;\n callback(result.error);\n }\n return result;\n}\n\n// ─── Error Recovery ─────────────────────────────────────────\n\n/**\n * Recovers from an Err by applying `fn` to the error value. Returns the original Ok unchanged.\n * This is the error-channel equivalent of `flatMap`.\n *\n * @example\n * const result: Result<number, string> = err(\"not found\");\n * const recovered = orElse(result, (e) => ok(e.length)); // ok(9)\n *\n * const okResult: Result<number, string> = ok(42);\n * const kept = orElse(okResult, (e) => ok(e.length)); // ok(42)\n *\n * @param result - The Result to recover from\n * @param fn - A function that receives the Err value and returns a fallback Result\n * @returns The original Ok, or the Result of applying fn to the error\n */\nexport function orElse<T1, E1, T2, E2>(result: Result<T1, E1>, fn: (error: E1) => Result<T2, E2>): Result<T1 | T2, E2> {\n return result.ok ? result : fn(result.error);\n}\n\n// ─── Pattern Matching ───────────────────────────────────────\n\n/**\n * Pattern matches on a Result to handle both Ok and Error cases.\n *\n * @example\n * const result = ok(42);\n * const message = match(result, {\n * ok: (value) => `Got value: ${value}`,\n * err: (error) => `Got error: ${error}`\n * }); // \"Got value: 42\"\n *\n * @param result - The Result to match against\n * @param patterns - An object containing handler functions for Ok and Error cases\n * @returns The result of calling the appropriate handler function\n */\nexport function match<T, E, R1, R2>(\n result: Result<T, E>,\n patterns: {\n ok: (value: T) => R1;\n err: (error: E) => R2;\n },\n): R1 | R2 {\n if (isOk(result)) {\n const okFn = patterns.ok;\n return okFn(result.value);\n } else {\n const errFn = patterns.err;\n return errFn(result.error);\n }\n}\n\n// ─── Unwrap ─────────────────────────────────────────────────\n\n/**\n * Unwraps the value inside a Result, throwing an error if the Result is an error.\n *\n * @remarks\n * This function is intended for prototyping only and should not be used in production.\n * In production code, prefer using pattern matching or `unwrapOr` to handle both Ok and Error cases safely.\n *\n * @example\n * const result: Result<number, string> = ok(123);\n * const value: number = unwrap(result);\n *\n * // With custom error message\n * try {\n * const badResult: Result<number, string> = err(\"Invalid data\");\n * unwrap(badResult, \"Custom error message\");\n * } catch (error) {\n * // Error will contain \"Custom error message\" instead of the default\n * }\n *\n * @param result - The Result to unwrap\n * @param errorMsg - Optional custom error message to use if Result is an error\n * @returns The contained value\n * @throws If the Result is an error\n */\nexport function unwrap<T, E>(result: Result<T, E>, errorMsg?: string): T {\n if (result.ok) {\n return result.value;\n } else {\n throw new Error(errorMsg || `Cannot unwrap an error Result: ${String(result.error)}`);\n }\n}\n\n/**\n * Unwraps the value inside a Result, providing a default value if the Result is an error.\n *\n * @example\n * const result: Result<number, string> = err(\"error\");\n * const value: number = unwrapOr(result, 123);\n *\n * @param result - The Result to unwrap\n * @param defaultValue - The value to return if the Result is an error\n * @returns The contained value, or the default value if the Result is an error\n */\nexport function unwrapOr<T, E>(result: Result<T, E>, defaultValue: T): T {\n return result.ok ? result.value : defaultValue;\n}\n\n/**\n * Unwraps the value inside a Result, calling a function to generate a default value if the Result is an error.\n * Unlike unwrapOr, this only computes the default when needed.\n *\n * @example\n * const result: Result<number, string> = err(\"error\");\n * const value = unwrapOrElse(result, () => expensiveComputation());\n *\n * @param result - The Result to unwrap\n * @param defaultFn - A function that returns a default value if the Result is an error\n * @returns The contained value, or the result of calling defaultFn if the Result is an error\n */\nexport function unwrapOrElse<T, E>(result: Result<T, E>, defaultFn: () => T): T {\n return result.ok ? result.value : defaultFn();\n}\n\n// ─── Combining ──────────────────────────────────────────────\n\n/**\n * Combines an array of Results into a single Result containing an array of values.\n * Returns an Error if any Result in the array is an Error, using the first encountered error.\n * If an empty array is provided, returns `ok([])` (an Ok containing an empty array).\n *\n * @example\n * const results = [ok(1), ok(2), ok(3)];\n * const combined = combine(results); // ok([1, 2, 3])\n *\n * const withError = [ok(1), err(\"error\"), ok(3)];\n * const result = combine(withError); // err(\"error\")\n *\n * const emptyResults = [];\n * const emptyResult = combine(emptyResults); // ok([])\n *\n * @param results - An array of Results to combine\n * @returns A Result containing an array of all values if all inputs are Ok, or the first Error if any input is an Error\n */\n\n// Tuple-preserving overloads with error type unions\nexport function combine<T1, E1>(results: readonly [Result<T1, E1>]): Result<[T1], E1>;\nexport function combine<T1, E1, T2, E2>(results: readonly [Result<T1, E1>, Result<T2, E2>]): Result<[T1, T2], E1 | E2>;\nexport function combine<T1, E1, T2, E2, T3, E3>(\n results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>],\n): Result<[T1, T2, T3], E1 | E2 | E3>;\nexport function combine<T1, E1, T2, E2, T3, E3, T4, E4>(\n results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>, Result<T4, E4>],\n): Result<[T1, T2, T3, T4], E1 | E2 | E3 | E4>;\nexport function combine<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5>(\n results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>, Result<T4, E4>, Result<T5, E5>],\n): Result<[T1, T2, T3, T4, T5], E1 | E2 | E3 | E4 | E5>;\nexport function combine<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6>(\n results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>, Result<T4, E4>, Result<T5, E5>, Result<T6, E6>],\n): Result<[T1, T2, T3, T4, T5, T6], E1 | E2 | E3 | E4 | E5 | E6>;\nexport function combine<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7>(\n results: readonly [\n Result<T1, E1>,\n Result<T2, E2>,\n Result<T3, E3>,\n Result<T4, E4>,\n Result<T5, E5>,\n Result<T6, E6>,\n Result<T7, E7>,\n ],\n): Result<[T1, T2, T3, T4, T5, T6, T7], E1 | E2 | E3 | E4 | E5 | E6 | E7>;\nexport function combine<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7, T8, E8>(\n results: readonly [\n Result<T1, E1>,\n Result<T2, E2>,\n Result<T3, E3>,\n Result<T4, E4>,\n Result<T5, E5>,\n Result<T6, E6>,\n Result<T7, E7>,\n Result<T8, E8>,\n ],\n): Result<[T1, T2, T3, T4, T5, T6, T7, T8], E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8>;\nexport function combine<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7, T8, E8, T9, E9>(\n results: readonly [\n Result<T1, E1>,\n Result<T2, E2>,\n Result<T3, E3>,\n Result<T4, E4>,\n Result<T5, E5>,\n Result<T6, E6>,\n Result<T7, E7>,\n Result<T8, E8>,\n Result<T9, E9>,\n ],\n): Result<[T1, T2, T3, T4, T5, T6, T7, T8, T9], E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8 | E9>;\nexport function combine<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7, T8, E8, T9, E9, T10, E10>(\n results: readonly [\n Result<T1, E1>,\n Result<T2, E2>,\n Result<T3, E3>,\n Result<T4, E4>,\n Result<T5, E5>,\n Result<T6, E6>,\n Result<T7, E7>,\n Result<T8, E8>,\n Result<T9, E9>,\n Result<T10, E10>,\n ],\n): Result<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10], E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8 | E9 | E10>;\nexport function combine<T, E>(results: readonly Result<T, E>[]): Result<T[], E>;\nexport function combine<T, E>(results: readonly Result<T, E>[]): Result<T[], E> {\n const values: T[] = [];\n\n for (const result of results) {\n if (!result.ok) return result;\n values.push(result.value);\n }\n\n return ok(values);\n}\n\n/**\n * Combines an array of Results into a single Result containing an array of values.\n * Unlike combine, this collects all errors instead of returning just the first one.\n * If all Results are Ok, returns an Ok containing an array of all values.\n * If any Results are Error, returns an Error containing an array of all errors.\n *\n * @example\n * const results = [ok(1), ok(2), ok(3)];\n * const combined = combineAll(results); // ok([1, 2, 3])\n *\n * const withErrors = [ok(1), err(\"error1\"), ok(3), err(\"error2\")];\n * const result = combineAll(withErrors); // err([\"error1\", \"error2\"])\n *\n * const emptyResults = [];\n * const emptyResult = combineAll(emptyResults); // ok([])\n *\n * @param results - An array of Results to combine\n * @returns A Result containing an array of all values if all inputs are Ok, or an array of all errors if any input is an Error\n */\n\n// Tuple-preserving overloads with error arrays\nexport function combineAll<T1, E1>(results: readonly [Result<T1, E1>]): Result<[T1], E1[]>;\nexport function combineAll<T1, E1, T2, E2>(\n results: readonly [Result<T1, E1>, Result<T2, E2>],\n): Result<[T1, T2], (E1 | E2)[]>;\nexport function combineAll<T1, E1, T2, E2, T3, E3>(\n results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>],\n): Result<[T1, T2, T3], (E1 | E2 | E3)[]>;\nexport function combineAll<T1, E1, T2, E2, T3, E3, T4, E4>(\n results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>, Result<T4, E4>],\n): Result<[T1, T2, T3, T4], (E1 | E2 | E3 | E4)[]>;\nexport function combineAll<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5>(\n results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>, Result<T4, E4>, Result<T5, E5>],\n): Result<[T1, T2, T3, T4, T5], (E1 | E2 | E3 | E4 | E5)[]>;\nexport function combineAll<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6>(\n results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>, Result<T4, E4>, Result<T5, E5>, Result<T6, E6>],\n): Result<[T1, T2, T3, T4, T5, T6], (E1 | E2 | E3 | E4 | E5 | E6)[]>;\nexport function combineAll<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7>(\n results: readonly [\n Result<T1, E1>,\n Result<T2, E2>,\n Result<T3, E3>,\n Result<T4, E4>,\n Result<T5, E5>,\n Result<T6, E6>,\n Result<T7, E7>,\n ],\n): Result<[T1, T2, T3, T4, T5, T6, T7], (E1 | E2 | E3 | E4 | E5 | E6 | E7)[]>;\nexport function combineAll<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7, T8, E8>(\n results: readonly [\n Result<T1, E1>,\n Result<T2, E2>,\n Result<T3, E3>,\n Result<T4, E4>,\n Result<T5, E5>,\n Result<T6, E6>,\n Result<T7, E7>,\n Result<T8, E8>,\n ],\n): Result<[T1, T2, T3, T4, T5, T6, T7, T8], (E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8)[]>;\nexport function combineAll<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7, T8, E8, T9, E9>(\n results: readonly [\n Result<T1, E1>,\n Result<T2, E2>,\n Result<T3, E3>,\n Result<T4, E4>,\n Result<T5, E5>,\n Result<T6, E6>,\n Result<T7, E7>,\n Result<T8, E8>,\n Result<T9, E9>,\n ],\n): Result<[T1, T2, T3, T4, T5, T6, T7, T8, T9], (E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8 | E9)[]>;\nexport function combineAll<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7, T8, E8, T9, E9, T10, E10>(\n results: readonly [\n Result<T1, E1>,\n Result<T2, E2>,\n Result<T3, E3>,\n Result<T4, E4>,\n Result<T5, E5>,\n Result<T6, E6>,\n Result<T7, E7>,\n Result<T8, E8>,\n Result<T9, E9>,\n Result<T10, E10>,\n ],\n): Result<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10], (E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8 | E9 | E10)[]>;\nexport function combineAll<T, E>(results: readonly Result<T, E>[]): Result<T[], E[]>;\nexport function combineAll<T, E>(results: readonly Result<T, E>[]): Result<T[], E[]> {\n const errors: E[] = [];\n const values: T[] = [];\n\n for (const result of results) {\n if (result.ok) {\n values.push(result.value);\n } else {\n errors.push(result.error);\n }\n }\n\n return errors.length > 0 ? err(errors) : ok(values);\n}\n\n// ─── Conversions ────────────────────────────────────────────\n\n/**\n * Converts a Result to an Option.\n * If the Result is Ok, returns a Some variant with the value.\n * If the Result is an Error, returns a None variant.\n *\n * @example\n * import { some, none } from \"@/option\";\n *\n * const option1 = mapToOption(ok(123)); // some(123)\n * const option2 = mapToOption(err(\"error\")); // none()\n *\n * @param result - The Result to convert\n * @returns An Option containing the value if Ok, or None if Error\n */\nexport function mapToOption<T, E>(result: Result<T, E>): Option<T> {\n return result.ok ? some(result.value) : none();\n}\n\n/**\n * Safely executes a function that might throw and converts the result into a Result type.\n * If the function executes successfully, returns an Ok variant with the return value.\n * If the function throws an error, returns an Error variant containing the error message as a string.\n *\n * @example\n * const parseJson = (str: string) => fromTry(() => JSON.parse(str));\n *\n * const validResult = parseJson('{\"name\":\"John\"}'); // ok({ name: 'John' })\n * const invalidResult = parseJson('invalid json'); // err(\"Unexpected token...\")\n *\n * @param f - The function to execute\n * @returns A Result containing either the function's return value or the error message\n */\nexport function fromTry<T>(f: () => T): Result<T, string> {\n try {\n return ok(f());\n } catch (error) {\n return err(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Safely executes a function that might throw and converts the result into a Result type.\n * Unlike fromTry, this preserves the full Error object instead of just the message.\n * Use this when you need access to error stack traces or custom error properties.\n *\n * @example\n * const result = fromTryWithError(() => JSON.parse('invalid'));\n * if (isErr(result)) {\n * console.log(result.error.stack); // Access to full stack trace\n * }\n *\n * @param f - The function to execute\n * @returns A Result containing either the function's return value or the full Error object\n */\nexport function fromTryWithError<T>(f: () => T): Result<T, Error> {\n try {\n return ok(f());\n } catch (error) {\n return err(error instanceof Error ? error : new Error(String(error)));\n }\n}\n\n/**\n * Converts a Promise to a Result, capturing any errors that occur during promise resolution.\n * Returns error messages as strings for simplicity.\n *\n * @example\n * const result = await fromPromise(fetch('https://api.example.com/data'));\n * if (isErr(result)) {\n * console.log(result.error); // Error message as string\n * }\n *\n * @param promise - The Promise to convert\n * @returns A Promise that resolves to a Result with string error messages\n */\nexport async function fromPromise<T>(promise: Promise<T>): Promise<Result<T, string>> {\n try {\n const value = await promise;\n return ok(value);\n } catch (error) {\n return err(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Converts a Promise to a Result, preserving the full error object or custom error type.\n * Use this when you need access to error stack traces, custom properties, or specific error types.\n *\n * @example\n * // With default error handling (preserves full error)\n * const result = await fromPromiseWithError(fetch('https://api.example.com/data'));\n *\n * @example\n * // With specific error type and transformer\n * type ApiError = { code: number; message: string };\n *\n * const result = await fromPromiseWithError<Response, ApiError>(\n * fetch('https://api.example.com/data'),\n * (error) => ({\n * code: error instanceof Error ? 500 : 400,\n * message: error instanceof Error ? error.message : String(error)\n * })\n * );\n *\n * @param promise - The Promise to convert\n * @param errorFn - Optional function to transform caught errors to the expected error type\n * @returns A Promise that resolves to a Result\n */\nexport async function fromPromiseWithError<T, E = unknown>(\n promise: Promise<T>,\n errorFn: (error: unknown) => E = (error) => error as unknown as E,\n): Promise<Result<T, E>> {\n try {\n const value = await promise;\n return ok(value);\n } catch (error) {\n return err(errorFn(error));\n }\n}\n\n/**\n * Converts a Result to a Promise.\n *\n * @example\n * const result = ok(123);\n * const promise = toPromise(result);\n * const value = await promise; // 123\n *\n * @param result - The Result to convert\n * @returns A Promise that resolves to the value if Ok, or rejects with the error if Error\n */\nexport function toPromise<T, E>(result: Result<T, E>): Promise<T> {\n return result.ok ? Promise.resolve(result.value) : Promise.reject(result.error);\n}\n\n// ─── Curried / Point-Free ───────────────────────────────────\n\n/**\n * Curried version of `map`. Returns a function that transforms the Ok value using `fn`,\n * passing through any Err unchanged.\n *\n * @example\n * const double = mapWith((n: number) => n * 2);\n *\n * const result: Result<number, string> = ok(5);\n * const doubled: Result<number, string> = double(result); // ok(10)\n *\n * const errResult: Result<number, string> = err(\"fail\");\n * const stillErr: Result<number, string> = double(errResult); // err(\"fail\")\n *\n * // Point-free in a pipe\n * const result2 = pipe(ok(3), mapWith((n: number) => n + 1)); // ok(4)\n *\n * @param fn - The function to apply to the Ok value\n * @returns A function that takes a Result and returns a new Result with the Ok value transformed\n */\nexport function mapWith<T, U>(fn: (value: T) => U): <E>(result: Result<T, E>) => Result<U, E> {\n return (result) => map(result, fn);\n}\n\n/**\n * Curried version of `flatMap`. Returns a function that chains on an Ok value using `fn`,\n * passing through any Err unchanged. Supports both sync and async step functions.\n *\n * @example\n * const toStr = flatMapWith((n: number) => ok(n.toString()));\n *\n * const result: Result<number, string> = ok(5);\n * const chained: Result<string, string> = toStr(result); // ok(\"5\")\n *\n * const errResult: Result<number, string> = err(\"fail\");\n * const stillErr: Result<string, string> = toStr(errResult); // err(\"fail\")\n *\n * // Async step function in a pipe\n * const fetchUser = flatMapWith(async (id: number) => {\n * const user = await getUser(id);\n * return user ? ok(user) : err(\"not found\" as const);\n * });\n *\n * @param fn - The function to apply to the Ok value, returning a Result or Promise<Result>\n * @returns A function that takes a Result and returns a (possibly async) Result\n */\nexport function flatMapWith<T, U, E2>(\n fn: (value: T) => Promise<Result<U, E2>>,\n): <E1>(result: Result<T, E1>) => Promise<Result<U, E1 | E2>>;\nexport function flatMapWith<T, U, E2>(\n fn: (value: T) => Result<U, E2>,\n): <E1>(result: Result<T, E1>) => Result<U, E1 | E2>;\nexport function flatMapWith<T, U, E2>(\n fn: (value: T) => MaybeAsync<Result<U, E2>>,\n): <E1>(result: Result<T, E1>) => MaybeAsync<Result<U, E1 | E2>> {\n return (result) => {\n if (!result.ok) return result;\n return fn(result.value);\n };\n}\n\n/**\n * Curried version of `mapErr`. Returns a function that transforms the Err value using `fn`,\n * passing through any Ok unchanged.\n *\n * @example\n * const wrapError = mapErrWith((msg: string) => new Error(msg));\n *\n * const result: Result<number, string> = err(\"fail\");\n * const wrapped: Result<number, Error> = wrapError(result); // err(Error(\"fail\"))\n *\n * const okResult: Result<number, string> = ok(42);\n * const stillOk: Result<number, Error> = wrapError(okResult); // ok(42)\n *\n * // Point-free in a pipe\n * const result2 = pipe(err(\"oops\"), mapErrWith((s: string) => s.toUpperCase())); // err(\"OOPS\")\n *\n * @param fn - The function to apply to the Err value\n * @returns A function that takes a Result and returns a new Result with the Err value transformed\n */\nexport function mapErrWith<E, F>(fn: (error: E) => F): <T>(result: Result<T, E>) => Result<T, F> {\n return (result) => mapErr(result, fn);\n}\n\n/**\n * Curried version of `filter`. Returns a function that keeps an Ok value matching the predicate,\n * or returns the provided error if the predicate fails.\n *\n * @example\n * const isPositive = filterWith((n: number) => n > 0, \"must be positive\");\n *\n * const result: Result<number, string> = ok(5);\n * const filtered: Result<number, string> = isPositive(result); // ok(5)\n *\n * const zero: Result<number, string> = ok(-1);\n * const failed: Result<number, string> = isPositive(zero); // err(\"must be positive\")\n *\n * // Point-free in a pipe\n * const result2 = pipe(ok(10), filterWith((n: number) => n < 100, \"too large\")); // ok(10)\n *\n * @param predicate - A function that determines if the Ok value should be kept\n * @param error - The error to return if the predicate fails\n * @returns A function that takes a Result and returns the original Result or an Err\n */\nexport function filterWith<T, E2>(\n predicate: (value: T) => boolean,\n error: E2,\n): <E1>(result: Result<T, E1>) => Result<T, E1 | E2> {\n return (result) => filter(result, predicate, error);\n}\n\n/**\n * Curried version of `tap`. Returns a function that performs a side effect on the Ok value\n * without changing the Result. Err values pass through untouched. Supports both sync and async\n * side effects.\n *\n * @example\n * const log = tapWith((n: number) => console.log(\"value:\", n));\n *\n * const result: Result<number, string> = ok(5);\n * const same: Result<number, string> = log(result); // logs \"value: 5\", returns ok(5)\n *\n * const errResult: Result<number, string> = err(\"fail\");\n * const stillErr: Result<number, string> = log(errResult); // no log, returns err(\"fail\")\n *\n * // Async side effect in a pipe\n * const audit = tapWith(async (user: User) => {\n * await saveAuditLog(user.id);\n * });\n *\n * @param fn - The side-effect function to execute on the Ok value\n * @returns A function that takes a Result, performs the side effect if Ok, and returns the original Result\n */\nexport function tapWith<T>(fn: (value: T) => Promise<void>): <E>(result: Result<T, E>) => Promise<Result<T, E>>;\nexport function tapWith<T>(fn: (value: T) => void): <E>(result: Result<T, E>) => Result<T, E>;\nexport function tapWith<T>(fn: (value: T) => MaybeAsync<void>): <E>(result: Result<T, E>) => MaybeAsync<Result<T, E>> {\n return (result) => {\n if (!result.ok) return result;\n const out = fn(result.value);\n if (out instanceof Promise) return out.then(() => result);\n return result;\n };\n}\n\n/**\n * Curried version of `tapErr`. Returns a function that performs a side effect on the Err value\n * without changing the Result. Ok values pass through untouched.\n *\n * @example\n * const logErr = tapErrWith((msg: string) => console.error(\"error:\", msg));\n *\n * const errResult: Result<number, string> = err(\"fail\");\n * const same: Result<number, string> = logErr(errResult); // logs \"error: fail\", returns err(\"fail\")\n *\n * const okResult: Result<number, string> = ok(42);\n * const stillOk: Result<number, string> = logErr(okResult); // no log, returns ok(42)\n *\n * // Point-free in a pipe\n * const result = pipe(err(\"oops\"), tapErrWith((e: string) => reportError(e)));\n *\n * // Async side effect in a pipe\n * const audit = tapErrWith(async (error: string) => {\n * await logErrorToService(error);\n * });\n *\n * @param fn - The side-effect function to execute on the Err value\n * @returns A function that takes a Result, performs the side effect if Err, and returns the original Result\n */\nexport function tapErrWith<E>(fn: (error: E) => Promise<void>): <T>(result: Result<T, E>) => Promise<Result<T, E>>;\nexport function tapErrWith<E>(fn: (error: E) => void): <T>(result: Result<T, E>) => Result<T, E>;\nexport function tapErrWith<E>(\n fn: (error: E) => MaybeAsync<void>,\n): <T>(result: Result<T, E>) => MaybeAsync<Result<T, E>> {\n return (result) => {\n if (result.ok) return result;\n const out = fn(result.error);\n if (out instanceof Promise) return out.then(() => result);\n return result;\n };\n}\n\n/**\n * Curried version of `orElse`. Recovers from an Err by applying `fn` to the error value;\n * passes through Ok unchanged.\n *\n * @example\n * const recover = orElseWith((msg: string) => ok(msg.length));\n *\n * const errResult: Result<number, string> = err(\"fail\");\n * const recovered: Result<number, string> = recover(errResult); // ok(4)\n *\n * const okResult: Result<number, string> = ok(42);\n * const kept: Result<number, string> = recover(okResult); // ok(42)\n *\n * // Point-free in a pipe — recover using the error\n * const result = pipe(\n * err(\"not found\"),\n * orElseWith((e: string) => ok(`recovered from: ${e}`))\n * ); // ok(\"recovered from: not found\")\n *\n * @param fn - A function that receives the Err value and returns a fallback Result\n * @returns A function that takes a Result and returns either the original Ok or the result of calling fn\n */\nexport function orElseWith<E1, T2, E2>(\n fn: (error: E1) => Result<T2, E2>,\n): <T1>(result: Result<T1, E1>) => Result<T1 | T2, E2> {\n return (result) => orElse(result, fn);\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/option/option.ts","../../src/result/result.ts"],"names":[],"mappings":";AAWO,IAAM,YAAA,GAAe,OAAO,cAAc,CAAA;AA6B1C,SAAS,KAAQ,KAAA,EAAqB;AAC3C,EAAA,OAAO;AAAA,IACL,IAAA,EAAM,IAAA;AAAA,IACN,KAAA;AAAA,IACA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ACF;AA4BO,SAAS,WACd,EAAA,EACuD;AACvD,EAAA,OAAO,CAAC,MAAA,KAAW;AACjB,IAAA,IAAI,MAAA,CAAO,IAAI,OAAO,MAAA;AACtB,IAAA,MAAM,GAAA,GAAM,EAAA,CAAG,MAAA,CAAO,KAAK,CAAA;AAC3B,IAAA,IAAI,eAAe,OAAA,EAAS,OAAO,GAAA,CAAI,IAAA,CAAK,MAAM,MAAM,CAAA;AACxD,IAAA,OAAO,MAAA;AAAA,EACT,CAAA;AACF;AAwBO,SAAS,WACd,EAAA,EACqD;AACrD,EAAA,OAAO,CAAC,MAAA,KAAW,MAAA,CAAO,MAAA,EAAQ,EAAE,CAAA;AACtC","file":"index.mjs","sourcesContent":["import { type Result, ok, err } from '@/result';\n\nimport type { MaybeAsync } from '@/composition';\n\n// ─── Types & Constructors ───────────────────────────────────\n\n/**\n * Symbol used to identify Option objects.\n *\n * @internal\n */\nexport const OPTION_BRAND = Symbol('OPTION_BRAND');\n\n/**\n * Represents an Option type.\n *\n * @example\n * const some: Option<number> = some(123);\n * const none: Option<number> = none();\n *\n * @param T - The type of the value contained in the Some variant\n * @returns An Option containing a value or nothing\n */\nexport type Option<T> =\n | {\n readonly some: true;\n readonly value: T;\n readonly [OPTION_BRAND]: 'some';\n }\n | {\n readonly some: false;\n readonly [OPTION_BRAND]: 'none';\n };\n\n/**\n * Creates an Option containing a value.\n *\n * @param value - The value to contain\n * @returns An Option containing the value\n */\nexport function some<T>(value: T): Option<T> {\n return {\n some: true,\n value,\n [OPTION_BRAND]: 'some',\n };\n}\n\n/**\n * Creates an Option containing nothing.\n *\n * @example\n * const some: Option<number> = some(123);\n * const none: Option<number> = none();\n *\n * @returns An Option containing nothing\n */\nexport function none<T = never>(): Option<T> {\n return {\n some: false,\n [OPTION_BRAND]: 'none',\n };\n}\n\n// ─── Type Guards ────────────────────────────────────────────\n\n/**\n * Type guard that checks if an Option is a Some variant containing a value.\n *\n * @example\n * const option: Option<number> = some(123);\n * if (isSome(option)) {\n * // TypeScript knows option.value exists here\n * console.log(option.value);\n * }\n *\n * @param option - The option to check\n * @returns A type predicate indicating if the option is a Some variant\n */\nexport function isSome<T>(option: Option<T>): option is {\n readonly some: true;\n readonly value: T;\n readonly [OPTION_BRAND]: 'some';\n} {\n return option.some;\n}\n\n/**\n * Type guard that checks if an Option is a None variant containing no value.\n *\n * @example\n * const option: Option<number> = none();\n * if (isNone(option)) {\n * // TypeScript knows this option has no value\n * // Attempting to access option.value would be a type error\n * }\n *\n * @param option - The option to check\n * @returns A type predicate indicating if the option is a None variant\n */\nexport function isNone<T>(option: Option<T>): option is {\n readonly some: false;\n readonly [OPTION_BRAND]: 'none';\n} {\n return !option.some;\n}\n\n// ─── Transformations ────────────────────────────────────────\n\n/**\n * Maps the value inside an Option using a transformation function.\n *\n * @example\n * const option: Option<number> = some(123);\n * const transformed: Option<string> = map(option, (value) => value.toString());\n *\n * @param option - The Option to transform\n * @param fn - The function to apply to the contained value\n * @returns A new Option containing the transformed value, or none if the input was none\n */\nexport function map<T, U>(option: Option<T>, fn: (value: T) => U): Option<U> {\n return option.some ? some(fn(option.value)) : none();\n}\n\n/**\n * Maps the value inside an Option using a transformation function that returns an Option.\n *\n * @example\n * const option: Option<number> = some(123);\n * const transformed: Option<string> = flatMap(option, (value) => some(value.toString()));\n *\n * @param option - The Option to transform\n * @param fn - The function to apply to the contained value, returning an Option\n * @returns The Option returned by the transformation function, or none if the input was none\n */\nexport function flatMap<T, U>(option: Option<T>, fn: (value: T) => Option<U>): Option<U> {\n return option.some ? fn(option.value) : none();\n}\n\n/**\n * Maps both the Some and None branches of an Option using separate transformation functions.\n * This is similar to combining a match with flatMap: it lets you decide the resulting Option in both cases.\n *\n * @example\n * const o1 = some(123);\n * const r1 = bimap(o1, (n) => some(n.toString()), () => none()); // some(\"123\")\n *\n * const o2 = none<number>();\n * const r2 = bimap(o2, (n) => some(n.toString()), () => some('fallback')); // some(\"fallback\")\n *\n * @param option - The Option to transform\n * @param someFn - The function to apply if the Option is Some, returning a new Option\n * @param noneFn - The function to apply if the Option is None, returning a new Option\n * @returns The Option returned by the appropriate transformation function\n */\nexport function bimap<T, U>(option: Option<T>, someFn: (value: T) => Option<U>, noneFn: () => Option<U>): Option<U> {\n return option.some ? someFn(option.value) : noneFn();\n}\n\n/**\n * Filters an Option based on a predicate function.\n *\n * @example\n * const option: Option<number> = some(123);\n * const filtered: Option<number> = filter(option, value => value > 100); // still Some(123)\n * const filtered2: Option<number> = filter(option, value => value < 100); // None\n *\n * @param option - The Option to filter\n * @param predicate - A function that determines if the value should be kept\n * @returns The original Option if it contains a value that satisfies the predicate, otherwise None\n */\nexport function filter<T>(option: Option<T>, predicate: (value: T) => boolean): Option<T> {\n return option.some && predicate(option.value) ? option : none();\n}\n\n// ─── Side Effects ───────────────────────────────────────────\n\n/**\n * Executes a callback with the value if the Option is Some, without changing the Option.\n * Useful for side effects like logging while maintaining a processing chain.\n *\n * @example\n * const result = pipe(\n * some(123),\n * opt => map(opt, x => x * 2),\n * opt => tap(opt, x => console.log(`Value: ${x}`)), // Logs but doesn't change the Option\n * opt => filter(opt, x => x > 200)\n * );\n *\n * @param option - The Option to tap into\n * @param fn - The function to execute with the value if Some\n * @returns The original Option unchanged\n */\nexport function tap<T>(option: Option<T>, fn: (value: T) => void): Option<T> {\n if (isSome(option)) {\n const callback = fn;\n callback(option.value);\n }\n return option;\n}\n\n// ─── Pattern Matching ───────────────────────────────────────\n\n/**\n * Pattern matches on an Option to handle both Some and None cases.\n *\n * @example\n * const option = some(42);\n * const result = match(option, {\n * some: (value) => `Got value: ${value}`,\n * none: () => \"Got nothing\"\n * }); // \"Got value: 42\"\n *\n * const empty = none<number>();\n * const result2 = match(empty, {\n * some: (value) => `Got value: ${value}`,\n * none: () => \"Got nothing\"\n * }); // \"Got nothing\"\n *\n * @param option - The Option to match against\n * @param patterns - An object containing handler functions for Some and None cases\n * @returns The result of calling the appropriate handler function\n */\nexport function match<T, R1, R2>(\n option: Option<T>,\n patterns: {\n some: (value: T) => R1;\n none: () => R2;\n },\n): R1 | R2 {\n if (isSome(option)) {\n const someFn = patterns.some;\n return someFn(option.value);\n } else {\n const noneFn = patterns.none;\n return noneFn();\n }\n}\n\n// ─── Unwrap ─────────────────────────────────────────────────\n\n/**\n * Unwraps the value inside an Option, throwing an error if the Option is none.\n *\n * @remarks\n * This function is intended for prototyping only and should not be used in production.\n * In production code, prefer using pattern matching or `unwrapOr` to handle both Some and None cases safely.\n *\n * @example\n * const option: Option<number> = some(123);\n * const value: number = unwrap(option);\n *\n * @param option - The Option to unwrap\n * @param [errorMsg] - Optional custom error message to throw when the Option is none\n * @returns The contained value\n */\nexport function unwrap<T>(option: Option<T>, errorMsg?: string): T {\n if (option.some) {\n return option.value;\n } else {\n throw new Error(errorMsg || 'Cannot unwrap a none Option');\n }\n}\n\n/**\n * Unwraps the value inside an Option, providing a default value if the Option is none.\n *\n * @example\n * const option: Option<number> = none();\n * const value: number = unwrapOr(option, 123);\n *\n * @param option - The Option to unwrap\n * @param defaultValue - The value to return if the Option is none\n * @returns The contained value, or the default value if the Option is none\n */\nexport function unwrapOr<T>(option: Option<T>, defaultValue: T): T {\n return option.some ? option.value : defaultValue;\n}\n\n/**\n * Unwraps the value inside an Option, calling a function to generate a default value if the Option is none.\n * Unlike unwrapOr, this only computes the default when needed.\n *\n * @example\n * const option: Option<number> = none();\n * const value = unwrapOrElse(option, () => expensiveComputation());\n *\n * @param option - The Option to unwrap\n * @param defaultFn - A function that returns a default value if the Option is none\n * @returns The contained value, or the result of calling defaultFn if the Option is none\n */\nexport function unwrapOrElse<T>(option: Option<T>, defaultFn: () => T): T {\n return option.some ? option.value : defaultFn();\n}\n\n// ─── Combining ──────────────────────────────────────────────\n\n/**\n * Combines an array of Options into a single Option containing an array of values.\n * Returns None if any Option in the array is None.\n *\n * @example\n * const options = [some(1), some(2), some(3)];\n * const combined = combine(options); // some([1, 2, 3])\n *\n * const withNone = [some(1), none(), some(3)];\n * const result = combine(withNone); // none()\n *\n * @param options - An array of Options to combine\n * @returns An Option containing an array of all values if all inputs are Some, or None if any input is None\n */\n\n// Tuple-preserving overloads\nexport function combine<T1>(options: readonly [Option<T1>]): Option<[T1]>;\nexport function combine<T1, T2>(options: readonly [Option<T1>, Option<T2>]): Option<[T1, T2]>;\nexport function combine<T1, T2, T3>(options: readonly [Option<T1>, Option<T2>, Option<T3>]): Option<[T1, T2, T3]>;\nexport function combine<T1, T2, T3, T4>(\n options: readonly [Option<T1>, Option<T2>, Option<T3>, Option<T4>],\n): Option<[T1, T2, T3, T4]>;\nexport function combine<T1, T2, T3, T4, T5>(\n options: readonly [Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>],\n): Option<[T1, T2, T3, T4, T5]>;\nexport function combine<T1, T2, T3, T4, T5, T6>(\n options: readonly [Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>, Option<T6>],\n): Option<[T1, T2, T3, T4, T5, T6]>;\nexport function combine<T1, T2, T3, T4, T5, T6, T7>(\n options: readonly [Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>, Option<T6>, Option<T7>],\n): Option<[T1, T2, T3, T4, T5, T6, T7]>;\nexport function combine<T1, T2, T3, T4, T5, T6, T7, T8>(\n options: readonly [Option<T1>, Option<T2>, Option<T3>, Option<T4>, Option<T5>, Option<T6>, Option<T7>, Option<T8>],\n): Option<[T1, T2, T3, T4, T5, T6, T7, T8]>;\nexport function combine<T1, T2, T3, T4, T5, T6, T7, T8, T9>(\n options: readonly [\n Option<T1>,\n Option<T2>,\n Option<T3>,\n Option<T4>,\n Option<T5>,\n Option<T6>,\n Option<T7>,\n Option<T8>,\n Option<T9>,\n ],\n): Option<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>;\nexport function combine<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(\n options: readonly [\n Option<T1>,\n Option<T2>,\n Option<T3>,\n Option<T4>,\n Option<T5>,\n Option<T6>,\n Option<T7>,\n Option<T8>,\n Option<T9>,\n Option<T10>,\n ],\n): Option<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>;\nexport function combine<T>(options: readonly Option<T>[]): Option<T[]>;\n\nexport function combine<T>(options: readonly Option<T>[]): Option<T[]> {\n const values: T[] = [];\n\n for (const option of options) {\n if (!option.some) return none();\n values.push(option.value);\n }\n\n return some(values);\n}\n\n// ─── Conversions ────────────────────────────────────────────\n\n/**\n * Creates an Option from a nullable value.\n *\n * @example\n * const option = fromNullable(123); // some(123)\n * const option = fromNullable(null); // none()\n *\n * @param value - The value to wrap in the Some variant\n * @returns An Option containing the value (Some) if the value is not null or undefined, or None if the value is null or undefined\n */\nexport const fromNullable = <T>(value: T | null | undefined): Option<T> => {\n return value === null || value === undefined ? none() : some(value);\n};\n\n/**\n * Converts an Option to a Result.\n * If the Option is Some, returns an Ok variant with the value.\n * If the Option is None, returns an Err variant with the provided error.\n *\n * @example\n * const result = mapToResult(some(123), 'Some error message');\n * if (isOk(result)) {\n * console.log('Value:', result.value);\n * } else {\n * console.error('Error:', result.error);\n * }\n *\n * @param option - The Option to convert\n * @param error - The error to return if the Option is None\n * @returns A Result containing either the value (Ok) or the error (Err)\n */\nexport function mapToResult<T, E>(option: Option<T>, error: E): Result<T, E> {\n return option.some ? ok(option.value) : err(error);\n}\n\n// ─── Curried / Point-Free ───────────────────────────────────\n\n/**\n * Curried version of `map`. Returns a function that transforms the Some value using `fn`,\n * passing through None unchanged.\n *\n * @example\n * const double = mapWith((n: number) => n * 2);\n *\n * const option: Option<number> = some(5);\n * const doubled: Option<number> = double(option); // some(10)\n *\n * const empty: Option<number> = none();\n * const stillNone: Option<number> = double(empty); // none()\n *\n * // Point-free in a pipe\n * const result = pipe(some(3), mapWith((n: number) => n + 1)); // some(4)\n *\n * @param fn - The function to apply to the Some value\n * @returns A function that takes an Option and returns a new Option with the Some value transformed\n */\nexport function mapWith<T, U>(fn: (value: T) => U): (option: Option<T>) => Option<U> {\n return (option) => map(option, fn);\n}\n\n/**\n * Curried version of `flatMap`. Returns a function that chains on a Some value using `fn`,\n * passing through None unchanged. Supports both sync and async step functions.\n *\n * @example\n * const safeParse = flatMapWith((s: string) => {\n * const n = parseInt(s, 10);\n * return isNaN(n) ? none() : some(n);\n * });\n *\n * const option: Option<string> = some(\"42\");\n * const parsed: Option<number> = safeParse(option); // some(42)\n *\n * const empty: Option<string> = none();\n * const stillNone: Option<number> = safeParse(empty); // none()\n *\n * // Async step function in a pipe\n * const fetchProfile = flatMapWith(async (id: number) => {\n * const profile = await getProfile(id);\n * return profile ? some(profile) : none();\n * });\n *\n * @param fn - The function to apply to the Some value, returning an Option or Promise<Option>\n * @returns A function that takes an Option and returns a (possibly async) Option\n */\nexport function flatMapWith<T, U>(fn: (value: T) => Promise<Option<U>>): (option: Option<T>) => Promise<Option<U>>;\nexport function flatMapWith<T, U>(fn: (value: T) => Option<U>): (option: Option<T>) => Option<U>;\nexport function flatMapWith<T, U>(\n fn: (value: T) => MaybeAsync<Option<U>>,\n): (option: Option<T>) => MaybeAsync<Option<U>> {\n return (option: Option<T>) => {\n if (!option.some) return option;\n return fn(option.value);\n };\n}\n\n/**\n * Curried version of `filter`. Returns a function that keeps a Some value matching the predicate,\n * or returns None if the predicate fails.\n *\n * @example\n * const isPositive = filterWith((n: number) => n > 0);\n *\n * const option: Option<number> = some(5);\n * const filtered: Option<number> = isPositive(option); // some(5)\n *\n * const negative: Option<number> = some(-1);\n * const gone: Option<number> = isPositive(negative); // none()\n *\n * // Point-free in a pipe\n * const result = pipe(some(10), filterWith((n: number) => n < 100)); // some(10)\n *\n * @param predicate - A function that determines if the Some value should be kept\n * @returns A function that takes an Option and returns the original Option or None\n */\nexport function filterWith<T>(predicate: (value: T) => boolean): (option: Option<T>) => Option<T> {\n return (option) => filter(option, predicate);\n}\n\n/**\n * Curried version of `tap`. Returns a function that performs a side effect on the Some value\n * without changing the Option. None values pass through untouched. Supports both sync and async\n * side effects.\n *\n * @example\n * const log = tapWith((n: number) => console.log(\"value:\", n));\n *\n * const option: Option<number> = some(5);\n * const same: Option<number> = log(option); // logs \"value: 5\", returns some(5)\n *\n * const empty: Option<number> = none();\n * const stillNone: Option<number> = log(empty); // no log, returns none()\n *\n * // Async side effect in a pipe\n * const audit = tapWith(async (user: User) => {\n * await saveAuditLog(user.id);\n * });\n *\n * @param fn - The side-effect function to execute on the Some value\n * @returns A function that takes an Option, performs the side effect if Some, and returns the original Option\n */\nexport function tapWith<T>(fn: (value: T) => Promise<void>): (option: Option<T>) => Promise<Option<T>>;\nexport function tapWith<T>(fn: (value: T) => void): (option: Option<T>) => Option<T>;\nexport function tapWith<T>(fn: (value: T) => MaybeAsync<void>): (option: Option<T>) => MaybeAsync<Option<T>> {\n return (option: Option<T>) => {\n if (!option.some) return option;\n const out = fn(option.value);\n if (out instanceof Promise) return out.then(() => option);\n return option;\n };\n}\n\n/**\n * Curried version of `mapToResult`. Returns a function that converts an Option\n * to a Result, using the provided error if the Option is None.\n *\n * @example\n * const toResult = mapToResultWith('not found');\n * toResult(some(42)); // ok(42)\n * toResult(none()); // err('not found')\n *\n * // Point-free in a pipe\n * pipe(fromNullable(value), mapToResultWith('missing'));\n *\n * @param error - The error to return if the Option is None\n * @returns A function that takes an Option and returns a Result\n */\nexport function mapToResultWith<E>(error: E): <T>(option: Option<T>) => Result<T, E> {\n return (option) => mapToResult(option, error);\n}\n","import { none, some, type Option } from '@/option';\n\nimport type { MaybeAsync } from '@/composition';\n\n// ─── Types & Constructors ───────────────────────────────────\n\n/**\n * Symbol used to identify Result objects.\n *\n * @internal\n */\nexport const RESULT_BRAND = Symbol('RESULT_BRAND');\n\n/**\n * Represents a Result type.\n *\n * @example\n * const result: Result<number, string> = ok(123);\n * const error: Result<number, string> = error(\"An error occurred\");\n *\n * @param T - The type of the value contained in the Ok variant\n * @param E - The type of the error contained in the Error variant\n * @returns A Result containing a value or an error\n */\nexport type Result<T, E> =\n | {\n readonly ok: true;\n readonly value: T;\n readonly [RESULT_BRAND]: 'ok';\n }\n | {\n readonly ok: false;\n readonly error: E;\n readonly [RESULT_BRAND]: 'error';\n };\n\n/**\n * Creates a Result containing a value.\n *\n * @example\n * const result: Result<number, string> = ok(123);\n *\n * @param value - The value to contain\n * @returns A Result containing the value\n */\nexport function ok<T, E = never>(value: T): Result<T, E> {\n return {\n ok: true,\n value,\n [RESULT_BRAND]: 'ok',\n };\n}\n\n/**\n * Creates a Result containing an error.\n *\n * @example\n * const error: Result<number, string> = err(\"An error occurred\");\n *\n * @param error - The error to contain\n * @returns A Result containing the error\n */\nexport function err<E>(error: E): Result<never, E> {\n return { ok: false, error, [RESULT_BRAND]: 'error' };\n}\n\n// ─── Type Guards ────────────────────────────────────────────\n\n/**\n * Type guard that checks if a Result is an Ok variant containing a value.\n *\n * @example\n * const result: Result<number, string> = ok(123);\n * if (isOk(result)) {\n * // TypeScript knows result.value exists here\n * console.log(result.value);\n * }\n *\n * @param result - The result to check\n * @returns A type predicate indicating if the result is an Ok variant\n */\nexport function isOk<T, E>(\n result: Result<T, E>,\n): result is {\n readonly ok: true;\n readonly value: T;\n readonly [RESULT_BRAND]: 'ok';\n} {\n return result.ok;\n}\n\n/**\n * Type guard that checks if a Result is an Error variant.\n *\n * @example\n * const result: Result<number, string> = err(\"error\");\n * if (isErr(result)) {\n * // TypeScript knows result.error exists here\n * console.log(result.error);\n * }\n *\n * @param result - The result to check\n * @returns A type predicate indicating if the result is an Error variant\n */\nexport function isErr<T, E>(\n result: Result<T, E>,\n): result is {\n readonly ok: false;\n readonly error: E;\n readonly [RESULT_BRAND]: 'error';\n} {\n return !result.ok;\n}\n\n// ─── Transformations ────────────────────────────────────────\n\n/**\n * Maps the value inside a Result using a transformation function.\n *\n * @example\n * const result: Result<number, string> = ok(123);\n * const transformed: Result<string, string> = map(result, (value) => value.toString());\n *\n * @param result - The Result to transform\n * @param fn - The function to apply to the contained value\n * @returns A new Result containing the transformed value, or the original error if the input was an error\n */\nexport function map<T, E, U>(result: Result<T, E>, fn: (value: T) => U): Result<U, E> {\n return result.ok ? ok(fn(result.value)) : result;\n}\n\n/**\n * Maps the value inside a Result using a transformation function that returns a Result.\n *\n * @example\n * const result: Result<number, string> = ok(123);\n * const transformed: Result<string, string> = flatMap(result, (value) => ok(value.toString()));\n *\n * @param result - The Result to transform\n * @param fn - The function to apply to the contained value, returning a Result\n * @returns The Result returned by the transformation function, or the original error if the input was an error\n */\nexport function flatMap<T, U, E1, E2>(result: Result<T, E1>, fn: (value: T) => Result<U, E2>): Result<U, E1 | E2> {\n return result.ok ? fn(result.value) : result;\n}\n\n/**\n * Maps the error inside a Result using a transformation function.\n *\n * @example\n * const result: Result<number, string> = err(\"error\");\n * const transformed: Result<number, Error> = mapErr(result, (err) => new Error(err));\n *\n * @param result - The Result to transform\n * @param fn - The function to apply to the contained error\n * @returns A new Result containing the transformed error, or the original value if the input was ok\n */\nexport function mapErr<T, E, F>(result: Result<T, E>, fn: (error: E) => F): Result<T, F> {\n return result.ok ? result : err(fn(result.error));\n}\n\n/**\n * Maps both the value and error of a Result using separate transformation functions.\n * This is equivalent to chaining map and mapErr, but more concise.\n *\n * @example\n * const result: Result<number, string> = ok(123);\n * const transformed = bimap(\n * result,\n * (value) => value.toString(),\n * (error) => new Error(error)\n * ); // ok(\"123\")\n *\n * const error: Result<number, string> = err(\"failed\");\n * const transformed2 = bimap(\n * error,\n * (value) => value.toString(),\n * (error) => new Error(error)\n * ); // err(Error(\"failed\"))\n *\n * @param result - The Result to transform\n * @param okFn - The function to apply to the contained value if Ok\n * @param errFn - The function to apply to the contained error if Err\n * @returns A new Result with both branches transformed\n */\nexport function bimap<T, E, U, F>(result: Result<T, E>, okFn: (value: T) => U, errFn: (error: E) => F): Result<U, F> {\n return result.ok ? ok(okFn(result.value)) : err(errFn(result.error));\n}\n\n/**\n * Filters a Result based on a predicate function.\n *\n * @example\n * const result: Result<number, string> = ok(123);\n * const filtered: Result<number, string> = filter(result, value => value > 100, \"Value too small\"); // still Ok(123)\n * const filtered2: Result<number, string> = filter(result, value => value > 200, \"Value too small\"); // Err(\"Value too small\")\n *\n * @param result - The Result to filter\n * @param predicate - A function that determines if the value should be kept\n * @param error - The error to return if the predicate fails\n * @returns The original Result if it contains a value that satisfies the predicate, otherwise an Error\n */\nexport function filter<T, E1, E2>(\n result: Result<T, E1>,\n predicate: (value: T) => boolean,\n error: E2,\n): Result<T, E1 | E2> {\n if (!result.ok) return result;\n return predicate(result.value) ? result : err(error);\n}\n\n// ─── Side Effects ───────────────────────────────────────────\n\n/**\n * Executes a callback with the value if the Result is Ok, without changing the Result.\n * Useful for side effects like logging while maintaining a processing chain.\n *\n * @example\n * const finalResult = pipe(\n * ok(123),\n * r => map(r, x => x * 2),\n * r => tap(r, x => console.log(`Value: ${x}`)), // Logs but doesn't change the Result\n * r => filter(r, x => x > 200, \"Value too small\")\n * );\n *\n * @param result - The Result to tap into\n * @param fn - The function to execute with the value if Ok\n * @returns The original Result unchanged\n */\nexport function tap<T, E>(result: Result<T, E>, fn: (value: T) => void): Result<T, E> {\n if (isOk(result)) {\n const callback = fn;\n callback(result.value);\n }\n return result;\n}\n\n/**\n * Executes a callback with the error if the Result is an Error, without changing the Result.\n * Useful for side effects like logging while maintaining a processing chain.\n *\n * @example\n * const finalResult = pipe(\n * err(\"something went wrong\"),\n * r => tapErr(r, e => console.error(`Error: ${e}`)), // Logs but doesn't change the Result\n * r => mapErr(r, e => new Error(e))\n * );\n *\n * @param result - The Result to tap into\n * @param fn - The function to execute with the error if Error\n * @returns The original Result unchanged\n */\nexport function tapErr<T, E>(result: Result<T, E>, fn: (error: E) => void): Result<T, E> {\n if (isErr(result)) {\n const callback = fn;\n callback(result.error);\n }\n return result;\n}\n\n// ─── Error Recovery ─────────────────────────────────────────\n\n/**\n * Recovers from an Err by applying `fn` to the error value. Returns the original Ok unchanged.\n * This is the error-channel equivalent of `flatMap`.\n *\n * @example\n * const result: Result<number, string> = err(\"not found\");\n * const recovered = orElse(result, (e) => ok(e.length)); // ok(9)\n *\n * const okResult: Result<number, string> = ok(42);\n * const kept = orElse(okResult, (e) => ok(e.length)); // ok(42)\n *\n * @param result - The Result to recover from\n * @param fn - A function that receives the Err value and returns a fallback Result\n * @returns The original Ok, or the Result of applying fn to the error\n */\nexport function orElse<T1, E1, T2, E2>(result: Result<T1, E1>, fn: (error: E1) => Result<T2, E2>): Result<T1 | T2, E2> {\n return result.ok ? result : fn(result.error);\n}\n\n// ─── Pattern Matching ───────────────────────────────────────\n\n/**\n * Pattern matches on a Result to handle both Ok and Error cases.\n *\n * @example\n * const result = ok(42);\n * const message = match(result, {\n * ok: (value) => `Got value: ${value}`,\n * err: (error) => `Got error: ${error}`\n * }); // \"Got value: 42\"\n *\n * @param result - The Result to match against\n * @param patterns - An object containing handler functions for Ok and Error cases\n * @returns The result of calling the appropriate handler function\n */\nexport function match<T, E, R1, R2>(\n result: Result<T, E>,\n patterns: {\n ok: (value: T) => R1;\n err: (error: E) => R2;\n },\n): R1 | R2 {\n if (isOk(result)) {\n const okFn = patterns.ok;\n return okFn(result.value);\n } else {\n const errFn = patterns.err;\n return errFn(result.error);\n }\n}\n\n// ─── Unwrap ─────────────────────────────────────────────────\n\n/**\n * Unwraps the value inside a Result, throwing an error if the Result is an error.\n *\n * @remarks\n * This function is intended for prototyping only and should not be used in production.\n * In production code, prefer using pattern matching or `unwrapOr` to handle both Ok and Error cases safely.\n *\n * @example\n * const result: Result<number, string> = ok(123);\n * const value: number = unwrap(result);\n *\n * // With custom error message\n * try {\n * const badResult: Result<number, string> = err(\"Invalid data\");\n * unwrap(badResult, \"Custom error message\");\n * } catch (error) {\n * // Error will contain \"Custom error message\" instead of the default\n * }\n *\n * @param result - The Result to unwrap\n * @param errorMsg - Optional custom error message to use if Result is an error\n * @returns The contained value\n * @throws If the Result is an error\n */\nexport function unwrap<T, E>(result: Result<T, E>, errorMsg?: string): T {\n if (result.ok) {\n return result.value;\n } else {\n throw new Error(errorMsg || `Cannot unwrap an error Result: ${String(result.error)}`);\n }\n}\n\n/**\n * Unwraps the value inside a Result, providing a default value if the Result is an error.\n *\n * @example\n * const result: Result<number, string> = err(\"error\");\n * const value: number = unwrapOr(result, 123);\n *\n * @param result - The Result to unwrap\n * @param defaultValue - The value to return if the Result is an error\n * @returns The contained value, or the default value if the Result is an error\n */\nexport function unwrapOr<T, E>(result: Result<T, E>, defaultValue: T): T {\n return result.ok ? result.value : defaultValue;\n}\n\n/**\n * Unwraps the value inside a Result, calling a function to generate a default value if the Result is an error.\n * Unlike unwrapOr, this only computes the default when needed.\n *\n * @example\n * const result: Result<number, string> = err(\"error\");\n * const value = unwrapOrElse(result, () => expensiveComputation());\n *\n * @param result - The Result to unwrap\n * @param defaultFn - A function that returns a default value if the Result is an error\n * @returns The contained value, or the result of calling defaultFn if the Result is an error\n */\nexport function unwrapOrElse<T, E>(result: Result<T, E>, defaultFn: () => T): T {\n return result.ok ? result.value : defaultFn();\n}\n\n// ─── Combining ──────────────────────────────────────────────\n\n/**\n * Combines an array of Results into a single Result containing an array of values.\n * Returns an Error if any Result in the array is an Error, using the first encountered error.\n * If an empty array is provided, returns `ok([])` (an Ok containing an empty array).\n *\n * @example\n * const results = [ok(1), ok(2), ok(3)];\n * const combined = combine(results); // ok([1, 2, 3])\n *\n * const withError = [ok(1), err(\"error\"), ok(3)];\n * const result = combine(withError); // err(\"error\")\n *\n * const emptyResults = [];\n * const emptyResult = combine(emptyResults); // ok([])\n *\n * @param results - An array of Results to combine\n * @returns A Result containing an array of all values if all inputs are Ok, or the first Error if any input is an Error\n */\n\n// Tuple-preserving overloads with error type unions\nexport function combine<T1, E1>(results: readonly [Result<T1, E1>]): Result<[T1], E1>;\nexport function combine<T1, E1, T2, E2>(results: readonly [Result<T1, E1>, Result<T2, E2>]): Result<[T1, T2], E1 | E2>;\nexport function combine<T1, E1, T2, E2, T3, E3>(\n results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>],\n): Result<[T1, T2, T3], E1 | E2 | E3>;\nexport function combine<T1, E1, T2, E2, T3, E3, T4, E4>(\n results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>, Result<T4, E4>],\n): Result<[T1, T2, T3, T4], E1 | E2 | E3 | E4>;\nexport function combine<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5>(\n results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>, Result<T4, E4>, Result<T5, E5>],\n): Result<[T1, T2, T3, T4, T5], E1 | E2 | E3 | E4 | E5>;\nexport function combine<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6>(\n results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>, Result<T4, E4>, Result<T5, E5>, Result<T6, E6>],\n): Result<[T1, T2, T3, T4, T5, T6], E1 | E2 | E3 | E4 | E5 | E6>;\nexport function combine<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7>(\n results: readonly [\n Result<T1, E1>,\n Result<T2, E2>,\n Result<T3, E3>,\n Result<T4, E4>,\n Result<T5, E5>,\n Result<T6, E6>,\n Result<T7, E7>,\n ],\n): Result<[T1, T2, T3, T4, T5, T6, T7], E1 | E2 | E3 | E4 | E5 | E6 | E7>;\nexport function combine<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7, T8, E8>(\n results: readonly [\n Result<T1, E1>,\n Result<T2, E2>,\n Result<T3, E3>,\n Result<T4, E4>,\n Result<T5, E5>,\n Result<T6, E6>,\n Result<T7, E7>,\n Result<T8, E8>,\n ],\n): Result<[T1, T2, T3, T4, T5, T6, T7, T8], E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8>;\nexport function combine<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7, T8, E8, T9, E9>(\n results: readonly [\n Result<T1, E1>,\n Result<T2, E2>,\n Result<T3, E3>,\n Result<T4, E4>,\n Result<T5, E5>,\n Result<T6, E6>,\n Result<T7, E7>,\n Result<T8, E8>,\n Result<T9, E9>,\n ],\n): Result<[T1, T2, T3, T4, T5, T6, T7, T8, T9], E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8 | E9>;\nexport function combine<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7, T8, E8, T9, E9, T10, E10>(\n results: readonly [\n Result<T1, E1>,\n Result<T2, E2>,\n Result<T3, E3>,\n Result<T4, E4>,\n Result<T5, E5>,\n Result<T6, E6>,\n Result<T7, E7>,\n Result<T8, E8>,\n Result<T9, E9>,\n Result<T10, E10>,\n ],\n): Result<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10], E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8 | E9 | E10>;\nexport function combine<T, E>(results: readonly Result<T, E>[]): Result<T[], E>;\nexport function combine<T, E>(results: readonly Result<T, E>[]): Result<T[], E> {\n const values: T[] = [];\n\n for (const result of results) {\n if (!result.ok) return result;\n values.push(result.value);\n }\n\n return ok(values);\n}\n\n/**\n * Combines an array of Results into a single Result containing an array of values.\n * Unlike combine, this collects all errors instead of returning just the first one.\n * If all Results are Ok, returns an Ok containing an array of all values.\n * If any Results are Error, returns an Error containing an array of all errors.\n *\n * @example\n * const results = [ok(1), ok(2), ok(3)];\n * const combined = combineAll(results); // ok([1, 2, 3])\n *\n * const withErrors = [ok(1), err(\"error1\"), ok(3), err(\"error2\")];\n * const result = combineAll(withErrors); // err([\"error1\", \"error2\"])\n *\n * const emptyResults = [];\n * const emptyResult = combineAll(emptyResults); // ok([])\n *\n * @param results - An array of Results to combine\n * @returns A Result containing an array of all values if all inputs are Ok, or an array of all errors if any input is an Error\n */\n\n// Tuple-preserving overloads with error arrays\nexport function combineAll<T1, E1>(results: readonly [Result<T1, E1>]): Result<[T1], E1[]>;\nexport function combineAll<T1, E1, T2, E2>(\n results: readonly [Result<T1, E1>, Result<T2, E2>],\n): Result<[T1, T2], (E1 | E2)[]>;\nexport function combineAll<T1, E1, T2, E2, T3, E3>(\n results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>],\n): Result<[T1, T2, T3], (E1 | E2 | E3)[]>;\nexport function combineAll<T1, E1, T2, E2, T3, E3, T4, E4>(\n results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>, Result<T4, E4>],\n): Result<[T1, T2, T3, T4], (E1 | E2 | E3 | E4)[]>;\nexport function combineAll<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5>(\n results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>, Result<T4, E4>, Result<T5, E5>],\n): Result<[T1, T2, T3, T4, T5], (E1 | E2 | E3 | E4 | E5)[]>;\nexport function combineAll<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6>(\n results: readonly [Result<T1, E1>, Result<T2, E2>, Result<T3, E3>, Result<T4, E4>, Result<T5, E5>, Result<T6, E6>],\n): Result<[T1, T2, T3, T4, T5, T6], (E1 | E2 | E3 | E4 | E5 | E6)[]>;\nexport function combineAll<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7>(\n results: readonly [\n Result<T1, E1>,\n Result<T2, E2>,\n Result<T3, E3>,\n Result<T4, E4>,\n Result<T5, E5>,\n Result<T6, E6>,\n Result<T7, E7>,\n ],\n): Result<[T1, T2, T3, T4, T5, T6, T7], (E1 | E2 | E3 | E4 | E5 | E6 | E7)[]>;\nexport function combineAll<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7, T8, E8>(\n results: readonly [\n Result<T1, E1>,\n Result<T2, E2>,\n Result<T3, E3>,\n Result<T4, E4>,\n Result<T5, E5>,\n Result<T6, E6>,\n Result<T7, E7>,\n Result<T8, E8>,\n ],\n): Result<[T1, T2, T3, T4, T5, T6, T7, T8], (E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8)[]>;\nexport function combineAll<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7, T8, E8, T9, E9>(\n results: readonly [\n Result<T1, E1>,\n Result<T2, E2>,\n Result<T3, E3>,\n Result<T4, E4>,\n Result<T5, E5>,\n Result<T6, E6>,\n Result<T7, E7>,\n Result<T8, E8>,\n Result<T9, E9>,\n ],\n): Result<[T1, T2, T3, T4, T5, T6, T7, T8, T9], (E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8 | E9)[]>;\nexport function combineAll<T1, E1, T2, E2, T3, E3, T4, E4, T5, E5, T6, E6, T7, E7, T8, E8, T9, E9, T10, E10>(\n results: readonly [\n Result<T1, E1>,\n Result<T2, E2>,\n Result<T3, E3>,\n Result<T4, E4>,\n Result<T5, E5>,\n Result<T6, E6>,\n Result<T7, E7>,\n Result<T8, E8>,\n Result<T9, E9>,\n Result<T10, E10>,\n ],\n): Result<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10], (E1 | E2 | E3 | E4 | E5 | E6 | E7 | E8 | E9 | E10)[]>;\nexport function combineAll<T, E>(results: readonly Result<T, E>[]): Result<T[], E[]>;\nexport function combineAll<T, E>(results: readonly Result<T, E>[]): Result<T[], E[]> {\n const errors: E[] = [];\n const values: T[] = [];\n\n for (const result of results) {\n if (result.ok) {\n values.push(result.value);\n } else {\n errors.push(result.error);\n }\n }\n\n return errors.length > 0 ? err(errors) : ok(values);\n}\n\n/**\n * Separates an array of Results into successes and failures.\n * Unlike `combineAll` which is all-or-nothing, `partition` preserves both sides —\n * useful for batch processing where partial success is meaningful.\n *\n * @example\n * const results = [ok(1), err('bad'), ok(3), err('worse')];\n * partition(results);\n * // { successes: [1, 3], failures: ['bad', 'worse'] }\n */\nexport const partition = <T, E>(results: Result<T, E>[]): { successes: T[]; failures: E[] } => ({\n successes: results.filter((r) => isOk(r)).map((r) => r.value),\n failures: results.filter((r) => isErr(r)).map((r) => r.error),\n});\n\n// ─── Conversions ────────────────────────────────────────────\n\n/**\n * Converts a Result to an Option.\n * If the Result is Ok, returns a Some variant with the value.\n * If the Result is an Error, returns a None variant.\n *\n * @example\n * import { some, none } from \"@/option\";\n *\n * const option1 = mapToOption(ok(123)); // some(123)\n * const option2 = mapToOption(err(\"error\")); // none()\n *\n * @param result - The Result to convert\n * @returns An Option containing the value if Ok, or None if Error\n */\nexport function mapToOption<T, E>(result: Result<T, E>): Option<T> {\n return result.ok ? some(result.value) : none();\n}\n\n/**\n * Safely executes a function that might throw and converts the result into a Result type.\n * If the function executes successfully, returns an Ok variant with the return value.\n * If the function throws an error, returns an Error variant containing the error message as a string.\n *\n * @example\n * const parseJson = (str: string) => fromTry(() => JSON.parse(str));\n *\n * const validResult = parseJson('{\"name\":\"John\"}'); // ok({ name: 'John' })\n * const invalidResult = parseJson('invalid json'); // err(\"Unexpected token...\")\n *\n * @param f - The function to execute\n * @returns A Result containing either the function's return value or the error message\n */\nexport function fromTry<T>(f: () => T): Result<T, string> {\n try {\n return ok(f());\n } catch (error) {\n return err(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Safely executes a function that might throw and converts the result into a Result type.\n * Unlike fromTry, this preserves the full Error object instead of just the message.\n * Use this when you need access to error stack traces or custom error properties.\n *\n * @example\n * const result = fromTryWithError(() => JSON.parse('invalid'));\n * if (isErr(result)) {\n * console.log(result.error.stack); // Access to full stack trace\n * }\n *\n * @param f - The function to execute\n * @returns A Result containing either the function's return value or the full Error object\n */\nexport function fromTryWithError<T>(f: () => T): Result<T, Error> {\n try {\n return ok(f());\n } catch (error) {\n return err(error instanceof Error ? error : new Error(String(error)));\n }\n}\n\n/**\n * Converts a Promise to a Result, capturing any errors that occur during promise resolution.\n * Returns error messages as strings for simplicity.\n *\n * @example\n * const result = await fromPromise(fetch('https://api.example.com/data'));\n * if (isErr(result)) {\n * console.log(result.error); // Error message as string\n * }\n *\n * @param promise - The Promise to convert\n * @returns A Promise that resolves to a Result with string error messages\n */\nexport async function fromPromise<T>(promise: Promise<T>): Promise<Result<T, string>> {\n try {\n const value = await promise;\n return ok(value);\n } catch (error) {\n return err(error instanceof Error ? error.message : String(error));\n }\n}\n\n/**\n * Converts a Promise to a Result, preserving the full error object or custom error type.\n * Use this when you need access to error stack traces, custom properties, or specific error types.\n *\n * @example\n * // With default error handling (preserves full error)\n * const result = await fromPromiseWithError(fetch('https://api.example.com/data'));\n *\n * @example\n * // With specific error type and transformer\n * type ApiError = { code: number; message: string };\n *\n * const result = await fromPromiseWithError<Response, ApiError>(\n * fetch('https://api.example.com/data'),\n * (error) => ({\n * code: error instanceof Error ? 500 : 400,\n * message: error instanceof Error ? error.message : String(error)\n * })\n * );\n *\n * @param promise - The Promise to convert\n * @param errorFn - Optional function to transform caught errors to the expected error type\n * @returns A Promise that resolves to a Result\n */\nexport async function fromPromiseWithError<T, E = unknown>(\n promise: Promise<T>,\n errorFn: (error: unknown) => E = (error) => error as unknown as E,\n): Promise<Result<T, E>> {\n try {\n const value = await promise;\n return ok(value);\n } catch (error) {\n return err(errorFn(error));\n }\n}\n\n/**\n * Converts a Result to a Promise.\n *\n * @example\n * const result = ok(123);\n * const promise = toPromise(result);\n * const value = await promise; // 123\n *\n * @param result - The Result to convert\n * @returns A Promise that resolves to the value if Ok, or rejects with the error if Error\n */\nexport function toPromise<T, E>(result: Result<T, E>): Promise<T> {\n return result.ok ? Promise.resolve(result.value) : Promise.reject(result.error);\n}\n\n// ─── Curried / Point-Free ───────────────────────────────────\n\n/**\n * Curried version of `map`. Returns a function that transforms the Ok value using `fn`,\n * passing through any Err unchanged.\n *\n * @example\n * const double = mapWith((n: number) => n * 2);\n *\n * const result: Result<number, string> = ok(5);\n * const doubled: Result<number, string> = double(result); // ok(10)\n *\n * const errResult: Result<number, string> = err(\"fail\");\n * const stillErr: Result<number, string> = double(errResult); // err(\"fail\")\n *\n * // Point-free in a pipe\n * const result2 = pipe(ok(3), mapWith((n: number) => n + 1)); // ok(4)\n *\n * @param fn - The function to apply to the Ok value\n * @returns A function that takes a Result and returns a new Result with the Ok value transformed\n */\nexport function mapWith<T, U>(fn: (value: T) => U): <E>(result: Result<T, E>) => Result<U, E> {\n return (result) => map(result, fn);\n}\n\n/**\n * Curried version of `flatMap`. Returns a function that chains on an Ok value using `fn`,\n * passing through any Err unchanged. Supports both sync and async step functions.\n *\n * @example\n * const toStr = flatMapWith((n: number) => ok(n.toString()));\n *\n * const result: Result<number, string> = ok(5);\n * const chained: Result<string, string> = toStr(result); // ok(\"5\")\n *\n * const errResult: Result<number, string> = err(\"fail\");\n * const stillErr: Result<string, string> = toStr(errResult); // err(\"fail\")\n *\n * // Async step function in a pipe\n * const fetchUser = flatMapWith(async (id: number) => {\n * const user = await getUser(id);\n * return user ? ok(user) : err(\"not found\" as const);\n * });\n *\n * @param fn - The function to apply to the Ok value, returning a Result or Promise<Result>\n * @returns A function that takes a Result and returns a (possibly async) Result\n */\nexport function flatMapWith<T, U, E2>(\n fn: (value: T) => Promise<Result<U, E2>>,\n): <E1>(result: Result<T, E1>) => Promise<Result<U, E1 | E2>>;\nexport function flatMapWith<T, U, E2>(\n fn: (value: T) => Result<U, E2>,\n): <E1>(result: Result<T, E1>) => Result<U, E1 | E2>;\nexport function flatMapWith<T, U, E2>(\n fn: (value: T) => MaybeAsync<Result<U, E2>>,\n): <E1>(result: Result<T, E1>) => MaybeAsync<Result<U, E1 | E2>> {\n return (result) => {\n if (!result.ok) return result;\n return fn(result.value);\n };\n}\n\n/**\n * Curried version of `mapErr`. Returns a function that transforms the Err value using `fn`,\n * passing through any Ok unchanged.\n *\n * @example\n * const wrapError = mapErrWith((msg: string) => new Error(msg));\n *\n * const result: Result<number, string> = err(\"fail\");\n * const wrapped: Result<number, Error> = wrapError(result); // err(Error(\"fail\"))\n *\n * const okResult: Result<number, string> = ok(42);\n * const stillOk: Result<number, Error> = wrapError(okResult); // ok(42)\n *\n * // Point-free in a pipe\n * const result2 = pipe(err(\"oops\"), mapErrWith((s: string) => s.toUpperCase())); // err(\"OOPS\")\n *\n * @param fn - The function to apply to the Err value\n * @returns A function that takes a Result and returns a new Result with the Err value transformed\n */\nexport function mapErrWith<E, F>(fn: (error: E) => F): <T>(result: Result<T, E>) => Result<T, F> {\n return (result) => mapErr(result, fn);\n}\n\n/**\n * Curried version of `filter`. Returns a function that keeps an Ok value matching the predicate,\n * or returns the provided error if the predicate fails.\n *\n * @example\n * const isPositive = filterWith((n: number) => n > 0, \"must be positive\");\n *\n * const result: Result<number, string> = ok(5);\n * const filtered: Result<number, string> = isPositive(result); // ok(5)\n *\n * const zero: Result<number, string> = ok(-1);\n * const failed: Result<number, string> = isPositive(zero); // err(\"must be positive\")\n *\n * // Point-free in a pipe\n * const result2 = pipe(ok(10), filterWith((n: number) => n < 100, \"too large\")); // ok(10)\n *\n * @param predicate - A function that determines if the Ok value should be kept\n * @param error - The error to return if the predicate fails\n * @returns A function that takes a Result and returns the original Result or an Err\n */\nexport function filterWith<T, E2>(\n predicate: (value: T) => boolean,\n error: E2,\n): <E1>(result: Result<T, E1>) => Result<T, E1 | E2> {\n return (result) => filter(result, predicate, error);\n}\n\n/**\n * Curried version of `tap`. Returns a function that performs a side effect on the Ok value\n * without changing the Result. Err values pass through untouched. Supports both sync and async\n * side effects.\n *\n * @example\n * const log = tapWith((n: number) => console.log(\"value:\", n));\n *\n * const result: Result<number, string> = ok(5);\n * const same: Result<number, string> = log(result); // logs \"value: 5\", returns ok(5)\n *\n * const errResult: Result<number, string> = err(\"fail\");\n * const stillErr: Result<number, string> = log(errResult); // no log, returns err(\"fail\")\n *\n * // Async side effect in a pipe\n * const audit = tapWith(async (user: User) => {\n * await saveAuditLog(user.id);\n * });\n *\n * @param fn - The side-effect function to execute on the Ok value\n * @returns A function that takes a Result, performs the side effect if Ok, and returns the original Result\n */\nexport function tapWith<T>(fn: (value: T) => Promise<void>): <E>(result: Result<T, E>) => Promise<Result<T, E>>;\nexport function tapWith<T>(fn: (value: T) => void): <E>(result: Result<T, E>) => Result<T, E>;\nexport function tapWith<T>(fn: (value: T) => MaybeAsync<void>): <E>(result: Result<T, E>) => MaybeAsync<Result<T, E>> {\n return (result) => {\n if (!result.ok) return result;\n const out = fn(result.value);\n if (out instanceof Promise) return out.then(() => result);\n return result;\n };\n}\n\n/**\n * Curried version of `tapErr`. Returns a function that performs a side effect on the Err value\n * without changing the Result. Ok values pass through untouched.\n *\n * @example\n * const logErr = tapErrWith((msg: string) => console.error(\"error:\", msg));\n *\n * const errResult: Result<number, string> = err(\"fail\");\n * const same: Result<number, string> = logErr(errResult); // logs \"error: fail\", returns err(\"fail\")\n *\n * const okResult: Result<number, string> = ok(42);\n * const stillOk: Result<number, string> = logErr(okResult); // no log, returns ok(42)\n *\n * // Point-free in a pipe\n * const result = pipe(err(\"oops\"), tapErrWith((e: string) => reportError(e)));\n *\n * // Async side effect in a pipe\n * const audit = tapErrWith(async (error: string) => {\n * await logErrorToService(error);\n * });\n *\n * @param fn - The side-effect function to execute on the Err value\n * @returns A function that takes a Result, performs the side effect if Err, and returns the original Result\n */\nexport function tapErrWith<E>(fn: (error: E) => Promise<void>): <T>(result: Result<T, E>) => Promise<Result<T, E>>;\nexport function tapErrWith<E>(fn: (error: E) => void): <T>(result: Result<T, E>) => Result<T, E>;\nexport function tapErrWith<E>(\n fn: (error: E) => MaybeAsync<void>,\n): <T>(result: Result<T, E>) => MaybeAsync<Result<T, E>> {\n return (result) => {\n if (result.ok) return result;\n const out = fn(result.error);\n if (out instanceof Promise) return out.then(() => result);\n return result;\n };\n}\n\n/**\n * Curried version of `orElse`. Recovers from an Err by applying `fn` to the error value;\n * passes through Ok unchanged.\n *\n * @example\n * const recover = orElseWith((msg: string) => ok(msg.length));\n *\n * const errResult: Result<number, string> = err(\"fail\");\n * const recovered: Result<number, string> = recover(errResult); // ok(4)\n *\n * const okResult: Result<number, string> = ok(42);\n * const kept: Result<number, string> = recover(okResult); // ok(42)\n *\n * // Point-free in a pipe — recover using the error\n * const result = pipe(\n * err(\"not found\"),\n * orElseWith((e: string) => ok(`recovered from: ${e}`))\n * ); // ok(\"recovered from: not found\")\n *\n * @param fn - A function that receives the Err value and returns a fallback Result\n * @returns A function that takes a Result and returns either the original Ok or the result of calling fn\n */\nexport function orElseWith<E1, T2, E2>(\n fn: (error: E1) => Result<T2, E2>,\n): <T1>(result: Result<T1, E1>) => Result<T1 | T2, E2> {\n return (result) => orElse(result, fn);\n}\n"]}
|