ts-data-forge 14.1.0 → 14.2.0
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/dist/functional/optional/impl/optional-map.d.mts +1 -1
- package/dist/functional/optional/impl/optional-map.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-map.mjs.map +1 -1
- package/dist/functional/result/impl/result-map-err.d.mts +1 -1
- package/dist/functional/result/impl/result-map-err.d.mts.map +1 -1
- package/dist/functional/result/impl/result-map-err.mjs.map +1 -1
- package/dist/functional/result/impl/result-map.d.mts +1 -1
- package/dist/functional/result/impl/result-map.d.mts.map +1 -1
- package/dist/functional/result/impl/result-map.mjs.map +1 -1
- package/package.json +1 -1
- package/src/functional/optional/impl/optional-map.mts +16 -1
- package/src/functional/optional.test.mts +48 -1
- package/src/functional/result/impl/result-map-err.mts +16 -1
- package/src/functional/result/impl/result-map.mts +16 -1
- package/src/functional/result.test.mts +78 -1
|
@@ -30,5 +30,5 @@ import { type Unwrap } from './types.mjs';
|
|
|
30
30
|
* `None` if the input was `None`.
|
|
31
31
|
*/
|
|
32
32
|
export declare function map<O extends UnknownOptional, S2>(optional: O, mapFn: (value: Unwrap<O>) => S2): Optional<S2>;
|
|
33
|
-
export declare function map<S, S2>(mapFn: (value: S) => S2): (optional:
|
|
33
|
+
export declare function map<S, S2>(mapFn: (value: S) => S2): <O extends UnknownOptional>(optional: Unwrap<O> extends S ? O : never) => Optional<S2>;
|
|
34
34
|
//# sourceMappingURL=optional-map.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"optional-map.d.mts","sourceRoot":"","sources":["../../../../src/functional/optional/impl/optional-map.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAKtE,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,GAAG,CAAC,CAAC,SAAS,eAAe,EAAE,EAAE,EAC/C,QAAQ,EAAE,CAAC,EACX,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,KAAK,EAAE,GAC9B,QAAQ,CAAC,EAAE,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"optional-map.d.mts","sourceRoot":"","sources":["../../../../src/functional/optional/impl/optional-map.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAKtE,OAAO,EAAE,KAAK,MAAM,EAAE,MAAM,aAAa,CAAC;AAE1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAgB,GAAG,CAAC,CAAC,SAAS,eAAe,EAAE,EAAE,EAC/C,QAAQ,EAAE,CAAC,EACX,KAAK,EAAE,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,CAAC,KAAK,EAAE,GAC9B,QAAQ,CAAC,EAAE,CAAC,CAAC;AAgBhB,wBAAgB,GAAG,CAAC,CAAC,EAAE,EAAE,EACvB,KAAK,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,GACtB,CAAC,CAAC,SAAS,eAAe,EAC3B,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,KAAK,KACtC,QAAQ,CAAC,EAAE,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"optional-map.mjs","sources":["../../../../src/functional/optional/impl/optional-map.mts"],"sourcesContent":["import { type Optional, type UnknownOptional } from '../optional.mjs';\nimport { isSome } from './optional-is-some.mjs';\nimport { none } from './optional-none.mjs';\nimport { some } from './optional-some.mjs';\nimport { unwrap } from './optional-unwrap.mjs';\nimport { type Unwrap } from './types.mjs';\n\n/**\n * Maps an {@link Optional}<S> to {@link Optional}<S2> by applying a function to\n * a contained value. If the {@link Optional} is {@link None}, it\n * returns {@link Optional.none}. Otherwise, it applies the `mapFn` to the\n * value in `Some` and returns a new `Some` with the result.\n *\n * @example\n *\n * ```ts\n * const numberOptional = Optional.some(21);\n *\n * const mapped = Optional.map(numberOptional, (value) => value * 2);\n *\n * assert.deepStrictEqual(mapped, Optional.some(42));\n *\n * const mapToLength = Optional.map((text: string) => text.length);\n *\n * assert.deepStrictEqual(mapToLength(Optional.some('abc')), Optional.some(3));\n *\n * assert.deepStrictEqual(mapToLength(Optional.none), Optional.none);\n * ```\n *\n * @template O The input `UnknownOptional` type.\n * @template S2 The type of the value returned by the mapping function.\n * @param optional The `Optional` to map.\n * @param mapFn The function to apply to the value if it exists.\n * @returns A new `Optional<S2>` resulting from the mapping, or\n * `None` if the input was `None`.\n */\nexport function map<O extends UnknownOptional, S2>(\n optional: O,\n mapFn: (value: Unwrap<O>) => S2,\n): Optional<S2>;\n\n// Curried version\nexport function map<S, S2>(\n mapFn: (value: S) => S2,\n): (optional:
|
|
1
|
+
{"version":3,"file":"optional-map.mjs","sources":["../../../../src/functional/optional/impl/optional-map.mts"],"sourcesContent":["import { type Optional, type UnknownOptional } from '../optional.mjs';\nimport { isSome } from './optional-is-some.mjs';\nimport { none } from './optional-none.mjs';\nimport { some } from './optional-some.mjs';\nimport { unwrap } from './optional-unwrap.mjs';\nimport { type Unwrap } from './types.mjs';\n\n/**\n * Maps an {@link Optional}<S> to {@link Optional}<S2> by applying a function to\n * a contained value. If the {@link Optional} is {@link None}, it\n * returns {@link Optional.none}. Otherwise, it applies the `mapFn` to the\n * value in `Some` and returns a new `Some` with the result.\n *\n * @example\n *\n * ```ts\n * const numberOptional = Optional.some(21);\n *\n * const mapped = Optional.map(numberOptional, (value) => value * 2);\n *\n * assert.deepStrictEqual(mapped, Optional.some(42));\n *\n * const mapToLength = Optional.map((text: string) => text.length);\n *\n * assert.deepStrictEqual(mapToLength(Optional.some('abc')), Optional.some(3));\n *\n * assert.deepStrictEqual(mapToLength(Optional.none), Optional.none);\n * ```\n *\n * @template O The input `UnknownOptional` type.\n * @template S2 The type of the value returned by the mapping function.\n * @param optional The `Optional` to map.\n * @param mapFn The function to apply to the value if it exists.\n * @returns A new `Optional<S2>` resulting from the mapping, or\n * `None` if the input was `None`.\n */\nexport function map<O extends UnknownOptional, S2>(\n optional: O,\n mapFn: (value: Unwrap<O>) => S2,\n): Optional<S2>;\n\n// Curried version\n//\n// The returned function is generic over the *whole* optional, mirroring the\n// direct overload above, rather than fixing its parameter to `Optional<S>`.\n// Fixing it makes the curried form unusable from a caller that is itself\n// generic over the optional: with `S` bound to `Unwrap<O>` at currying time\n// the parameter becomes `Optional<Unwrap<O>>`, and TypeScript cannot show a\n// bare `O extends UnknownOptional` is assignable to that — although every\n// concrete instantiation of `O` is.\n//\n// The conditional is what keeps the check: `O` is inferred from the argument\n// and its value type still has to satisfy `S`, so mapping a `(s: string) =>\n// …` over an `Optional<number>` stays an error. The cost is the message it\n// produces — \"not assignable to `never`\" rather than naming the two optionals.\nexport function map<S, S2>(\n mapFn: (value: S) => S2,\n): <O extends UnknownOptional>(\n optional: Unwrap<O> extends S ? O : never,\n) => Optional<S2>;\n\nexport function map<O extends UnknownOptional, S2>(\n ...args:\n | readonly [optional: O, mapFn: (value: Unwrap<O>) => S2]\n | readonly [mapFn: (value: Unwrap<O>) => S2]\n): Optional<S2> | ((optional: O) => Optional<S2>) {\n switch (args.length) {\n case 2: {\n const [optional, mapFn] = args;\n\n return mapImpl(optional, mapFn);\n }\n\n case 1: {\n // Curried version: first argument is mapping function\n const [mapFn] = args;\n\n return (optional: O) => mapImpl(optional, mapFn);\n }\n }\n}\n\nconst mapImpl = <O extends UnknownOptional, S2>(\n optional: O,\n mapFn: (value: Unwrap<O>) => S2,\n): Optional<S2> =>\n isSome(optional)\n ? // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n some(mapFn(unwrap(optional)!))\n : none;\n"],"names":[],"mappings":";;;;;AA6DO,SAAS,OACX,IAAA,EAG6C;AAChD,EAAA,QAAQ,KAAK,MAAA;AAAQ,IACnB,KAAK,CAAA,EAAG;AACN,MAAA,MAAM,CAAC,QAAA,EAAU,KAAK,CAAA,GAAI,IAAA;AAE1B,MAAA,OAAO,OAAA,CAAQ,UAAU,KAAK,CAAA;AAAA,IAChC;AAAA,IAEA,KAAK,CAAA,EAAG;AAEN,MAAA,MAAM,CAAC,KAAK,CAAA,GAAI,IAAA;AAEhB,MAAA,OAAO,CAAC,QAAA,KAAgB,OAAA,CAAQ,QAAA,EAAU,KAAK,CAAA;AAAA,IACjD;AAAA;AAEJ;AAEA,MAAM,OAAA,GAAU,CACd,QAAA,EACA,KAAA,KAEA,OAAO,QAAQ,CAAA;AAAA;AAAA,EAEX,IAAA,CAAK,KAAA,CAAM,MAAA,CAAO,QAAQ,CAAE,CAAC;AAAA,IAC7B,IAAA;;;;"}
|
|
@@ -35,5 +35,5 @@ import { type UnwrapErr, type UnwrapOk } from './types.mjs';
|
|
|
35
35
|
* @returns A new `Result<UnwrapOk<R>, E2>`.
|
|
36
36
|
*/
|
|
37
37
|
export declare function mapErr<R extends UnknownResult, E2>(result: R, mapFn: (error: UnwrapErr<R>) => E2): Result<UnwrapOk<R>, E2>;
|
|
38
|
-
export declare function mapErr<E, E2>(mapFn: (error: E) => E2): <
|
|
38
|
+
export declare function mapErr<E, E2>(mapFn: (error: E) => E2): <R extends UnknownResult>(result: UnwrapErr<R> extends E ? R : never) => Result<UnwrapOk<R>, E2>;
|
|
39
39
|
//# sourceMappingURL=result-map-err.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"result-map-err.d.mts","sourceRoot":"","sources":["../../../../src/functional/result/impl/result-map-err.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,MAAM,EAAE,KAAK,aAAa,EAAE,MAAM,eAAe,CAAC;AAGhE,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE5D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,MAAM,CAAC,CAAC,SAAS,aAAa,EAAE,EAAE,EAChD,MAAM,EAAE,CAAC,EACT,KAAK,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,KAAK,EAAE,GACjC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"result-map-err.d.mts","sourceRoot":"","sources":["../../../../src/functional/result/impl/result-map-err.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,MAAM,EAAE,KAAK,aAAa,EAAE,MAAM,eAAe,CAAC;AAGhE,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE5D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,MAAM,CAAC,CAAC,SAAS,aAAa,EAAE,EAAE,EAChD,MAAM,EAAE,CAAC,EACT,KAAK,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC,KAAK,EAAE,GACjC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AAgB3B,wBAAgB,MAAM,CAAC,CAAC,EAAE,EAAE,EAC1B,KAAK,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,GACtB,CAAC,CAAC,SAAS,aAAa,EACzB,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,KAAK,KACvC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"result-map-err.mjs","sources":["../../../../src/functional/result/impl/result-map-err.mts"],"sourcesContent":["import { type Ok } from '../../../types.mjs';\nimport { type Result, type UnknownResult } from '../result.mjs';\nimport { err } from './result-err.mjs';\nimport { isOk } from './result-is-ok.mjs';\nimport { type UnwrapErr, type UnwrapOk } from './types.mjs';\n\n/**\n * Maps a `Result<S, E>` to `Result<S, E2>` by applying a function to the\n * error value. If the `Result` is `Result.Ok`, returns the original `Ok`.\n *\n * @example\n *\n * ```ts\n * const okValue = Result.ok(3) as Result<number, string>;\n *\n * const errValue = Result.err('missing');\n *\n * const untouchedOk = Result.mapErr(okValue, (error) => error.toUpperCase());\n *\n * const uppercasedErr = Result.mapErr(errValue, (error) =>\n * error.toUpperCase(),\n * );\n *\n * assert.deepStrictEqual(untouchedOk, Result.ok(3));\n *\n * assert.deepStrictEqual(uppercasedErr, Result.err('MISSING'));\n *\n * const mapError = Result.mapErr((error: Readonly<Error>) => error.message);\n *\n * const wrapped = mapError(Result.err(new Error('boom')));\n *\n * assert.deepStrictEqual(wrapped, Result.err('boom'));\n * ```\n *\n * @template R The input `UnknownResult` type.\n * @template E2 The type of the error value returned by the mapping function.\n * @param result The `Result` to map.\n * @param mapFn The function to apply to the error value if present.\n * @returns A new `Result<UnwrapOk<R>, E2>`.\n */\nexport function mapErr<R extends UnknownResult, E2>(\n result: R,\n mapFn: (error: UnwrapErr<R>) => E2,\n): Result<UnwrapOk<R>, E2>;\n\n// Curried version\nexport function mapErr<E, E2>(\n mapFn: (error: E) => E2,\n): <
|
|
1
|
+
{"version":3,"file":"result-map-err.mjs","sources":["../../../../src/functional/result/impl/result-map-err.mts"],"sourcesContent":["import { type Ok } from '../../../types.mjs';\nimport { type Result, type UnknownResult } from '../result.mjs';\nimport { err } from './result-err.mjs';\nimport { isOk } from './result-is-ok.mjs';\nimport { type UnwrapErr, type UnwrapOk } from './types.mjs';\n\n/**\n * Maps a `Result<S, E>` to `Result<S, E2>` by applying a function to the\n * error value. If the `Result` is `Result.Ok`, returns the original `Ok`.\n *\n * @example\n *\n * ```ts\n * const okValue = Result.ok(3) as Result<number, string>;\n *\n * const errValue = Result.err('missing');\n *\n * const untouchedOk = Result.mapErr(okValue, (error) => error.toUpperCase());\n *\n * const uppercasedErr = Result.mapErr(errValue, (error) =>\n * error.toUpperCase(),\n * );\n *\n * assert.deepStrictEqual(untouchedOk, Result.ok(3));\n *\n * assert.deepStrictEqual(uppercasedErr, Result.err('MISSING'));\n *\n * const mapError = Result.mapErr((error: Readonly<Error>) => error.message);\n *\n * const wrapped = mapError(Result.err(new Error('boom')));\n *\n * assert.deepStrictEqual(wrapped, Result.err('boom'));\n * ```\n *\n * @template R The input `UnknownResult` type.\n * @template E2 The type of the error value returned by the mapping function.\n * @param result The `Result` to map.\n * @param mapFn The function to apply to the error value if present.\n * @returns A new `Result<UnwrapOk<R>, E2>`.\n */\nexport function mapErr<R extends UnknownResult, E2>(\n result: R,\n mapFn: (error: UnwrapErr<R>) => E2,\n): Result<UnwrapOk<R>, E2>;\n\n// Curried version\n//\n// The returned function is generic over the *whole* result, mirroring the\n// direct overload above, rather than fixing its parameter to `Result<S, E>`.\n// Fixing it makes the curried form unusable from a caller that is itself\n// generic over the result: with `E` bound to `UnwrapErr<R>` at currying time the\n// parameter becomes `Result<...>` in terms of `R`, and TypeScript cannot show\n// a bare `R extends UnknownResult` is assignable to that — although every\n// concrete instantiation of `R` is.\n//\n// The conditional is what keeps the check: `R` is inferred from the argument\n// and its error type still has to satisfy `E`, so the mismatched case stays an\n// error. The cost is the message it produces — \"not assignable to `never`\"\n// rather than naming the two results.\nexport function mapErr<E, E2>(\n mapFn: (error: E) => E2,\n): <R extends UnknownResult>(\n result: UnwrapErr<R> extends E ? R : never,\n) => Result<UnwrapOk<R>, E2>;\n\nexport function mapErr<R extends UnknownResult, E2>(\n ...args:\n | readonly [result: R, mapFn: (error: UnwrapErr<R>) => E2]\n | readonly [mapFn: (error: UnwrapErr<R>) => E2]\n): Result<UnwrapOk<R>, E2> | ((result: R) => Result<UnwrapOk<R>, E2>) {\n switch (args.length) {\n case 2: {\n // Direct version: first argument is result\n const [result, mapFn] = args;\n\n return mapErrImpl(result, mapFn);\n }\n\n case 1: {\n // Curried version: first argument is mapping function\n const [mapFn] = args;\n\n return (result: R) => mapErrImpl(result, mapFn);\n }\n }\n}\n\nconst mapErrImpl = <R extends UnknownResult, E2>(\n result: R,\n mapFn: (error: UnwrapErr<R>) => E2,\n): Result<UnwrapOk<R>, E2> =>\n isOk(result)\n ? // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n (result as Ok<UnwrapOk<R>>)\n : // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n err(mapFn(result.value as UnwrapErr<R>));\n"],"names":[],"mappings":";;;AAiEO,SAAS,UACX,IAAA,EAGiE;AACpE,EAAA,QAAQ,KAAK,MAAA;AAAQ,IACnB,KAAK,CAAA,EAAG;AAEN,MAAA,MAAM,CAAC,MAAA,EAAQ,KAAK,CAAA,GAAI,IAAA;AAExB,MAAA,OAAO,UAAA,CAAW,QAAQ,KAAK,CAAA;AAAA,IACjC;AAAA,IAEA,KAAK,CAAA,EAAG;AAEN,MAAA,MAAM,CAAC,KAAK,CAAA,GAAI,IAAA;AAEhB,MAAA,OAAO,CAAC,MAAA,KAAc,UAAA,CAAW,MAAA,EAAQ,KAAK,CAAA;AAAA,IAChD;AAAA;AAEJ;AAEA,MAAM,UAAA,GAAa,CACjB,MAAA,EACA,KAAA,KAEA,KAAK,MAAM,CAAA;AAAA;AAAA,EAEN;AAAA;AAAA;AAAA,EAED,GAAA,CAAI,KAAA,CAAM,MAAA,CAAO,KAAqB,CAAC;AAAA,CAAA;;;;"}
|
|
@@ -35,5 +35,5 @@ import { type UnwrapErr, type UnwrapOk } from './types.mjs';
|
|
|
35
35
|
* @returns A new `Result<S2, UnwrapErr<R>>`.
|
|
36
36
|
*/
|
|
37
37
|
export declare function map<R extends UnknownResult, S2>(result: R, mapFn: (value: UnwrapOk<R>) => S2): Result<S2, UnwrapErr<R>>;
|
|
38
|
-
export declare function map<S, S2>(mapFn: (value: S) => S2): <
|
|
38
|
+
export declare function map<S, S2>(mapFn: (value: S) => S2): <R extends UnknownResult>(result: UnwrapOk<R> extends S ? R : never) => Result<S2, UnwrapErr<R>>;
|
|
39
39
|
//# sourceMappingURL=result-map.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"result-map.d.mts","sourceRoot":"","sources":["../../../../src/functional/result/impl/result-map.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,MAAM,EAAE,KAAK,aAAa,EAAE,MAAM,eAAe,CAAC;AAGhE,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE5D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,GAAG,CAAC,CAAC,SAAS,aAAa,EAAE,EAAE,EAC7C,MAAM,EAAE,CAAC,EACT,KAAK,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,KAAK,EAAE,GAChC,MAAM,CAAC,EAAE,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"result-map.d.mts","sourceRoot":"","sources":["../../../../src/functional/result/impl/result-map.mts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,MAAM,EAAE,KAAK,aAAa,EAAE,MAAM,eAAe,CAAC;AAGhE,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE5D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AACH,wBAAgB,GAAG,CAAC,CAAC,SAAS,aAAa,EAAE,EAAE,EAC7C,MAAM,EAAE,CAAC,EACT,KAAK,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,KAAK,EAAE,GAChC,MAAM,CAAC,EAAE,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;AAgB5B,wBAAgB,GAAG,CAAC,CAAC,EAAE,EAAE,EACvB,KAAK,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,GACtB,CAAC,CAAC,SAAS,aAAa,EACzB,MAAM,EAAE,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,KAAK,KACtC,MAAM,CAAC,EAAE,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"result-map.mjs","sources":["../../../../src/functional/result/impl/result-map.mts"],"sourcesContent":["import { type Err } from '../../../types.mjs';\nimport { type Result, type UnknownResult } from '../result.mjs';\nimport { isErr } from './result-is-err.mjs';\nimport { ok } from './result-ok.mjs';\nimport { type UnwrapErr, type UnwrapOk } from './types.mjs';\n\n/**\n * Maps a `Result<S, E>` to `Result<S2, E>` by applying a function to the\n * success value. If the `Result` is `Result.Err`, returns the original\n * `Err`.\n *\n * @example\n *\n * ```ts\n * const okNumber = Result.ok(5);\n *\n * const errMessage = Result.err('error');\n *\n * const doubled = Result.map(okNumber, (value) => value * 2);\n *\n * const untouchedError = Result.map(errMessage, (value: number) => value * 2);\n *\n * assert.deepStrictEqual(doubled, Result.ok(10));\n *\n * assert.deepStrictEqual(untouchedError, errMessage);\n *\n * const mapToLength = Result.map((text: string) => text.length);\n *\n * assert.deepStrictEqual(mapToLength(Result.ok('abc')), Result.ok(3));\n *\n * assert.deepStrictEqual(mapToLength(Result.err('bad')), Result.err('bad'));\n * ```\n *\n * @template R The input `UnknownResult` type.\n * @template S2 The type of the success value returned by the mapping\n * function.\n * @param result The `Result` to map.\n * @param mapFn The function to apply to the success value if present.\n * @returns A new `Result<S2, UnwrapErr<R>>`.\n */\nexport function map<R extends UnknownResult, S2>(\n result: R,\n mapFn: (value: UnwrapOk<R>) => S2,\n): Result<S2, UnwrapErr<R>>;\n\n// Curried version\nexport function map<S, S2>(\n mapFn: (value: S) => S2,\n): <
|
|
1
|
+
{"version":3,"file":"result-map.mjs","sources":["../../../../src/functional/result/impl/result-map.mts"],"sourcesContent":["import { type Err } from '../../../types.mjs';\nimport { type Result, type UnknownResult } from '../result.mjs';\nimport { isErr } from './result-is-err.mjs';\nimport { ok } from './result-ok.mjs';\nimport { type UnwrapErr, type UnwrapOk } from './types.mjs';\n\n/**\n * Maps a `Result<S, E>` to `Result<S2, E>` by applying a function to the\n * success value. If the `Result` is `Result.Err`, returns the original\n * `Err`.\n *\n * @example\n *\n * ```ts\n * const okNumber = Result.ok(5);\n *\n * const errMessage = Result.err('error');\n *\n * const doubled = Result.map(okNumber, (value) => value * 2);\n *\n * const untouchedError = Result.map(errMessage, (value: number) => value * 2);\n *\n * assert.deepStrictEqual(doubled, Result.ok(10));\n *\n * assert.deepStrictEqual(untouchedError, errMessage);\n *\n * const mapToLength = Result.map((text: string) => text.length);\n *\n * assert.deepStrictEqual(mapToLength(Result.ok('abc')), Result.ok(3));\n *\n * assert.deepStrictEqual(mapToLength(Result.err('bad')), Result.err('bad'));\n * ```\n *\n * @template R The input `UnknownResult` type.\n * @template S2 The type of the success value returned by the mapping\n * function.\n * @param result The `Result` to map.\n * @param mapFn The function to apply to the success value if present.\n * @returns A new `Result<S2, UnwrapErr<R>>`.\n */\nexport function map<R extends UnknownResult, S2>(\n result: R,\n mapFn: (value: UnwrapOk<R>) => S2,\n): Result<S2, UnwrapErr<R>>;\n\n// Curried version\n//\n// The returned function is generic over the *whole* result, mirroring the\n// direct overload above, rather than fixing its parameter to `Result<S, E>`.\n// Fixing it makes the curried form unusable from a caller that is itself\n// generic over the result: with `S` bound to `UnwrapOk<R>` at currying time the\n// parameter becomes `Result<...>` in terms of `R`, and TypeScript cannot show\n// a bare `R extends UnknownResult` is assignable to that — although every\n// concrete instantiation of `R` is.\n//\n// The conditional is what keeps the check: `R` is inferred from the argument\n// and its success type still has to satisfy `S`, so the mismatched case stays an\n// error. The cost is the message it produces — \"not assignable to `never`\"\n// rather than naming the two results.\nexport function map<S, S2>(\n mapFn: (value: S) => S2,\n): <R extends UnknownResult>(\n result: UnwrapOk<R> extends S ? R : never,\n) => Result<S2, UnwrapErr<R>>;\n\nexport function map<R extends UnknownResult, S2>(\n ...args:\n | readonly [result: R, mapFn: (value: UnwrapOk<R>) => S2]\n | readonly [mapFn: (value: UnwrapOk<R>) => S2]\n): Result<S2, UnwrapErr<R>> | ((result: R) => Result<S2, UnwrapErr<R>>) {\n switch (args.length) {\n case 2: {\n // Direct version: first argument is result\n const [result, mapFn] = args;\n\n return mapImpl(result, mapFn);\n }\n\n case 1: {\n // Curried version\n const [mapFn] = args;\n\n return (result: R) => mapImpl(result, mapFn);\n }\n }\n}\n\nconst mapImpl = <R extends UnknownResult, S2>(\n result: R,\n mapFn: (value: UnwrapOk<R>) => S2,\n): Result<S2, UnwrapErr<R>> =>\n isErr(result)\n ? // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n (result as Err<UnwrapErr<R>>)\n : // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n ok(mapFn(result.value as UnwrapOk<R>));\n"],"names":[],"mappings":";;;AAiEO,SAAS,OACX,IAAA,EAGmE;AACtE,EAAA,QAAQ,KAAK,MAAA;AAAQ,IACnB,KAAK,CAAA,EAAG;AAEN,MAAA,MAAM,CAAC,MAAA,EAAQ,KAAK,CAAA,GAAI,IAAA;AAExB,MAAA,OAAO,OAAA,CAAQ,QAAQ,KAAK,CAAA;AAAA,IAC9B;AAAA,IAEA,KAAK,CAAA,EAAG;AAEN,MAAA,MAAM,CAAC,KAAK,CAAA,GAAI,IAAA;AAEhB,MAAA,OAAO,CAAC,MAAA,KAAc,OAAA,CAAQ,MAAA,EAAQ,KAAK,CAAA;AAAA,IAC7C;AAAA;AAEJ;AAEA,MAAM,OAAA,GAAU,CACd,MAAA,EACA,KAAA,KAEA,MAAM,MAAM,CAAA;AAAA;AAAA,EAEP;AAAA;AAAA;AAAA,EAED,EAAA,CAAG,KAAA,CAAM,MAAA,CAAO,KAAoB,CAAC;AAAA,CAAA;;;;"}
|
package/package.json
CHANGED
|
@@ -40,9 +40,24 @@ export function map<O extends UnknownOptional, S2>(
|
|
|
40
40
|
): Optional<S2>;
|
|
41
41
|
|
|
42
42
|
// Curried version
|
|
43
|
+
//
|
|
44
|
+
// The returned function is generic over the *whole* optional, mirroring the
|
|
45
|
+
// direct overload above, rather than fixing its parameter to `Optional<S>`.
|
|
46
|
+
// Fixing it makes the curried form unusable from a caller that is itself
|
|
47
|
+
// generic over the optional: with `S` bound to `Unwrap<O>` at currying time
|
|
48
|
+
// the parameter becomes `Optional<Unwrap<O>>`, and TypeScript cannot show a
|
|
49
|
+
// bare `O extends UnknownOptional` is assignable to that — although every
|
|
50
|
+
// concrete instantiation of `O` is.
|
|
51
|
+
//
|
|
52
|
+
// The conditional is what keeps the check: `O` is inferred from the argument
|
|
53
|
+
// and its value type still has to satisfy `S`, so mapping a `(s: string) =>
|
|
54
|
+
// …` over an `Optional<number>` stays an error. The cost is the message it
|
|
55
|
+
// produces — "not assignable to `never`" rather than naming the two optionals.
|
|
43
56
|
export function map<S, S2>(
|
|
44
57
|
mapFn: (value: S) => S2,
|
|
45
|
-
):
|
|
58
|
+
): <O extends UnknownOptional>(
|
|
59
|
+
optional: Unwrap<O> extends S ? O : never,
|
|
60
|
+
) => Optional<S2>;
|
|
46
61
|
|
|
47
62
|
export function map<O extends UnknownOptional, S2>(
|
|
48
63
|
...args:
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { expectType } from '../expect-type.mjs';
|
|
2
2
|
import { Num } from '../number/index.mjs';
|
|
3
3
|
import { type None, type Some } from '../types.mjs';
|
|
4
|
-
import { Optional } from './optional/index.mjs';
|
|
4
|
+
import { Optional, type UnknownOptional } from './optional/index.mjs';
|
|
5
5
|
import { pipe } from './pipe.mjs';
|
|
6
6
|
import { Result } from './result/index.mjs';
|
|
7
7
|
|
|
@@ -144,6 +144,53 @@ describe('Optional test', () => {
|
|
|
144
144
|
}
|
|
145
145
|
});
|
|
146
146
|
|
|
147
|
+
test('curried form is usable from a caller generic over the optional', () => {
|
|
148
|
+
// The motivating case: with the curried parameter fixed to
|
|
149
|
+
// `Optional<S>`, `S` binds to `Optional.Unwrap<O>` here and a bare
|
|
150
|
+
// `O extends UnknownOptional` is not assignable to
|
|
151
|
+
// `Optional<Optional.Unwrap<O>>`, so this only compiles because the
|
|
152
|
+
// returned function is generic over the whole optional.
|
|
153
|
+
const liftMap =
|
|
154
|
+
<O extends UnknownOptional, B>(mapFn: (x: Optional.Unwrap<O>) => B) =>
|
|
155
|
+
(optional: O): Optional<B> =>
|
|
156
|
+
Optional.map(mapFn)(optional);
|
|
157
|
+
|
|
158
|
+
const lengthOf = liftMap<Optional<string>, number>((s) => s.length);
|
|
159
|
+
|
|
160
|
+
const mapped = lengthOf(Optional.some('abc'));
|
|
161
|
+
|
|
162
|
+
expectType<typeof mapped, Optional<number>>('=');
|
|
163
|
+
|
|
164
|
+
assert.deepStrictEqual(mapped, Optional.some(3));
|
|
165
|
+
|
|
166
|
+
assert.deepStrictEqual(lengthOf(Optional.none), Optional.none);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
test('curried form still infers the result precisely', () => {
|
|
170
|
+
const mapToLength = Optional.map((text: string) => text.length);
|
|
171
|
+
|
|
172
|
+
const mapped = mapToLength(Optional.some('abc'));
|
|
173
|
+
|
|
174
|
+
expectType<typeof mapped, Optional<number>>('=');
|
|
175
|
+
|
|
176
|
+
assert.deepStrictEqual(mapped, Optional.some(3));
|
|
177
|
+
|
|
178
|
+
const empty = mapToLength(Optional.none);
|
|
179
|
+
|
|
180
|
+
expectType<typeof empty, Optional<number>>('=');
|
|
181
|
+
|
|
182
|
+
assert.deepStrictEqual(empty, Optional.none);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test('curried form still rejects a mismatched value type', () => {
|
|
186
|
+
const mapToLength = Optional.map((text: string) => text.length);
|
|
187
|
+
|
|
188
|
+
// @ts-expect-error an Optional<number> is not an Optional<string>
|
|
189
|
+
const rejected = (): unknown => mapToLength(Optional.some(42));
|
|
190
|
+
|
|
191
|
+
expect(rejected).toBeTypeOf('function');
|
|
192
|
+
});
|
|
193
|
+
|
|
147
194
|
test('should preserve types correctly', () => {
|
|
148
195
|
const some = Optional.some(42);
|
|
149
196
|
|
|
@@ -44,9 +44,24 @@ export function mapErr<R extends UnknownResult, E2>(
|
|
|
44
44
|
): Result<UnwrapOk<R>, E2>;
|
|
45
45
|
|
|
46
46
|
// Curried version
|
|
47
|
+
//
|
|
48
|
+
// The returned function is generic over the *whole* result, mirroring the
|
|
49
|
+
// direct overload above, rather than fixing its parameter to `Result<S, E>`.
|
|
50
|
+
// Fixing it makes the curried form unusable from a caller that is itself
|
|
51
|
+
// generic over the result: with `E` bound to `UnwrapErr<R>` at currying time the
|
|
52
|
+
// parameter becomes `Result<...>` in terms of `R`, and TypeScript cannot show
|
|
53
|
+
// a bare `R extends UnknownResult` is assignable to that — although every
|
|
54
|
+
// concrete instantiation of `R` is.
|
|
55
|
+
//
|
|
56
|
+
// The conditional is what keeps the check: `R` is inferred from the argument
|
|
57
|
+
// and its error type still has to satisfy `E`, so the mismatched case stays an
|
|
58
|
+
// error. The cost is the message it produces — "not assignable to `never`"
|
|
59
|
+
// rather than naming the two results.
|
|
47
60
|
export function mapErr<E, E2>(
|
|
48
61
|
mapFn: (error: E) => E2,
|
|
49
|
-
): <
|
|
62
|
+
): <R extends UnknownResult>(
|
|
63
|
+
result: UnwrapErr<R> extends E ? R : never,
|
|
64
|
+
) => Result<UnwrapOk<R>, E2>;
|
|
50
65
|
|
|
51
66
|
export function mapErr<R extends UnknownResult, E2>(
|
|
52
67
|
...args:
|
|
@@ -44,9 +44,24 @@ export function map<R extends UnknownResult, S2>(
|
|
|
44
44
|
): Result<S2, UnwrapErr<R>>;
|
|
45
45
|
|
|
46
46
|
// Curried version
|
|
47
|
+
//
|
|
48
|
+
// The returned function is generic over the *whole* result, mirroring the
|
|
49
|
+
// direct overload above, rather than fixing its parameter to `Result<S, E>`.
|
|
50
|
+
// Fixing it makes the curried form unusable from a caller that is itself
|
|
51
|
+
// generic over the result: with `S` bound to `UnwrapOk<R>` at currying time the
|
|
52
|
+
// parameter becomes `Result<...>` in terms of `R`, and TypeScript cannot show
|
|
53
|
+
// a bare `R extends UnknownResult` is assignable to that — although every
|
|
54
|
+
// concrete instantiation of `R` is.
|
|
55
|
+
//
|
|
56
|
+
// The conditional is what keeps the check: `R` is inferred from the argument
|
|
57
|
+
// and its success type still has to satisfy `S`, so the mismatched case stays an
|
|
58
|
+
// error. The cost is the message it produces — "not assignable to `never`"
|
|
59
|
+
// rather than naming the two results.
|
|
47
60
|
export function map<S, S2>(
|
|
48
61
|
mapFn: (value: S) => S2,
|
|
49
|
-
): <
|
|
62
|
+
): <R extends UnknownResult>(
|
|
63
|
+
result: UnwrapOk<R> extends S ? R : never,
|
|
64
|
+
) => Result<S2, UnwrapErr<R>>;
|
|
50
65
|
|
|
51
66
|
export function map<R extends UnknownResult, S2>(
|
|
52
67
|
...args:
|
|
@@ -3,7 +3,7 @@ import { Num } from '../number/index.mjs';
|
|
|
3
3
|
import { type Err, type Ok } from '../types.mjs';
|
|
4
4
|
import { Optional } from './optional/index.mjs';
|
|
5
5
|
import { pipe } from './pipe.mjs';
|
|
6
|
-
import { Result } from './result/index.mjs';
|
|
6
|
+
import { Result, type UnknownResult } from './result/index.mjs';
|
|
7
7
|
|
|
8
8
|
describe('Result test', () => {
|
|
9
9
|
describe('ok', () => {
|
|
@@ -174,6 +174,49 @@ describe('Result test', () => {
|
|
|
174
174
|
|
|
175
175
|
expect(result.value).toBe('10');
|
|
176
176
|
});
|
|
177
|
+
|
|
178
|
+
test('curried form is usable from a caller generic over the result', () => {
|
|
179
|
+
// The motivating case: with the curried parameter fixed to
|
|
180
|
+
// `Result<S, E>`, `S` binds to `Result.UnwrapOk<R>` here and a bare
|
|
181
|
+
// `R extends UnknownResult` is not assignable to that, so this only
|
|
182
|
+
// compiles because the returned function is generic over the whole
|
|
183
|
+
// result.
|
|
184
|
+
const liftMap =
|
|
185
|
+
<R extends UnknownResult, S2>(mapFn: (x: Result.UnwrapOk<R>) => S2) =>
|
|
186
|
+
(result: R): Result<S2, Result.UnwrapErr<R>> =>
|
|
187
|
+
Result.map(mapFn)(result);
|
|
188
|
+
|
|
189
|
+
const lengthOf = liftMap<Result<string, 'boom'>, number>((s) => s.length);
|
|
190
|
+
|
|
191
|
+
const mapped = lengthOf(Result.ok('abc'));
|
|
192
|
+
|
|
193
|
+
expectType<typeof mapped, Result<number, 'boom'>>('=');
|
|
194
|
+
|
|
195
|
+
assert.deepStrictEqual(mapped, Result.ok(3));
|
|
196
|
+
|
|
197
|
+
assert.deepStrictEqual(lengthOf(Result.err('boom')), Result.err('boom'));
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
test('curried form keeps the error type and rejects a mismatch', () => {
|
|
201
|
+
const mapToLength = Result.map((text: string) => text.length);
|
|
202
|
+
|
|
203
|
+
const mapped = mapToLength(Result.ok('abc'));
|
|
204
|
+
|
|
205
|
+
expectType<typeof mapped, Result<number, never>>('=');
|
|
206
|
+
|
|
207
|
+
assert.deepStrictEqual(mapped, Result.ok(3));
|
|
208
|
+
|
|
209
|
+
const failed = mapToLength(Result.err('boom' as const));
|
|
210
|
+
|
|
211
|
+
expectType<typeof failed, Result<number, 'boom'>>('=');
|
|
212
|
+
|
|
213
|
+
assert.deepStrictEqual(failed, Result.err('boom'));
|
|
214
|
+
|
|
215
|
+
// @ts-expect-error a Result<number, …> is not a Result<string, …>
|
|
216
|
+
const rejected = (): unknown => mapToLength(Result.ok(42));
|
|
217
|
+
|
|
218
|
+
expect(rejected).toBeTypeOf('function');
|
|
219
|
+
});
|
|
177
220
|
});
|
|
178
221
|
|
|
179
222
|
describe('mapErr', () => {
|
|
@@ -234,6 +277,40 @@ describe('Result test', () => {
|
|
|
234
277
|
|
|
235
278
|
expect(result.value).toBe('ERROR: FAILED');
|
|
236
279
|
});
|
|
280
|
+
|
|
281
|
+
test('curried form is usable from a caller generic over the result', () => {
|
|
282
|
+
const liftMapErr =
|
|
283
|
+
<R extends UnknownResult, E2>(mapFn: (x: Result.UnwrapErr<R>) => E2) =>
|
|
284
|
+
(result: R): Result<Result.UnwrapOk<R>, E2> =>
|
|
285
|
+
Result.mapErr(mapFn)(result);
|
|
286
|
+
|
|
287
|
+
const shout = liftMapErr<Result<number, string>, string>((e) =>
|
|
288
|
+
e.toUpperCase(),
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
const mapped = shout(Result.err('failed'));
|
|
292
|
+
|
|
293
|
+
expectType<typeof mapped, Result<number, string>>('=');
|
|
294
|
+
|
|
295
|
+
assert.deepStrictEqual(mapped, Result.err('FAILED'));
|
|
296
|
+
|
|
297
|
+
assert.deepStrictEqual(shout(Result.ok(1)), Result.ok(1));
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
test('curried form keeps the ok type and rejects a mismatch', () => {
|
|
301
|
+
const shout = Result.mapErr((e: string) => e.toUpperCase());
|
|
302
|
+
|
|
303
|
+
const mapped = shout(Result.err('failed'));
|
|
304
|
+
|
|
305
|
+
expectType<typeof mapped, Result<never, string>>('=');
|
|
306
|
+
|
|
307
|
+
assert.deepStrictEqual(mapped, Result.err('FAILED'));
|
|
308
|
+
|
|
309
|
+
// @ts-expect-error a Result<…, number> is not a Result<…, string>
|
|
310
|
+
const rejected = (): unknown => shout(Result.err(42));
|
|
311
|
+
|
|
312
|
+
expect(rejected).toBeTypeOf('function');
|
|
313
|
+
});
|
|
237
314
|
});
|
|
238
315
|
|
|
239
316
|
describe('unwrapThrow', () => {
|