@consolidados/results 0.1.3 → 0.1.4

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 CHANGED
@@ -309,12 +309,13 @@ The `Result` type currently implements the following methods:
309
309
  - [x] `map(fn)`: Maps a successful value using a function.
310
310
  - [x] `flatMap(fn)`: Applies a function that returns a `Result`.
311
311
  - [x] `mapErr(fn)`: Maps an error value using a function.
312
+ - [x] `ok()`: Converts `Result<T, E>` into `Option<T>`.
313
+
312
314
 
313
315
  #### Methods to be Developed
314
316
 
315
317
  The following methods are planned for future development:
316
318
 
317
- - [ ] `expect(message)`: Extracts the successful value or throws an error with a custom message.
318
319
  - [ ] `ok()`: Converts `Result<T, E>` into `Option<T>`.
319
320
  - [ ] `err()`: Converts `Result<T, E>` into `Option<E>`.
320
321
  - [ ] `and(res)`: Returns `Err` if `self` is `Err`, otherwise returns `res`.
@@ -1,5 +1,5 @@
1
1
  import { O as Option } from '../option-DpT8KCGE.cjs';
2
- import { R as Result } from '../err-B7LUEZ0f.cjs';
2
+ import { R as Result } from '../types/globals.cjs';
3
3
 
4
4
  declare function match<T, E extends Error, R>(matcher: Result<T, E>, cases: {
5
5
  Ok: (value: T) => R;
@@ -1,5 +1,5 @@
1
1
  import { O as Option } from '../option-DpT8KCGE.js';
2
- import { R as Result } from '../err-B7LUEZ0f.js';
2
+ import { R as Result } from '../types/globals.js';
3
3
 
4
4
  declare function match<T, E extends Error, R>(matcher: Result<T, E>, cases: {
5
5
  Ok: (value: T) => R;
package/dist/index.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  // src/helpers/match.ts
4
- function match(matcher, cases) {
4
+ function match2(matcher, cases) {
5
5
  if ("isOk" in matcher && matcher.isOk()) {
6
6
  if (!cases.Ok) throw new Error("Missing case for Ok");
7
7
  return cases.Ok(matcher.unwrap());
@@ -20,10 +20,10 @@ function match(matcher, cases) {
20
20
  }
21
21
  throw new Error("Invalid matcher or missing case");
22
22
  }
23
- global.match = match;
23
+ global.match = match2;
24
24
 
25
25
  // src/option/__internal__/return-types/some.ts
26
- var Some = class _Some {
26
+ var Some2 = class _Some {
27
27
  /**
28
28
  * Creates a new `Some` option with the given value.
29
29
  * @param value The value to be wrapped in the `Some` option.
@@ -81,7 +81,7 @@ var Some = class _Some {
81
81
  };
82
82
 
83
83
  // src/option/__internal__/return-types/none.ts
84
- var None = class _None {
84
+ var None2 = class _None {
85
85
  /**
86
86
  * Checks if this option is a `Some`.
87
87
  * @returns `false` because this is a `None`.
@@ -133,14 +133,14 @@ var None = class _None {
133
133
  };
134
134
 
135
135
  // src/option/option.ts
136
- function Some2(value) {
137
- return new Some(value);
136
+ function Some3(value) {
137
+ return new Some2(value);
138
138
  }
139
- function None2() {
140
- return new None();
139
+ function None3() {
140
+ return new None2();
141
141
  }
142
- global.Some = Some2;
143
- global.None = None2;
142
+ global.Some = Some3;
143
+ global.None = None3;
144
144
 
145
145
  // src/result/__internal__/return-types/err.ts
146
146
  var Err = class _Err extends Error {
@@ -212,6 +212,13 @@ var Err = class _Err extends Error {
212
212
  unwrapErr() {
213
213
  return this.error;
214
214
  }
215
+ /**
216
+ * Converts `Result` type to `Option` type.
217
+ * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
218
+ */
219
+ ok() {
220
+ return None();
221
+ }
215
222
  };
216
223
 
217
224
  // src/result/__internal__/return-types/ok.ts
@@ -280,6 +287,16 @@ var Ok = class _Ok {
280
287
  unwrapErr() {
281
288
  throw new Error("Called unwrapErr on an Ok value");
282
289
  }
290
+ /**
291
+ * Converts `Result` type to `Option` type.
292
+ * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
293
+ */
294
+ ok() {
295
+ return match(this, {
296
+ Ok: (v) => Some(v),
297
+ Err: (_) => None()
298
+ });
299
+ }
283
300
  };
284
301
 
285
302
  // src/result/result.ts
@@ -292,10 +309,8 @@ function Err2(error) {
292
309
  global.Ok = Ok2;
293
310
  global.Err = Err2;
294
311
 
295
- exports.Err = Err2;
296
- exports.None = None2;
297
- exports.Ok = Ok2;
298
- exports.Some = Some2;
299
- exports.match = match;
312
+ exports.None = None3;
313
+ exports.Some = Some3;
314
+ exports.match = match2;
300
315
  //# sourceMappingURL=index.cjs.map
301
316
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/helpers/match.ts","../src/option/__internal__/return-types/some.ts","../src/option/__internal__/return-types/none.ts","../src/option/option.ts","../src/result/__internal__/return-types/err.ts","../src/result/__internal__/return-types/ok.ts","../src/result/result.ts"],"names":["Some","None","Ok","Err"],"mappings":";;;AAiBO,SAAS,KAAA,CACd,SACA,KAMG,EAAA;AACH,EAAA,IAAI,MAAU,IAAA,OAAA,IAAW,OAAQ,CAAA,IAAA,EAAQ,EAAA;AACvC,IAAA,IAAI,CAAC,KAAM,CAAA,EAAA,EAAU,MAAA,IAAI,MAAM,qBAAqB,CAAA;AACpD,IAAA,OAAO,KAAM,CAAA,EAAA,CAAG,OAAQ,CAAA,MAAA,EAAQ,CAAA;AAAA;AAGlC,EAAA,IAAI,OAAW,IAAA,OAAA,IAAW,OAAQ,CAAA,KAAA,EAAS,EAAA;AACzC,IAAA,IAAI,CAAC,KAAM,CAAA,GAAA,EAAW,MAAA,IAAI,MAAM,sBAAsB,CAAA;AACtD,IAAA,OAAO,KAAM,CAAA,GAAA,CAAI,OAAQ,CAAA,SAAA,EAAgB,CAAA;AAAA;AAG3C,EAAA,IAAI,QAAY,IAAA,OAAA,IAAW,OAAQ,CAAA,MAAA,EAAU,EAAA;AAC3C,IAAA,IAAI,CAAC,KAAM,CAAA,IAAA,EAAY,MAAA,IAAI,MAAM,uBAAuB,CAAA;AACxD,IAAA,OAAO,KAAM,CAAA,IAAA,CAAK,OAAQ,CAAA,MAAA,EAAQ,CAAA;AAAA;AAGpC,EAAA,IAAI,QAAY,IAAA,OAAA,IAAW,OAAQ,CAAA,MAAA,EAAU,EAAA;AAC3C,IAAA,I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G,KAAK,KAAK,CAAA;AAAA;AACtB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,OAAwB,GAAgD,EAAA;AACtE,IAAO,OAAA,IAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA,EAMA,SAAmB,GAAA;AACjB,IAAM,MAAA,IAAI,MAAM,iCAAiC,CAAA;AAAA;AAErD,CAAA;;;AC9DA,SAASC,IAAM,KAAqB,EAAA;AACnC,EAAO,OAAA,IAAI,GAAO,KAAK,CAAA;AACxB;AAYA,SAASC,KAAqB,KAA+B,EAAA;AAC5D,EAAO,OAAA,IAAI,IAAQ,KAAK,CAAA;AACzB;AAEC,MAAA,CAAe,EAAKD,GAAAA,GAAAA;AACpB,MAAA,CAAe,GAAMC,GAAAA,IAAAA","file":"index.cjs","sourcesContent":["import type { Option } from \"../option\";\nimport type { Result } from \"../result\";\n\nexport function match<T, E extends Error, R>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => R;\n Err: (error: E) => R;\n },\n): R;\nexport function match<T, R>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => R;\n None: () => R;\n },\n): R;\nexport function match<T, E extends Error, R>(\n matcher: Result<T, E> | Option<T>,\n cases: {\n Ok?: (value: T) => R;\n Err?: (error: E) => R;\n Some?: (value: T) => R;\n None?: () => R;\n },\n): R {\n if (\"isOk\" in matcher && matcher.isOk()) {\n if (!cases.Ok) throw new Error(\"Missing case for Ok\");\n return cases.Ok(matcher.unwrap());\n }\n\n if (\"isErr\" in matcher && matcher.isErr()) {\n if (!cases.Err) throw new Error(\"Missing case for Err\");\n return cases.Err(matcher.unwrapErr() as E);\n }\n\n if (\"isSome\" in matcher && matcher.isSome()) {\n if (!cases.Some) throw new Error(\"Missing case for Some\");\n return cases.Some(matcher.unwrap());\n }\n\n if (\"isNone\" in matcher && matcher.isNone()) {\n if (!cases.None) throw new Error(\"Missing case for None\");\n return cases.None();\n }\n\n throw new Error(\"Invalid matcher or missing case\");\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).match = match;\n","import type { None } from \"./none\";\nimport type { Option } from \"./option\";\n\n/**\n * Represents a `Some` option, which holds a value.\n * @template T The type of the value held by this `Some` instance.\n */\nexport class Some<T> {\n /**\n * Creates a new `Some` option with the given value.\n * @param value The value to be wrapped in the `Some` option.\n */\n constructor(private value: T) { }\n\n /**\n * Checks if this option is a `Some`.\n * @returns `true` if this option is a `Some`, otherwise `false`.\n */\n isSome(): this is Some<T> {\n return true;\n }\n\n /**\n * Checks if this option is a `None`.\n * @returns `false` because this is a `Some`.\n */\n isNone(): this is None {\n return false;\n }\n\n /**\n * Unwraps the value held by this `Some` option.\n * @returns The value held by this `Some` option.\n */\n unwrap(): T {\n return this.value;\n }\n\n /**\n * Applies a transformation function to the value held by this `Some` option and returns a new `Option` 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 `Some` option containing the transformed value.\n */\n map<U>(fn: (value: T) => U): Option<U> {\n return new Some(fn(this.value));\n }\n\n /**\n * Applies a transformation function that returns an `Option` to the value held by this `Some` option.\n * @template U The type of the value in the resulting `Option`.\n * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U>(fn: (value: T) => Option<U>): Option<U> {\n return fn(this.value);\n }\n\n /**\n * Returns the value held by this `Some` option, ignoring the default value provided.\n * @param _ A default value (ignored in this implementation).\n * @returns The value held by this `Some` option.\n */\n unwrapOr(_: T): T {\n return this.value;\n }\n}\n","import type { Some } from \"./some\";\nimport type { Option } from \"./option\";\n\n/**\n * Represents a `None` option, which holds no value.\n */\nexport class None {\n /**\n * Checks if this option is a `Some`.\n * @returns `false` because this is a `None`.\n */\n isSome(): this is Some<never> {\n return false;\n }\n\n /**\n * Checks if this option is a `None`.\n * @returns `true` because this is a `None`.\n */\n isNone(): this is None {\n return true;\n }\n\n /**\n * Attempts to unwrap the value from this `None` option.\n * @throws An error because `None` has no value.\n */\n unwrap(): never {\n throw new Error(\"Called unwrap on a None value\");\n }\n\n /**\n * Applies a transformation function to the value (which does not exist) of this `None` option.\n * @template U The type of the value that would have been returned.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns A new `None` option.\n */\n map<U>(_fn: (value: never) => U): Option<U> {\n return new None();\n }\n\n /**\n * Applies a transformation function that returns an `Option` to the value (which does not exist) of this `None` option.\n * @template U The type of the value in the resulting `Option`.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns A new `None` option.\n */\n flatMap<U>(_fn: (value: never) => Option<U>): Option<U> {\n return new None();\n }\n\n /**\n * Returns the default value provided, since `None` has no value.\n * @template T The type of the default value.\n * @param defaultValue The value to return.\n * @returns The default value provided.\n */\n unwrapOr<T>(defaultValue: T): T {\n return defaultValue;\n }\n}\n","import {\n\tSome as SomeType,\n\tNone as NoneType,\n\ttype Option,\n} from \"./__internal__/return-types\";\n\n/**\n * Creates a new `Some` instance, representing an `Option` with a value.\n * @template T The type of the value contained in the `Some`.\n * @param value The value to wrap in the `Some` instance.\n * @returns A `Some` instance containing the given value.\n * @example\n * const option = Some(42);\n * console.log(option.isSome()); // true\n * console.log(option.unwrap()); // 42\n */\nfunction Some<T>(value: T): SomeType<T> {\n\treturn new SomeType(value);\n}\n\n/**\n * Creates a new `None` instance, representing an `Option` with no value.\n * @returns A `None` instance.\n * @example\n * const option = None();\n * console.log(option.isNone()); // true\n * console.log(option.unwrap()); // throws Error: \"Called unwrap on a None value\"\n */\nfunction None(): NoneType {\n\treturn new NoneType();\n}\n\n(global as any).Some = Some;\n(global as any).None = None;\n\nexport { None, Some, Option };\n","import type { ResultDefinition } from \"./result\";\nimport type { Ok } from \"./ok\";\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`.\n */\nexport class Err<E extends Error>\n extends Error\n 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.\n */\n constructor(error: E | string) {\n super(typeof error === \"string\" ? error : error.message);\n this.error =\n typeof error === \"string\" ? (new Error(error) as E) : (error as E);\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 extends Error ? E : Error> {\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.\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 extends Error>(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 * @param _fn The transformation function (ignored in this implementation).\n * @returns The original `Err` instance.\n */\n flatMap<U>(\n _fn: (value: never) => ResultDefinition<U, never>,\n ): ResultDefinition<never, E> {\n return this as unknown as ResultDefinition<never, 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","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 * 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 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 * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U>(\n fn: (value: T) => ResultDefinition<U, never>,\n ): ResultDefinition<U, never> {\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`).\n * @param _fn The mapping function for errors (not used).\n * @returns The original `Ok` instance.\n */\n // mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {\n // \treturn this;\n mapErr<U extends Error>(_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","import {\n\tErr as ErrType,\n\tOk as OkType,\n\ttype 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\treturn new OkType(value);\n}\n\n/**\n * Creates a new `Err` instance, representing a failed result.\n * @template E The type of the error contained in the `Err`.\n * @param error The error to wrap in the `Err` instance.\n * @returns An `Err` instance containing the given error.\n * @example\n * const result = Err(\"Something went wrong\");\n * console.log(result.isErr()); // true\n * console.log(result.unwrapErr()); // \"Something went wrong\"\n */\nfunction Err<E extends Error>(error: E | string): ErrType<E> {\n\treturn new ErrType(error);\n}\n\n(global as any).Ok = Ok;\n(global as any).Err = Err;\n\nexport { Err, Ok, Result, ErrType, OkType };\n"]}
1
+ {"version":3,"sources":["../src/helpers/match.ts","../src/option/__internal__/return-types/some.ts","../src/option/__internal__/return-types/none.ts","../src/option/option.ts","../src/result/__internal__/return-types/err.ts","../src/result/__internal__/return-types/ok.ts","../src/result/result.ts"],"names":["match","Some","None","Ok","Err"],"mappings":";;;AAiBO,SAASA,MAAAA,CACd,SACA,KAMG,EAAA;AACH,EAAA,IAAI,MAAU,IAAA,OAAA,IAAW,OAAQ,CAAA,IAAA,EAAQ,EAAA;AACvC,IAAA,IAAI,CAAC,KAAM,CAAA,EAAA,EAAU,MAAA,IAAI,MAAM,qBAAqB,CAAA;AACpD,IAAA,OAAO,KAAM,CAAA,EAAA,CAAG,OAAQ,CAAA,MAAA,EAAQ,CAAA;AAAA;AAGlC,EAAA,IAAI,OAAW,IAAA,OAAA,IAAW,OAAQ,CAAA,KAAA,EAAS,EAAA;AACzC,IAAA,IAAI,CAAC,KAAM,CAAA,GAAA,EAAW,MAAA,IAAI,MAAM,sBAAsB,CAAA;AACtD,IAAA,OAAO,KAAM,CAAA,GAAA,CAAI,OAAQ,CAAA,SAAA,EAAgB,CAA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AA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA,EAMA,KAA4B,GAAA;AAC1B,IAAO,OAAA,KAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA,EAMA,MAAY,GAAA;AACV,IAAA,OAAO,IAAK,CAAA,KAAA;AAAA;AACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAO,EAAiD,EAAA;AACtD,IAAA,OAAO,IAAI,GAAA,CAAG,EAAG,CAAA,IAAA,CAAK,KAAK,CAAC,CAAA;AAAA;AAC9B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,QACE,EAC4B,EAAA;AAC5B,IAAO,OAAA,EAAA,CAAG,KAAK,KAAK,CAAA;AAAA;AACtB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,OAAwB,GAAgD,EAAA;AACtE,IAAO,OAAA,IAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA,EAMA,SAAmB,GAAA;AACjB,IAAM,MAAA,IAAI,MAAM,iCAAiC,CAAA;AAAA;AACnD;AAAA;AAAA;AAAA;AAAA,EAMA,EAAK,GAAA;AACH,IAAA,OAAO,MAAM,IAAM,EAAA;AAAA,MACjB,EAAI,EAAA,CAAC,CAAM,KAAA,IAAA,CAAK,CAAC,CAAA;AAAA,MACjB,GAAA,EAAK,CAAC,CAAA,KAAM,IAAK;AAAA,KAClB,CAAA;AAAA;AAEL,CAAA;;;ACzEA,SAASC,IAAM,KAAqB,EAAA;AAClC,EAAO,OAAA,IAAI,GAAO,KAAK,CAAA;AACzB;AAYA,SAASC,KAAqB,KAA+B,EAAA;AAC3D,EAAO,OAAA,IAAI,IAAQ,KAAK,CAAA;AAC1B;AAGC,MAAA,CAAe,EAAKD,GAAAA,GAAAA;AAEpB,MAAA,CAAe,GAAMC,GAAAA,IAAAA","file":"index.cjs","sourcesContent":["import type { Option } from \"../option\";\nimport type { Result } from \"../result\";\n\nexport function match<T, E extends Error, R>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => R;\n Err: (error: E) => R;\n },\n): R;\nexport function match<T, R>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => R;\n None: () => R;\n },\n): R;\nexport function match<T, E extends Error, R>(\n matcher: Result<T, E> | Option<T>,\n cases: {\n Ok?: (value: T) => R;\n Err?: (error: E) => R;\n Some?: (value: T) => R;\n None?: () => R;\n },\n): R {\n if (\"isOk\" in matcher && matcher.isOk()) {\n if (!cases.Ok) throw new Error(\"Missing case for Ok\");\n return cases.Ok(matcher.unwrap());\n }\n\n if (\"isErr\" in matcher && matcher.isErr()) {\n if (!cases.Err) throw new Error(\"Missing case for Err\");\n return cases.Err(matcher.unwrapErr() as E);\n }\n\n if (\"isSome\" in matcher && matcher.isSome()) {\n if (!cases.Some) throw new Error(\"Missing case for Some\");\n return cases.Some(matcher.unwrap());\n }\n\n if (\"isNone\" in matcher && matcher.isNone()) {\n if (!cases.None) throw new Error(\"Missing case for None\");\n return cases.None();\n }\n\n throw new Error(\"Invalid matcher or missing case\");\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).match = match;\n","import type { None } from \"./none\";\nimport type { Option } from \"./option\";\n\n/**\n * Represents a `Some` option, which holds a value.\n * @template T The type of the value held by this `Some` instance.\n */\nexport class Some<T> {\n /**\n * Creates a new `Some` option with the given value.\n * @param value The value to be wrapped in the `Some` option.\n */\n constructor(private value: T) { }\n\n /**\n * Checks if this option is a `Some`.\n * @returns `true` if this option is a `Some`, otherwise `false`.\n */\n isSome(): this is Some<T> {\n return true;\n }\n\n /**\n * Checks if this option is a `None`.\n * @returns `false` because this is a `Some`.\n */\n isNone(): this is None {\n return false;\n }\n\n /**\n * Unwraps the value held by this `Some` option.\n * @returns The value held by this `Some` option.\n */\n unwrap(): T {\n return this.value;\n }\n\n /**\n * Applies a transformation function to the value held by this `Some` option and returns a new `Option` 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 `Some` option containing the transformed value.\n */\n map<U>(fn: (value: T) => U): Option<U> {\n return new Some(fn(this.value));\n }\n\n /**\n * Applies a transformation function that returns an `Option` to the value held by this `Some` option.\n * @template U The type of the value in the resulting `Option`.\n * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U>(fn: (value: T) => Option<U>): Option<U> {\n return fn(this.value);\n }\n\n /**\n * Returns the value held by this `Some` option, ignoring the default value provided.\n * @param _ A default value (ignored in this implementation).\n * @returns The value held by this `Some` option.\n */\n unwrapOr(_: T): T {\n return this.value;\n }\n}\n","import type { Some } from \"./some\";\nimport type { Option } from \"./option\";\n\n/**\n * Represents a `None` option, which holds no value.\n */\nexport class None {\n /**\n * Checks if this option is a `Some`.\n * @returns `false` because this is a `None`.\n */\n isSome(): this is Some<never> {\n return false;\n }\n\n /**\n * Checks if this option is a `None`.\n * @returns `true` because this is a `None`.\n */\n isNone(): this is None {\n return true;\n }\n\n /**\n * Attempts to unwrap the value from this `None` option.\n * @throws An error because `None` has no value.\n */\n unwrap(): never {\n throw new Error(\"Called unwrap on a None value\");\n }\n\n /**\n * Applies a transformation function to the value (which does not exist) of this `None` option.\n * @template U The type of the value that would have been returned.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns A new `None` option.\n */\n map<U>(_fn: (value: never) => U): Option<U> {\n return new None();\n }\n\n /**\n * Applies a transformation function that returns an `Option` to the value (which does not exist) of this `None` option.\n * @template U The type of the value in the resulting `Option`.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns A new `None` option.\n */\n flatMap<U>(_fn: (value: never) => Option<U>): Option<U> {\n return new None();\n }\n\n /**\n * Returns the default value provided, since `None` has no value.\n * @template T The type of the default value.\n * @param defaultValue The value to return.\n * @returns The default value provided.\n */\n unwrapOr<T>(defaultValue: T): T {\n return defaultValue;\n }\n}\n","import {\n\tSome as SomeType,\n\tNone as NoneType,\n\ttype Option,\n} from \"./__internal__/return-types\";\n\n/**\n * Creates a new `Some` instance, representing an `Option` with a value.\n * @template T The type of the value contained in the `Some`.\n * @param value The value to wrap in the `Some` instance.\n * @returns A `Some` instance containing the given value.\n * @example\n * const option = Some(42);\n * console.log(option.isSome()); // true\n * console.log(option.unwrap()); // 42\n */\nfunction Some<T>(value: T): SomeType<T> {\n\treturn new SomeType(value);\n}\n\n/**\n * Creates a new `None` instance, representing an `Option` with no value.\n * @returns A `None` instance.\n * @example\n * const option = None();\n * console.log(option.isNone()); // true\n * console.log(option.unwrap()); // throws Error: \"Called unwrap on a None value\"\n */\nfunction None(): NoneType {\n\treturn new NoneType();\n}\n\n(global as any).Some = Some;\n(global as any).None = None;\n\nexport { None, Some, Option };\n","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`.\n */\nexport class Err<E extends Error>\n extends Error\n implements ResultDefinition<never, E>\n{\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.\n */\n constructor(error: E | string) {\n super(typeof error === \"string\" ? error : error.message);\n this.error =\n typeof error === \"string\" ? (new Error(error) as E) : (error as E);\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 extends Error ? E : Error> {\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.\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 extends Error>(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 * @param _fn The transformation function (ignored in this implementation).\n * @returns The original `Err` instance.\n */\n flatMap<U>(\n _fn: (value: never) => ResultDefinition<U, never>,\n ): ResultDefinition<never, E> {\n return this as unknown as ResultDefinition<never, 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 * 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 * 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 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 * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U>(\n fn: (value: T) => ResultDefinition<U, never>,\n ): ResultDefinition<U, never> {\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`).\n * @param _fn The mapping function for errors (not used).\n * @returns The original `Ok` instance.\n */\n // mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {\n // \treturn this;\n mapErr<U extends Error>(_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 * 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 match(this, {\n Ok: (v) => Some(v),\n Err: (_) => None(),\n });\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 a new `Err` instance, representing a failed result.\n * @template E The type of the error contained in the `Err`.\n * @param error The error to wrap in the `Err` instance.\n * @returns An `Err` instance containing the given error.\n * @example\n * const result = Err(\"Something went wrong\");\n * console.log(result.isErr()); // true\n * console.log(result.unwrapErr()); // \"Something went wrong\"\n */\nfunction Err<E extends Error>(error: E | string): ErrType<E> {\n return new ErrType(error);\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).Ok = Ok;\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).Err = Err;\n\nexport type { Err, Ok, Result, ErrType, OkType };\n"]}
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
+ export { R as Result } from './types/globals.cjs';
1
2
  export { match } from './helpers/match.cjs';
2
3
  export { None, Some } from './option/option.cjs';
3
4
  export { Err, Ok } from './result/result.cjs';
4
5
  export { O as Option } from './option-DpT8KCGE.cjs';
5
- export { R as Result } from './err-B7LUEZ0f.cjs';
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
+ export { R as Result } from './types/globals.js';
1
2
  export { match } from './helpers/match.js';
2
3
  export { None, Some } from './option/option.js';
3
4
  export { Err, Ok } from './result/result.js';
4
5
  export { O as Option } from './option-DpT8KCGE.js';
5
- export { R as Result } from './err-B7LUEZ0f.js';
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/helpers/match.ts
2
- function match(matcher, cases) {
2
+ function match2(matcher, cases) {
3
3
  if ("isOk" in matcher && matcher.isOk()) {
4
4
  if (!cases.Ok) throw new Error("Missing case for Ok");
5
5
  return cases.Ok(matcher.unwrap());
@@ -18,10 +18,10 @@ function match(matcher, cases) {
18
18
  }
19
19
  throw new Error("Invalid matcher or missing case");
20
20
  }
21
- global.match = match;
21
+ global.match = match2;
22
22
 
23
23
  // src/option/__internal__/return-types/some.ts
24
- var Some = class _Some {
24
+ var Some2 = class _Some {
25
25
  /**
26
26
  * Creates a new `Some` option with the given value.
27
27
  * @param value The value to be wrapped in the `Some` option.
@@ -79,7 +79,7 @@ var Some = class _Some {
79
79
  };
80
80
 
81
81
  // src/option/__internal__/return-types/none.ts
82
- var None = class _None {
82
+ var None2 = class _None {
83
83
  /**
84
84
  * Checks if this option is a `Some`.
85
85
  * @returns `false` because this is a `None`.
@@ -131,14 +131,14 @@ var None = class _None {
131
131
  };
132
132
 
133
133
  // src/option/option.ts
134
- function Some2(value) {
135
- return new Some(value);
134
+ function Some3(value) {
135
+ return new Some2(value);
136
136
  }
137
- function None2() {
138
- return new None();
137
+ function None3() {
138
+ return new None2();
139
139
  }
140
- global.Some = Some2;
141
- global.None = None2;
140
+ global.Some = Some3;
141
+ global.None = None3;
142
142
 
143
143
  // src/result/__internal__/return-types/err.ts
144
144
  var Err = class _Err extends Error {
@@ -210,6 +210,13 @@ var Err = class _Err extends Error {
210
210
  unwrapErr() {
211
211
  return this.error;
212
212
  }
213
+ /**
214
+ * Converts `Result` type to `Option` type.
215
+ * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
216
+ */
217
+ ok() {
218
+ return None();
219
+ }
213
220
  };
214
221
 
215
222
  // src/result/__internal__/return-types/ok.ts
@@ -278,6 +285,16 @@ var Ok = class _Ok {
278
285
  unwrapErr() {
279
286
  throw new Error("Called unwrapErr on an Ok value");
280
287
  }
288
+ /**
289
+ * Converts `Result` type to `Option` type.
290
+ * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
291
+ */
292
+ ok() {
293
+ return match(this, {
294
+ Ok: (v) => Some(v),
295
+ Err: (_) => None()
296
+ });
297
+ }
281
298
  };
282
299
 
283
300
  // src/result/result.ts
@@ -290,6 +307,6 @@ function Err2(error) {
290
307
  global.Ok = Ok2;
291
308
  global.Err = Err2;
292
309
 
293
- export { Err2 as Err, None2 as None, Ok2 as Ok, Some2 as Some, match };
310
+ export { None3 as None, Some3 as Some, match2 as match };
294
311
  //# sourceMappingURL=index.js.map
295
312
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/helpers/match.ts","../src/option/__internal__/return-types/some.ts","../src/option/__internal__/return-types/none.ts","../src/option/option.ts","../src/result/__internal__/return-types/err.ts","../src/result/__internal__/return-types/ok.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KAA+B,EAAA;AAC5D,EAAO,OAAA,IAAI,IAAQ,KAAK,CAAA;AACzB;AAEC,MAAA,CAAe,EAAKD,GAAAA,GAAAA;AACpB,MAAA,CAAe,GAAMC,GAAAA,IAAAA","file":"index.js","sourcesContent":["import type { Option } from \"../option\";\nimport type { Result } from \"../result\";\n\nexport function match<T, E extends Error, R>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => R;\n Err: (error: E) => R;\n },\n): R;\nexport function match<T, R>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => R;\n None: () => R;\n },\n): R;\nexport function match<T, E extends Error, R>(\n matcher: Result<T, E> | Option<T>,\n cases: {\n Ok?: (value: T) => R;\n Err?: (error: E) => R;\n Some?: (value: T) => R;\n None?: () => R;\n },\n): R {\n if (\"isOk\" in matcher && matcher.isOk()) {\n if (!cases.Ok) throw new Error(\"Missing case for Ok\");\n return cases.Ok(matcher.unwrap());\n }\n\n if (\"isErr\" in matcher && matcher.isErr()) {\n if (!cases.Err) throw new Error(\"Missing case for Err\");\n return cases.Err(matcher.unwrapErr() as E);\n }\n\n if (\"isSome\" in matcher && matcher.isSome()) {\n if (!cases.Some) throw new Error(\"Missing case for Some\");\n return cases.Some(matcher.unwrap());\n }\n\n if (\"isNone\" in matcher && matcher.isNone()) {\n if (!cases.None) throw new Error(\"Missing case for None\");\n return cases.None();\n }\n\n throw new Error(\"Invalid matcher or missing case\");\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).match = match;\n","import type { None } from \"./none\";\nimport type { Option } from \"./option\";\n\n/**\n * Represents a `Some` option, which holds a value.\n * @template T The type of the value held by this `Some` instance.\n */\nexport class Some<T> {\n /**\n * Creates a new `Some` option with the given value.\n * @param value The value to be wrapped in the `Some` option.\n */\n constructor(private value: T) { }\n\n /**\n * Checks if this option is a `Some`.\n * @returns `true` if this option is a `Some`, otherwise `false`.\n */\n isSome(): this is Some<T> {\n return true;\n }\n\n /**\n * Checks if this option is a `None`.\n * @returns `false` because this is a `Some`.\n */\n isNone(): this is None {\n return false;\n }\n\n /**\n * Unwraps the value held by this `Some` option.\n * @returns The value held by this `Some` option.\n */\n unwrap(): T {\n return this.value;\n }\n\n /**\n * Applies a transformation function to the value held by this `Some` option and returns a new `Option` 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 `Some` option containing the transformed value.\n */\n map<U>(fn: (value: T) => U): Option<U> {\n return new Some(fn(this.value));\n }\n\n /**\n * Applies a transformation function that returns an `Option` to the value held by this `Some` option.\n * @template U The type of the value in the resulting `Option`.\n * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U>(fn: (value: T) => Option<U>): Option<U> {\n return fn(this.value);\n }\n\n /**\n * Returns the value held by this `Some` option, ignoring the default value provided.\n * @param _ A default value (ignored in this implementation).\n * @returns The value held by this `Some` option.\n */\n unwrapOr(_: T): T {\n return this.value;\n }\n}\n","import type { Some } from \"./some\";\nimport type { Option } from \"./option\";\n\n/**\n * Represents a `None` option, which holds no value.\n */\nexport class None {\n /**\n * Checks if this option is a `Some`.\n * @returns `false` because this is a `None`.\n */\n isSome(): this is Some<never> {\n return false;\n }\n\n /**\n * Checks if this option is a `None`.\n * @returns `true` because this is a `None`.\n */\n isNone(): this is None {\n return true;\n }\n\n /**\n * Attempts to unwrap the value from this `None` option.\n * @throws An error because `None` has no value.\n */\n unwrap(): never {\n throw new Error(\"Called unwrap on a None value\");\n }\n\n /**\n * Applies a transformation function to the value (which does not exist) of this `None` option.\n * @template U The type of the value that would have been returned.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns A new `None` option.\n */\n map<U>(_fn: (value: never) => U): Option<U> {\n return new None();\n }\n\n /**\n * Applies a transformation function that returns an `Option` to the value (which does not exist) of this `None` option.\n * @template U The type of the value in the resulting `Option`.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns A new `None` option.\n */\n flatMap<U>(_fn: (value: never) => Option<U>): Option<U> {\n return new None();\n }\n\n /**\n * Returns the default value provided, since `None` has no value.\n * @template T The type of the default value.\n * @param defaultValue The value to return.\n * @returns The default value provided.\n */\n unwrapOr<T>(defaultValue: T): T {\n return defaultValue;\n }\n}\n","import {\n\tSome as SomeType,\n\tNone as NoneType,\n\ttype Option,\n} from \"./__internal__/return-types\";\n\n/**\n * Creates a new `Some` instance, representing an `Option` with a value.\n * @template T The type of the value contained in the `Some`.\n * @param value The value to wrap in the `Some` instance.\n * @returns A `Some` instance containing the given value.\n * @example\n * const option = Some(42);\n * console.log(option.isSome()); // true\n * console.log(option.unwrap()); // 42\n */\nfunction Some<T>(value: T): SomeType<T> {\n\treturn new SomeType(value);\n}\n\n/**\n * Creates a new `None` instance, representing an `Option` with no value.\n * @returns A `None` instance.\n * @example\n * const option = None();\n * console.log(option.isNone()); // true\n * console.log(option.unwrap()); // throws Error: \"Called unwrap on a None value\"\n */\nfunction None(): NoneType {\n\treturn new NoneType();\n}\n\n(global as any).Some = Some;\n(global as any).None = None;\n\nexport { None, Some, Option };\n","import type { ResultDefinition } from \"./result\";\nimport type { Ok } from \"./ok\";\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`.\n */\nexport class Err<E extends Error>\n extends Error\n 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.\n */\n constructor(error: E | string) {\n super(typeof error === \"string\" ? error : error.message);\n this.error =\n typeof error === \"string\" ? (new Error(error) as E) : (error as E);\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 extends Error ? E : Error> {\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.\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 extends Error>(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 * @param _fn The transformation function (ignored in this implementation).\n * @returns The original `Err` instance.\n */\n flatMap<U>(\n _fn: (value: never) => ResultDefinition<U, never>,\n ): ResultDefinition<never, E> {\n return this as unknown as ResultDefinition<never, 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","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 * 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 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 * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U>(\n fn: (value: T) => ResultDefinition<U, never>,\n ): ResultDefinition<U, never> {\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`).\n * @param _fn The mapping function for errors (not used).\n * @returns The original `Ok` instance.\n */\n // mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {\n // \treturn this;\n mapErr<U extends Error>(_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","import {\n\tErr as ErrType,\n\tOk as OkType,\n\ttype 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\treturn new OkType(value);\n}\n\n/**\n * Creates a new `Err` instance, representing a failed result.\n * @template E The type of the error contained in the `Err`.\n * @param error The error to wrap in the `Err` instance.\n * @returns An `Err` instance containing the given error.\n * @example\n * const result = Err(\"Something went wrong\");\n * console.log(result.isErr()); // true\n * console.log(result.unwrapErr()); // \"Something went wrong\"\n */\nfunction Err<E extends Error>(error: E | string): ErrType<E> {\n\treturn new ErrType(error);\n}\n\n(global as any).Ok = Ok;\n(global as any).Err = Err;\n\nexport { Err, Ok, Result, ErrType, OkType };\n"]}
1
+ {"version":3,"sources":["../src/helpers/match.ts","../src/option/__internal__/return-types/some.ts","../src/option/__internal__/return-types/none.ts","../src/option/option.ts","../src/result/__internal__/return-types/err.ts","../src/result/__internal__/return-types/ok.ts","../src/result/result.ts"],"names":["match","Some","None","Ok","Err"],"mappings":";AAiBO,SAASA,MAAAA,CACd,SACA,KAMG,EAAA;AACH,EAAA,IAAI,MAAU,IAAA,OAAA,IAAW,OAAQ,CAAA,IAAA,EAAQ,EAAA;AACvC,IAAA,IAAI,CAAC,KAAM,CAAA,EAAA,EAAU,MAAA,IAAI,MAAM,qBAAqB,CAAA;AACpD,IAAA,OAAO,KAAM,CAAA,EAAA,CAAG,OAAQ,CAAA,MAAA,EAAQ,CAAA;AAAA;AAGlC,EAAA,IAAI,OAAW,IAAA,OAAA,IAAW,OAAQ,CAAA,KAAA,EAAS,EAAA;AACzC,IAAA,IAAI,CAAC,KAAM,CAAA,GAAA,EAAW,MAAA,IAAI,MAAM,sBAAsB,CAAA;AACtD,IAAA,OAAO,KAAM,CAAA,GAAA,CAAI,OAAQ,CAAA,SAAA,EAAgB,CAAA;AAAA;AAG3C,EAAA,IAAI,QAAY,IAAA,OAAA,IAAW,OAAQ,CAAA,MAAA,EAAU,EAAA;AAC3C,IAAA,IAAI,CAAC,KAAM,CAAA,IAAA,EAAY,MAAA,IAAI,MAAM,uBAAuB,CAAA;AACxD,IAAA,OAAO,KAAM,CAAA,IAAA,CAAK,OAAQ,CAAA,MAAA,EAAQ,CAAA;AAAA;AAGpC,EAAA,IAAI,QAAY,IAAA,OAAA,IAAW,OAAQ,CAAA,MAAA,EAAU,EAAA;AAC3C,IAAA,IAAI,CAAC,KAAM,CAA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AA;AAAA;AAAA,EAQA,QACE,EAC4B,EAAA;AAC5B,IAAO,OAAA,EAAA,CAAG,KAAK,KAAK,CAAA;AAAA;AACtB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,OAAwB,GAAgD,EAAA;AACtE,IAAO,OAAA,IAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA,EAMA,SAAmB,GAAA;AACjB,IAAM,MAAA,IAAI,MAAM,iCAAiC,CAAA;AAAA;AACnD;AAAA;AAAA;AAAA;AAAA,EAMA,EAAK,GAAA;AACH,IAAA,OAAO,MAAM,IAAM,EAAA;AAAA,MACjB,EAAI,EAAA,CAAC,CAAM,KAAA,IAAA,CAAK,CAAC,CAAA;AAAA,MACjB,GAAA,EAAK,CAAC,CAAA,KAAM,IAAK;AAAA,KAClB,CAAA;AAAA;AAEL,CAAA;;;ACzEA,SAASC,IAAM,KAAqB,EAAA;AAClC,EAAO,OAAA,IAAI,GAAO,KAAK,CAAA;AACzB;AAYA,SAASC,KAAqB,KAA+B,EAAA;AAC3D,EAAO,OAAA,IAAI,IAAQ,KAAK,CAAA;AAC1B;AAGC,MAAA,CAAe,EAAKD,GAAAA,GAAAA;AAEpB,MAAA,CAAe,GAAMC,GAAAA,IAAAA","file":"index.js","sourcesContent":["import type { Option } from \"../option\";\nimport type { Result } from \"../result\";\n\nexport function match<T, E extends Error, R>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => R;\n Err: (error: E) => R;\n },\n): R;\nexport function match<T, R>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => R;\n None: () => R;\n },\n): R;\nexport function match<T, E extends Error, R>(\n matcher: Result<T, E> | Option<T>,\n cases: {\n Ok?: (value: T) => R;\n Err?: (error: E) => R;\n Some?: (value: T) => R;\n None?: () => R;\n },\n): R {\n if (\"isOk\" in matcher && matcher.isOk()) {\n if (!cases.Ok) throw new Error(\"Missing case for Ok\");\n return cases.Ok(matcher.unwrap());\n }\n\n if (\"isErr\" in matcher && matcher.isErr()) {\n if (!cases.Err) throw new Error(\"Missing case for Err\");\n return cases.Err(matcher.unwrapErr() as E);\n }\n\n if (\"isSome\" in matcher && matcher.isSome()) {\n if (!cases.Some) throw new Error(\"Missing case for Some\");\n return cases.Some(matcher.unwrap());\n }\n\n if (\"isNone\" in matcher && matcher.isNone()) {\n if (!cases.None) throw new Error(\"Missing case for None\");\n return cases.None();\n }\n\n throw new Error(\"Invalid matcher or missing case\");\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).match = match;\n","import type { None } from \"./none\";\nimport type { Option } from \"./option\";\n\n/**\n * Represents a `Some` option, which holds a value.\n * @template T The type of the value held by this `Some` instance.\n */\nexport class Some<T> {\n /**\n * Creates a new `Some` option with the given value.\n * @param value The value to be wrapped in the `Some` option.\n */\n constructor(private value: T) { }\n\n /**\n * Checks if this option is a `Some`.\n * @returns `true` if this option is a `Some`, otherwise `false`.\n */\n isSome(): this is Some<T> {\n return true;\n }\n\n /**\n * Checks if this option is a `None`.\n * @returns `false` because this is a `Some`.\n */\n isNone(): this is None {\n return false;\n }\n\n /**\n * Unwraps the value held by this `Some` option.\n * @returns The value held by this `Some` option.\n */\n unwrap(): T {\n return this.value;\n }\n\n /**\n * Applies a transformation function to the value held by this `Some` option and returns a new `Option` 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 `Some` option containing the transformed value.\n */\n map<U>(fn: (value: T) => U): Option<U> {\n return new Some(fn(this.value));\n }\n\n /**\n * Applies a transformation function that returns an `Option` to the value held by this `Some` option.\n * @template U The type of the value in the resulting `Option`.\n * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U>(fn: (value: T) => Option<U>): Option<U> {\n return fn(this.value);\n }\n\n /**\n * Returns the value held by this `Some` option, ignoring the default value provided.\n * @param _ A default value (ignored in this implementation).\n * @returns The value held by this `Some` option.\n */\n unwrapOr(_: T): T {\n return this.value;\n }\n}\n","import type { Some } from \"./some\";\nimport type { Option } from \"./option\";\n\n/**\n * Represents a `None` option, which holds no value.\n */\nexport class None {\n /**\n * Checks if this option is a `Some`.\n * @returns `false` because this is a `None`.\n */\n isSome(): this is Some<never> {\n return false;\n }\n\n /**\n * Checks if this option is a `None`.\n * @returns `true` because this is a `None`.\n */\n isNone(): this is None {\n return true;\n }\n\n /**\n * Attempts to unwrap the value from this `None` option.\n * @throws An error because `None` has no value.\n */\n unwrap(): never {\n throw new Error(\"Called unwrap on a None value\");\n }\n\n /**\n * Applies a transformation function to the value (which does not exist) of this `None` option.\n * @template U The type of the value that would have been returned.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns A new `None` option.\n */\n map<U>(_fn: (value: never) => U): Option<U> {\n return new None();\n }\n\n /**\n * Applies a transformation function that returns an `Option` to the value (which does not exist) of this `None` option.\n * @template U The type of the value in the resulting `Option`.\n * @param _fn The transformation function (ignored in this implementation).\n * @returns A new `None` option.\n */\n flatMap<U>(_fn: (value: never) => Option<U>): Option<U> {\n return new None();\n }\n\n /**\n * Returns the default value provided, since `None` has no value.\n * @template T The type of the default value.\n * @param defaultValue The value to return.\n * @returns The default value provided.\n */\n unwrapOr<T>(defaultValue: T): T {\n return defaultValue;\n }\n}\n","import {\n\tSome as SomeType,\n\tNone as NoneType,\n\ttype Option,\n} from \"./__internal__/return-types\";\n\n/**\n * Creates a new `Some` instance, representing an `Option` with a value.\n * @template T The type of the value contained in the `Some`.\n * @param value The value to wrap in the `Some` instance.\n * @returns A `Some` instance containing the given value.\n * @example\n * const option = Some(42);\n * console.log(option.isSome()); // true\n * console.log(option.unwrap()); // 42\n */\nfunction Some<T>(value: T): SomeType<T> {\n\treturn new SomeType(value);\n}\n\n/**\n * Creates a new `None` instance, representing an `Option` with no value.\n * @returns A `None` instance.\n * @example\n * const option = None();\n * console.log(option.isNone()); // true\n * console.log(option.unwrap()); // throws Error: \"Called unwrap on a None value\"\n */\nfunction None(): NoneType {\n\treturn new NoneType();\n}\n\n(global as any).Some = Some;\n(global as any).None = None;\n\nexport { None, Some, Option };\n","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`.\n */\nexport class Err<E extends Error>\n extends Error\n implements ResultDefinition<never, E>\n{\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.\n */\n constructor(error: E | string) {\n super(typeof error === \"string\" ? error : error.message);\n this.error =\n typeof error === \"string\" ? (new Error(error) as E) : (error as E);\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 extends Error ? E : Error> {\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.\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 extends Error>(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 * @param _fn The transformation function (ignored in this implementation).\n * @returns The original `Err` instance.\n */\n flatMap<U>(\n _fn: (value: never) => ResultDefinition<U, never>,\n ): ResultDefinition<never, E> {\n return this as unknown as ResultDefinition<never, 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 * 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 * 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 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 * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U>(\n fn: (value: T) => ResultDefinition<U, never>,\n ): ResultDefinition<U, never> {\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`).\n * @param _fn The mapping function for errors (not used).\n * @returns The original `Ok` instance.\n */\n // mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {\n // \treturn this;\n mapErr<U extends Error>(_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 * 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 match(this, {\n Ok: (v) => Some(v),\n Err: (_) => None(),\n });\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 a new `Err` instance, representing a failed result.\n * @template E The type of the error contained in the `Err`.\n * @param error The error to wrap in the `Err` instance.\n * @returns An `Err` instance containing the given error.\n * @example\n * const result = Err(\"Something went wrong\");\n * console.log(result.isErr()); // true\n * console.log(result.unwrapErr()); // \"Something went wrong\"\n */\nfunction Err<E extends Error>(error: E | string): ErrType<E> {\n return new ErrType(error);\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).Ok = Ok;\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).Err = Err;\n\nexport type { Err, Ok, Result, ErrType, OkType };\n"]}
@@ -70,6 +70,13 @@ var Err = class _Err extends Error {
70
70
  unwrapErr() {
71
71
  return this.error;
72
72
  }
73
+ /**
74
+ * Converts `Result` type to `Option` type.
75
+ * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
76
+ */
77
+ ok() {
78
+ return None();
79
+ }
73
80
  };
74
81
 
75
82
  // src/result/__internal__/return-types/ok.ts
@@ -138,6 +145,16 @@ var Ok = class _Ok {
138
145
  unwrapErr() {
139
146
  throw new Error("Called unwrapErr on an Ok value");
140
147
  }
148
+ /**
149
+ * Converts `Result` type to `Option` type.
150
+ * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
151
+ */
152
+ ok() {
153
+ return match(this, {
154
+ Ok: (v) => Some(v),
155
+ Err: (_) => None()
156
+ });
157
+ }
141
158
  };
142
159
 
143
160
  // src/result/result.ts
@@ -149,8 +166,5 @@ function Err2(error) {
149
166
  }
150
167
  global.Ok = Ok2;
151
168
  global.Err = Err2;
152
-
153
- exports.Err = Err2;
154
- exports.Ok = Ok2;
155
169
  //# sourceMappingURL=index.cjs.map
156
170
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"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,IAAA,SACH,KAC8B,CAAA;AAAA,EAC9B,KAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAKR,YAAY,KAAmB,EAAA;AAC7B,IAAA,KAAA,CAAM,OAAO,KAAA,KAAU,QAAW,GAAA,KAAA,GAAQ,MAAM,OAAO,CAAA;AACvD,IAAA,IAAA,CAAK,QACH,OAAO,KAAA,KAAU,WAAY,IAAI,KAAA,CAAM,KAAK,CAAW,GAAA,KAAA;AACzD,IAAO,MAAA,CAAA,cAAA,CAAe,IAAM,EAAA,IAAA,CAAI,SAAS,CAAA;AAEzC,IAAA,IAAI,MAAM,iBAAmB,EAAA;AAC3B,MAAM,KAAA,CAAA,iBAAA,CAAkB,MAAM,IAAG,CAAA;AAAA;AACnC;AACF;AAAA;AAAA;AAAA;AAAA,EAMA,IAA0B,GAAA;A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;AAAA;AAAA,EAQA,QACE,EAC4B,EAAA;AAC5B,IAAO,OAAA,EAAA,CAAG,KAAK,KAAK,CAAA;AAAA;AACtB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,OAAwB,GAAgD,EAAA;AACtE,IAAO,OAAA,IAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA,EAMA,SAAmB,GAAA;AACjB,IAAM,MAAA,IAAI,MAAM,iCAAiC,CAAA;AAAA;AAErD,CAAA;;;AC9DA,SAASA,IAAM,KAAqB,EAAA;AACnC,EAAO,OAAA,IAAI,GAAO,KAAK,CAAA;AACxB;AAYA,SAASC,KAAqB,KAA+B,EAAA;AAC5D,EAAO,OAAA,IAAI,IAAQ,KAAK,CAAA;AACzB;AAEC,MAAA,CAAe,EAAKD,GAAAA,GAAAA;AACpB,MAAA,CAAe,GAAMC,GAAAA,IAAAA","file":"index.cjs","sourcesContent":["import type { ResultDefinition } from \"./result\";\nimport type { Ok } from \"./ok\";\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`.\n */\nexport class Err<E extends Error>\n extends Error\n 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.\n */\n constructor(error: E | string) {\n super(typeof error === \"string\" ? error : error.message);\n this.error =\n typeof error === \"string\" ? (new Error(error) as E) : (error as E);\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 extends Error ? E : Error> {\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.\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 extends Error>(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 * @param _fn The transformation function (ignored in this implementation).\n * @returns The original `Err` instance.\n */\n flatMap<U>(\n _fn: (value: never) => ResultDefinition<U, never>,\n ): ResultDefinition<never, E> {\n return this as unknown as ResultDefinition<never, 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","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 * 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 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 * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U>(\n fn: (value: T) => ResultDefinition<U, never>,\n ): ResultDefinition<U, never> {\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`).\n * @param _fn The mapping function for errors (not used).\n * @returns The original `Ok` instance.\n */\n // mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {\n // \treturn this;\n mapErr<U extends Error>(_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","import {\n\tErr as ErrType,\n\tOk as OkType,\n\ttype 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\treturn new OkType(value);\n}\n\n/**\n * Creates a new `Err` instance, representing a failed result.\n * @template E The type of the error contained in the `Err`.\n * @param error The error to wrap in the `Err` instance.\n * @returns An `Err` instance containing the given error.\n * @example\n * const result = Err(\"Something went wrong\");\n * console.log(result.isErr()); // true\n * console.log(result.unwrapErr()); // \"Something went wrong\"\n */\nfunction Err<E extends Error>(error: E | string): ErrType<E> {\n\treturn new ErrType(error);\n}\n\n(global as any).Ok = Ok;\n(global as any).Err = Err;\n\nexport { Err, Ok, Result, ErrType, OkType };\n"]}
1
+ {"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,IAAA,SACH,KAEV,CAAA;AAAA,EACU,KAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAKR,YAAY,KAAmB,EAAA;AAC7B,IAAA,KAAA,CAAM,OAAO,KAAA,KAAU,QAAW,GAAA,KAAA,GAAQ,MAAM,OAAO,CAAA;AACvD,IAAA,IAAA,CAAK,QACH,OAAO,KAAA,KAAU,WAAY,IAAI,KAAA,CAAM,KAAK,CAAW,GAAA,KAAA;AACzD,IAAO,MAAA,CAAA,cAAA,CAAe,IAAM,EAAA,IAAA,CAAI,SAAS,CAAA;AAEzC,IAAA,IAAI,MAAM,iBAAmB,EAAA;AAC3B,MAAM,KAAA,CAAA,iBAAA,CAAkB,MAAM,IAAG,CAAA;AAAA;AACnC;AACF;AAAA;AAAA;AAAA;AAAA,EAMA,IAA0B,GAAA;AACxB,IAAO,OAAA,KAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA,EAMA,KAAkD,GAAA;AAChD,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,OAAwB,EAA+C,EAAA;AACrE,IAAA,OAAO,IAAI,IAAA,CAAO,EAAG,CAAA,IAAA,CAAK,KAAK,CAAC,CAAA;AAAA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M,IAAM,EAAA;AAAA,MACjB,EAAI,EAAA,CAAC,CAAM,KAAA,IAAA,CAAK,CAAC,CAAA;AAAA,MACjB,GAAA,EAAK,CAAC,CAAA,KAAM,IAAK;AAAA,KAClB,CAAA;AAAA;AAEL,CAAA;;;ACzEA,SAASA,IAAM,KAAqB,EAAA;AAClC,EAAO,OAAA,IAAI,GAAO,KAAK,CAAA;AACzB;AAYA,SAASC,KAAqB,KAA+B,EAAA;AAC3D,EAAO,OAAA,IAAI,IAAQ,KAAK,CAAA;AAC1B;AAGC,MAAA,CAAe,EAAKD,GAAAA,GAAAA;AAEpB,MAAA,CAAe,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`.\n */\nexport class Err<E extends Error>\n extends Error\n implements ResultDefinition<never, E>\n{\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.\n */\n constructor(error: E | string) {\n super(typeof error === \"string\" ? error : error.message);\n this.error =\n typeof error === \"string\" ? (new Error(error) as E) : (error as E);\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 extends Error ? E : Error> {\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.\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 extends Error>(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 * @param _fn The transformation function (ignored in this implementation).\n * @returns The original `Err` instance.\n */\n flatMap<U>(\n _fn: (value: never) => ResultDefinition<U, never>,\n ): ResultDefinition<never, E> {\n return this as unknown as ResultDefinition<never, 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 * 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 * 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 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 * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U>(\n fn: (value: T) => ResultDefinition<U, never>,\n ): ResultDefinition<U, never> {\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`).\n * @param _fn The mapping function for errors (not used).\n * @returns The original `Ok` instance.\n */\n // mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {\n // \treturn this;\n mapErr<U extends Error>(_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 * 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 match(this, {\n Ok: (v) => Some(v),\n Err: (_) => None(),\n });\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 a new `Err` instance, representing a failed result.\n * @template E The type of the error contained in the `Err`.\n * @param error The error to wrap in the `Err` instance.\n * @returns An `Err` instance containing the given error.\n * @example\n * const result = Err(\"Something went wrong\");\n * console.log(result.isErr()); // true\n * console.log(result.unwrapErr()); // \"Something went wrong\"\n */\nfunction Err<E extends Error>(error: E | string): ErrType<E> {\n return new ErrType(error);\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).Ok = Ok;\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).Err = Err;\n\nexport type { Err, Ok, Result, ErrType, OkType };\n"]}
@@ -1,2 +1,3 @@
1
1
  export { Err, Ok } from './result.cjs';
2
- export { R as Result } from '../err-B7LUEZ0f.cjs';
2
+ export { R as Result } from '../types/globals.cjs';
3
+ import '../option-DpT8KCGE.cjs';
@@ -1,2 +1,3 @@
1
1
  export { Err, Ok } from './result.js';
2
- export { R as Result } from '../err-B7LUEZ0f.js';
2
+ export { R as Result } from '../types/globals.js';
3
+ import '../option-DpT8KCGE.js';
@@ -68,6 +68,13 @@ var Err = class _Err extends Error {
68
68
  unwrapErr() {
69
69
  return this.error;
70
70
  }
71
+ /**
72
+ * Converts `Result` type to `Option` type.
73
+ * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
74
+ */
75
+ ok() {
76
+ return None();
77
+ }
71
78
  };
72
79
 
73
80
  // src/result/__internal__/return-types/ok.ts
@@ -136,6 +143,16 @@ var Ok = class _Ok {
136
143
  unwrapErr() {
137
144
  throw new Error("Called unwrapErr on an Ok value");
138
145
  }
146
+ /**
147
+ * Converts `Result` type to `Option` type.
148
+ * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
149
+ */
150
+ ok() {
151
+ return match(this, {
152
+ Ok: (v) => Some(v),
153
+ Err: (_) => None()
154
+ });
155
+ }
139
156
  };
140
157
 
141
158
  // src/result/result.ts
@@ -147,7 +164,5 @@ function Err2(error) {
147
164
  }
148
165
  global.Ok = Ok2;
149
166
  global.Err = Err2;
150
-
151
- export { Err2 as Err, Ok2 as Ok };
152
167
  //# sourceMappingURL=index.js.map
153
168
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"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,IAAA,SACH,KAC8B,CAAA;AAAA,EAC9B,KAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAKR,YAAY,KAAmB,EAAA;AAC7B,IAAA,KAAA,CAAM,OAAO,KAAA,KAAU,QAAW,GAAA,KAAA,GAAQ,MAAM,OAAO,CAAA;AACvD,IAAA,IAAA,CAAK,QACH,OAAO,KAAA,KAAU,WAAY,IAAI,KAAA,CAAM,KAAK,CAAW,GAAA,KAAA;AACzD,IAAO,MAAA,CAAA,cAAA,CAAe,IAAM,EAAA,IAAA,CAAI,SAAS,CAAA;AAEzC,IAAA,IAAI,MAAM,iBAAmB,EAAA;AAC3B,MAAM,KAAA,CAAA,iBAAA,CAAkB,MAAM,IAAG,CAAA;AAAA;AACnC;AACF;AAAA;AAAA;AAAA;AAAA,EAMA,IAA0B,GAAA;AACxB,IAAO,OAAA,KAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA,EAMA,KAAkD,GAAA;AAChD,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,OAAwB,EAA+C,EAAA;AACrE,IAAA,OAAO,IAAI,IAAA,CAAO,EAAG,CAAA,IAAA,CAAK,KAAK,CAAC,CAAA;AAAA;AAClC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,QACE,GAC4B,EAAA;AAC5B,IAAO,OAAA,IAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA,EAMA,SAAe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\"./result\";\nimport type { Ok } from \"./ok\";\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`.\n */\nexport class Err<E extends Error>\n extends Error\n 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.\n */\n constructor(error: E | string) {\n super(typeof error === \"string\" ? error : error.message);\n this.error =\n typeof error === \"string\" ? (new Error(error) as E) : (error as E);\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 extends Error ? E : Error> {\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.\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 extends Error>(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 * @param _fn The transformation function (ignored in this implementation).\n * @returns The original `Err` instance.\n */\n flatMap<U>(\n _fn: (value: never) => ResultDefinition<U, never>,\n ): ResultDefinition<never, E> {\n return this as unknown as ResultDefinition<never, 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","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 * 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 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 * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U>(\n fn: (value: T) => ResultDefinition<U, never>,\n ): ResultDefinition<U, never> {\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`).\n * @param _fn The mapping function for errors (not used).\n * @returns The original `Ok` instance.\n */\n // mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {\n // \treturn this;\n mapErr<U extends Error>(_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","import {\n\tErr as ErrType,\n\tOk as OkType,\n\ttype 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\treturn new OkType(value);\n}\n\n/**\n * Creates a new `Err` instance, representing a failed result.\n * @template E The type of the error contained in the `Err`.\n * @param error The error to wrap in the `Err` instance.\n * @returns An `Err` instance containing the given error.\n * @example\n * const result = Err(\"Something went wrong\");\n * console.log(result.isErr()); // true\n * console.log(result.unwrapErr()); // \"Something went wrong\"\n */\nfunction Err<E extends Error>(error: E | string): ErrType<E> {\n\treturn new ErrType(error);\n}\n\n(global as any).Ok = Ok;\n(global as any).Err = Err;\n\nexport { Err, Ok, Result, ErrType, OkType };\n"]}
1
+ {"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,IAAA,SACH,KAEV,CAAA;AAAA,EACU,KAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAKR,YAAY,KAAmB,EAAA;AAC7B,IAAA,KAAA,CAAM,OAAO,KAAA,KAAU,QAAW,GAAA,KAAA,GAAQ,MAAM,OAAO,CAAA;AACvD,IAAA,IAAA,CAAK,QACH,OAAO,KAAA,KAAU,WAAY,IAAI,KAAA,CAAM,KAAK,CAAW,GAAA,KAAA;AACzD,IAAO,MAAA,CAAA,cAAA,CAAe,IAAM,EAAA,I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;AAAA;AAAA;AAAA;AAAA,EAMA,MAAY,GAAA;AACV,IAAA,OAAO,IAAK,CAAA,KAAA;AAAA;AACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAO,EAAiD,EAAA;AACtD,IAAA,OAAO,IAAI,GAAA,CAAG,EAAG,CAAA,IAAA,CAAK,KAAK,CAAC,CAAA;AAAA;AAC9B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,QACE,EAC4B,EAAA;AAC5B,IAAO,OAAA,EAAA,CAAG,KAAK,KAAK,CAAA;AAAA;AACtB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,OAAwB,GAAgD,EAAA;AACtE,IAAO,OAAA,IAAA;AAAA;AACT;AAAA;AAAA;AAAA;AAAA,EAMA,SAAmB,GAAA;AACjB,IAAM,MAAA,IAAI,MAAM,iCAAiC,CAAA;AAAA;AACnD;AAAA;AAAA;AAAA;AAAA,EAMA,EAAK,GAAA;AACH,IAAA,OAAO,MAAM,IAAM,EAAA;AAAA,MACjB,EAAI,EAAA,CAAC,CAAM,KAAA,IAAA,CAAK,CAAC,CAAA;AAAA,MACjB,GAAA,EAAK,CAAC,CAAA,KAAM,IAAK;AAAA,KAClB,CAAA;AAAA;AAEL,CAAA;;;ACzEA,SAASA,IAAM,KAAqB,EAAA;AAClC,EAAO,OAAA,IAAI,GAAO,KAAK,CAAA;AACzB;AAYA,SAASC,KAAqB,KAA+B,EAAA;AAC3D,EAAO,OAAA,IAAI,IAAQ,KAAK,CAAA;AAC1B;AAGC,MAAA,CAAe,EAAKD,GAAAA,GAAAA;AAEpB,MAAA,CAAe,GAAMC,GAAAA,IAAAA","file":"index.js","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`.\n */\nexport class Err<E extends Error>\n extends Error\n implements ResultDefinition<never, E>\n{\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.\n */\n constructor(error: E | string) {\n super(typeof error === \"string\" ? error : error.message);\n this.error =\n typeof error === \"string\" ? (new Error(error) as E) : (error as E);\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 extends Error ? E : Error> {\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.\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 extends Error>(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 * @param _fn The transformation function (ignored in this implementation).\n * @returns The original `Err` instance.\n */\n flatMap<U>(\n _fn: (value: never) => ResultDefinition<U, never>,\n ): ResultDefinition<never, E> {\n return this as unknown as ResultDefinition<never, 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 * 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 * 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 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 * @param fn The transformation function to apply to the value.\n * @returns The result of applying the transformation function.\n */\n flatMap<U>(\n fn: (value: T) => ResultDefinition<U, never>,\n ): ResultDefinition<U, never> {\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`).\n * @param _fn The mapping function for errors (not used).\n * @returns The original `Ok` instance.\n */\n // mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {\n // \treturn this;\n mapErr<U extends Error>(_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 * 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 match(this, {\n Ok: (v) => Some(v),\n Err: (_) => None(),\n });\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 a new `Err` instance, representing a failed result.\n * @template E The type of the error contained in the `Err`.\n * @param error The error to wrap in the `Err` instance.\n * @returns An `Err` instance containing the given error.\n * @example\n * const result = Err(\"Something went wrong\");\n * console.log(result.isErr()); // true\n * console.log(result.unwrapErr()); // \"Something went wrong\"\n */\nfunction Err<E extends Error>(error: E | string): ErrType<E> {\n return new ErrType(error);\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).Ok = Ok;\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).Err = Err;\n\nexport type { Err, Ok, Result, ErrType, OkType };\n"]}