@consolidados/results
Version:
Result types, ease and simple
1 lines • 12.8 kB
Source Map (JSON)
{"version":3,"sources":["../../src/result/__internal__/return-types/err.ts","../../src/result/__internal__/return-types/ok.ts","../../src/result/result.ts"],"names":["Ok","Err"],"mappings":";;;AAOO,IAAM,GAAA,GAAN,MAAM,IAA6C,CAAA;AAAA,EAChD,KAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAKR,YAAY,KAAU,EAAA;AAEpB,IAAA,IAAA,CAAK,KAAQ,GAAA,KAAA;AAAA;AAMf;AAAA;AAAA;AAAA;AAAA,EAMA,IAA0B,GAAA;AACxB,IAAO,OAAA,KAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA,EAMA,KAAwB,GAAA;AACtB,IAAO,OAAA,IAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA,EAMA,MAAgB,GAAA;AACd,IAAM,MAAA,IAAI,MAAM,+BAA+B,CAAA;AAAA;AACjD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAO,GAAsD,EAAA;AAC3D,IAAO,OAAA,IAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAU,EAA+C,EAAA;AACvD,IAAA,OAAO,IAAI,IAAA,CAAO,EAAG,CAAA,IAAA,CAAK,KAAK,CAAC,CAAA;AAAA;AAClC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,QACE,GACwB,EAAA;AACxB,IAAO,OAAA,IAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA,EAMA,SAAe,GAAA;AACb,IAAA,OAAO,IAAK,CAAA,KAAA;AAAA;AACd;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,KAAW,GAAA;AACT,IAAA,OAAO,IAAK,CAAA,KAAA;AAAA;AACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,SAAY,YAAoB,EAAA;AAC9B,IAAO,OAAA,YAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA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,IAAM,MAAA,IAAI,MAAM,iCAAiC,CAAA;AAAA;AACnD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,SAAY,YAAoB,EAAA;AAC9B,IAAA,OAAO,IAAK,CAAA,MAAA;AAAA;AACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,aAAgB,EAA4B,EAAA;AAC1C,IAAA,OAAO,IAAK,CAAA,MAAA;AAAA;AACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAU,EAAsE,EAAA;AAC9E,IAAO,OAAA,IAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA,EAMA,EAAK,GAAA;AACH,IAAO,OAAA,IAAA,CAAK,KAAK,MAAM,CAAA;AAAA;AAE3B,CAAA;;;AC5GA,SAASA,IAAM,KAAqB,EAAA;AAClC,EAAO,OAAA,IAAI,GAAO,KAAK,CAAA;AACzB;AAKA,SAASC,KAAO,KAAsB,EAAA;AACpC,EAAO,OAAA,IAAI,IAAQ,KAAK,CAAA;AAC1B;AAGC,UAAA,CAAmB,EAAKD,GAAAA,GAAAA;AAExB,UAAA,CAAmB,GAAMC,GAAAA,IAAAA","file":"index.cjs","sourcesContent":["import type { Ok } from \"./ok\";\nimport type { ResultDefinition } from \"./result\";\n\n/**\n * Represents a failed result (`Err`) that contains an error value.\n * @template E The type of the error contained in this `Err`. Can be any type (Error, string, enum, etc).\n */\nexport class Err<E> implements ResultDefinition<never, E> {\n private error: E;\n /**\n * Creates a new `Err` instance with the given error value.\n * @param error The error to wrap in the `Err` instance. Can be any type.\n */\n constructor(error: E) {\n // Store error as-is - conversion is handled by factory function\n this.error = error;\n // Object.setPrototypeOf(this, Err.prototype);\n //\n // if (Error.captureStackTrace) {\n // Error.captureStackTrace(this, Err);\n // }\n }\n\n /**\n * Checks if this result is an `Ok`.\n * @returns `false` because this is an `Err`.\n */\n isOk(): this is Ok<never> {\n return false;\n }\n\n /**\n * Checks if this result is an `Err`.\n * @returns `true` because this is an `Err`.\n */\n isErr(): this is Err<E> {\n return true;\n }\n\n /**\n * Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.\n * @throws An error because `unwrap` is called on an `Err`.\n */\n unwrap(): never {\n throw new Error(\"Called unwrap on an Err value\");\n }\n\n /**\n * Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.\n * @template U The type of the value (ignored for `Err`).\n * @param _fn The mapping function for values (not used).\n * @returns The original `Err` instance.\n */\n map<U>(_fn: (value: never) => U): ResultDefinition<never, E> {\n return this as unknown as ResultDefinition<never, E>;\n }\n\n /**\n * Maps the error value using a transformation function and returns a new `Result` with the transformed error.\n * @template U The type of the transformed error. Can be any type.\n * @param fn The transformation function to apply to the error value.\n * @returns A new `Err` containing the transformed error.\n */\n mapErr<U>(fn: (err: E) => U): ResultDefinition<never, U> {\n return new Err<U>(fn(this.error)) as unknown as ResultDefinition<never, U>;\n }\n\n /**\n * Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.\n * @template U The type of the value in the resulting `Result`.\n * @template F The type of the error in the resulting `Result`.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns The original `Err` instance cast to the new error type.\n */\n flatMap<U, F = E>(\n _fn: (value: never) => ResultDefinition<U, F>,\n ): ResultDefinition<U, E> {\n return this as unknown as ResultDefinition<U, E>;\n }\n\n /**\n * Retrieves the error value contained in this `Err`.\n * @returns The error value contained in this `Err`.\n */\n unwrapErr(): E {\n return this.error;\n }\n\n /**\n * Returns the inner error. After an `isErr()` type guard, TypeScript narrows the return type to `E`.\n * Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing).\n * @returns The error value contained in this `Err`.\n */\n value(): E {\n return this.error;\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise returns the provided default value.\n * @template U The type of the default value.\n * @param defaultValue The value to return since this is an `Err`.\n * @returns The provided default value.\n */\n unwrapOr<U>(defaultValue: U): U {\n return defaultValue;\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function.\n * @template U The type of the default value.\n * @param fn The function to compute the default value.\n * @returns The result of calling the provided function with the error.\n */\n unwrapOrElse<U>(fn: (error: E) => U): U {\n return fn(this.error);\n }\n\n /**\n * Returns this `Err` if it is `Err`, otherwise returns the result of the provided function.\n * @template T The type of the alternative success value.\n * @template F The type of the alternative error.\n * @param fn The function to compute the alternative result.\n * @returns The result of calling the provided function with the error.\n */\n orElse<T, F>(\n fn: (error: E) => ResultDefinition<T, F>,\n ): ResultDefinition<T, F> {\n return fn(this.error);\n }\n\n /**\n * Converts `Result` type to `Option` type.\n * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.\n */\n ok() {\n return None();\n }\n}\n","import type { ResultDefinition } from \"./result\";\nimport type { Err } from \"./err\";\n\n/**\n * Represents a successful result (`Ok`) that contains a value.\n * @template T The type of the value contained in this `Ok`.\n */\nexport class Ok<T> implements ResultDefinition<T, never> {\n /**\n * Creates a new `Ok` instance with the given value.\n * @param _value The value to wrap in the `Ok` instance.\n */\n constructor(private _value: T) {}\n /**\n * Checks if this result is an `Ok`.\n * @returns `true` because this is an `Ok`.\n */\n isOk(): this is Ok<T> {\n return true;\n }\n\n /**\n * Checks if this result is an `Err`.\n * @returns `false` because this is an `Ok`.\n */\n isErr(): this is Err<never> {\n return false;\n }\n\n /**\n * Retrieves the value contained in this `Ok`.\n * @returns The value contained in this `Ok`.\n */\n unwrap(): T {\n return this._value;\n }\n\n /**\n * Returns the inner value. After an `isOk()` type guard, TypeScript narrows the return type to `T`.\n * Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing).\n * @returns The value contained in this `Ok`.\n */\n value(): T {\n return this._value;\n }\n\n /**\n * Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.\n * @template U The type of the transformed value.\n * @param fn The transformation function to apply to the value.\n * @returns A new `Ok` containing the transformed value.\n */\n map<U>(fn: (value: T) => U): ResultDefinition<U, never> {\n return new Ok(fn(this._value)) as unknown as ResultDefinition<U, never>;\n }\n\n /**\n * Applies a transformation function that returns a `Result` to the value contained in this `Ok`.\n * @template U The type of the value in the resulting `Result`.\n * @template E The type of the error in the resulting `Result`.\n * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U, E = never>(\n fn: (value: T) => ResultDefinition<U, E>,\n ): ResultDefinition<U, E> {\n return fn(this._value);\n }\n\n /**\n * Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.\n * @template U The type of the error (ignored for `Ok`). Can be any type.\n * @param _fn The mapping function for errors (not used).\n * @returns The original `Ok` instance.\n */\n mapErr<U>(_fn: (err: never) => U): ResultDefinition<T, U> {\n return this as unknown as ResultDefinition<T, U>;\n }\n\n /**\n * Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.\n * @throws An error because `unwrapErr` is called on an `Ok`.\n */\n unwrapErr(): never {\n throw new Error(\"Called unwrapErr on an Ok value\");\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise returns the provided default value.\n * @template U The type of the default value.\n * @param defaultValue The value to return if this is an `Err`.\n * @returns The value contained in this `Ok`.\n */\n unwrapOr<U>(defaultValue: U): T {\n return this._value;\n }\n\n /**\n * Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function.\n * @template U The type of the default value.\n * @param fn The function to compute the default value.\n * @returns The value contained in this `Ok`.\n */\n unwrapOrElse<U>(fn: (error: never) => U): T {\n return this._value;\n }\n\n /**\n * Returns this `Ok` if it is `Ok`, otherwise returns the result of the provided function.\n * @template F The type of the alternative error.\n * @param fn The function to compute the alternative result.\n * @returns The original `Ok` instance.\n */\n orElse<F>(fn: (error: never) => ResultDefinition<T, F>): ResultDefinition<T, F> {\n return this as unknown as ResultDefinition<T, F>;\n }\n\n /**\n * Converts `Result` type to `Option` type.\n * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.\n */\n ok() {\n return Some(this._value);\n }\n}\n","import {\n Err as ErrType,\n Ok as OkType,\n type Result,\n} from \"./__internal__/return-types\";\n\n/**\n * Creates a new `Ok` instance, representing a successful result.\n * @template T The type of the value contained in the `Ok`.\n * @param value The value to wrap in the `Ok` instance.\n * @returns An `Ok` instance containing the given value.\n * @example\n * const result = Ok(42);\n * console.log(result.isOk()); // true\n * console.log(result.unwrap()); // 42\n */\nfunction Ok<T>(value: T): OkType<T> {\n return new OkType(value);\n}\n\n/**\n * Creates an Err - strings are converted to Error, everything else preserved\n */\nfunction Err<E>(error: E): ErrType<E> {\n return new ErrType(error);\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(globalThis as any).Ok = Ok;\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(globalThis as any).Err = Err;\n\nexport { Err, Ok, Result, ErrType, OkType };\n"]}