@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.
@@ -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,10 +166,5 @@ function Err2(error) {
149
166
  }
150
167
  global.Ok = Ok2;
151
168
  global.Err = Err2;
152
-
153
- exports.Err = Err2;
154
- exports.ErrType = Err;
155
- exports.Ok = Ok2;
156
- exports.OkType = Ok;
157
169
  //# sourceMappingURL=result.cjs.map
158
170
  //# sourceMappingURL=result.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":";;;AAOa,IAAA,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,K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A;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;AAErD;;;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":"result.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;A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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":"result.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,5 +1,6 @@
1
- import { E as Err$1, O as Ok$1 } from '../err-B7LUEZ0f.cjs';
2
- export { R as Result } from '../err-B7LUEZ0f.cjs';
1
+ import { E as Err$1, O as Ok$1 } from '../types/globals.cjs';
2
+ export { R as Result } from '../types/globals.cjs';
3
+ import '../option-DpT8KCGE.cjs';
3
4
 
4
5
  /**
5
6
  * Creates a new `Ok` instance, representing a successful result.
@@ -1,5 +1,6 @@
1
- import { E as Err$1, O as Ok$1 } from '../err-B7LUEZ0f.js';
2
- export { R as Result } from '../err-B7LUEZ0f.js';
1
+ import { E as Err$1, O as Ok$1 } from '../types/globals.js';
2
+ export { R as Result } from '../types/globals.js';
3
+ import '../option-DpT8KCGE.js';
3
4
 
4
5
  /**
5
6
  * Creates a new `Ok` instance, representing a successful result.
@@ -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, Err as ErrType, Ok2 as Ok, Ok as OkType };
152
167
  //# sourceMappingURL=result.js.map
153
168
  //# sourceMappingURL=result.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":";AAOa,IAAA,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,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AA;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;;;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":"result.js","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;AAClC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,QACE,GAC4B,EAAA;AAC5B,IAAO,OAAA,IAAA;AAAA;AACT;AAAA;AA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AAA,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":"result.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"]}
@@ -1,5 +1,142 @@
1
- import { O as Option, S as Some, N as None } from '../option-DpT8KCGE.cjs';
2
- import { R as Result, O as Ok, E as Err } from '../err-B7LUEZ0f.cjs';
1
+ import { O as Option, N as None, S as Some } from '../option-DpT8KCGE.cjs';
2
+
3
+ type OkType<T> = [T] extends [never] ? never : T;
4
+ type ErrType<E> = [E] extends [never] ? never : E;
5
+ /**
6
+ * Represents the result of an operation that can either succeed (`Ok`) or fail (`Err`).
7
+ */
8
+ type Result<T, E extends Error> = Ok<OkType<T>> | Err<ErrType<E>>;
9
+ /**
10
+ * Represents the result methods that must be implemented for success (`Ok`) or failure (`Err`).
11
+ */
12
+ interface ResultDefinition<T = never, E = never> {
13
+ isOk(): this is Ok<T>;
14
+ isErr(): this is Err<E extends Error ? E : Error>;
15
+ unwrap(): T;
16
+ unwrapErr(): E;
17
+ map<U>(fn: (value: T) => U): ResultDefinition<U, E>;
18
+ flatMap<U>(fn: (value: T) => ResultDefinition<U, E>): ResultDefinition<U, E>;
19
+ mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<T, U>;
20
+ ok(): Option<T>;
21
+ }
22
+
23
+ /**
24
+ * Represents a successful result (`Ok`) that contains a value.
25
+ * @template T The type of the value contained in this `Ok`.
26
+ */
27
+ declare class Ok<T> implements ResultDefinition<T, never> {
28
+ private value;
29
+ /**
30
+ * Creates a new `Ok` instance with the given value.
31
+ * @param value The value to wrap in the `Ok` instance.
32
+ */
33
+ constructor(value: T);
34
+ /**
35
+ * Checks if this result is an `Ok`.
36
+ * @returns `true` because this is an `Ok`.
37
+ */
38
+ isOk(): this is Ok<T>;
39
+ /**
40
+ * Checks if this result is an `Err`.
41
+ * @returns `false` because this is an `Ok`.
42
+ */
43
+ isErr(): this is Err<never>;
44
+ /**
45
+ * Retrieves the value contained in this `Ok`.
46
+ * @returns The value contained in this `Ok`.
47
+ */
48
+ unwrap(): T;
49
+ /**
50
+ * Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.
51
+ * @template U The type of the transformed value.
52
+ * @param fn The transformation function to apply to the value.
53
+ * @returns A new `Ok` containing the transformed value.
54
+ */
55
+ map<U>(fn: (value: T) => U): ResultDefinition<U, never>;
56
+ /**
57
+ * Applies a transformation function that returns a `Result` to the value contained in this `Ok`.
58
+ * @template U The type of the value in the resulting `Result`.
59
+ * @param fn The transformation function to apply to the value.
60
+ * @returns The result of applying the transformation function.
61
+ */
62
+ flatMap<U>(fn: (value: T) => ResultDefinition<U, never>): ResultDefinition<U, never>;
63
+ /**
64
+ * Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.
65
+ * @template U The type of the error (ignored for `Ok`).
66
+ * @param _fn The mapping function for errors (not used).
67
+ * @returns The original `Ok` instance.
68
+ */
69
+ mapErr<U extends Error>(_fn: (err: never) => U): ResultDefinition<T, U>;
70
+ /**
71
+ * Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.
72
+ * @throws An error because `unwrapErr` is called on an `Ok`.
73
+ */
74
+ unwrapErr(): never;
75
+ /**
76
+ * Converts `Result` type to `Option` type.
77
+ * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
78
+ */
79
+ ok(): Option<T>;
80
+ }
81
+
82
+ /**
83
+ * Represents a failed result (`Err`) that contains an error value.
84
+ * @template E The type of the error contained in this `Err`.
85
+ */
86
+ declare class Err<E extends Error> extends Error implements ResultDefinition<never, E> {
87
+ private error;
88
+ /**
89
+ * Creates a new `Err` instance with the given error value.
90
+ * @param error The error to wrap in the `Err` instance.
91
+ */
92
+ constructor(error: E | string);
93
+ /**
94
+ * Checks if this result is an `Ok`.
95
+ * @returns `false` because this is an `Err`.
96
+ */
97
+ isOk(): this is Ok<never>;
98
+ /**
99
+ * Checks if this result is an `Err`.
100
+ * @returns `true` because this is an `Err`.
101
+ */
102
+ isErr(): this is Err<E extends Error ? E : Error>;
103
+ /**
104
+ * Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.
105
+ * @throws An error because `unwrap` is called on an `Err`.
106
+ */
107
+ unwrap(): never;
108
+ /**
109
+ * Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.
110
+ * @template U The type of the value (ignored for `Err`).
111
+ * @param _fn The mapping function for values (not used).
112
+ * @returns The original `Err` instance.
113
+ */
114
+ map<U>(_fn: (value: never) => U): ResultDefinition<never, E>;
115
+ /**
116
+ * Maps the error value using a transformation function and returns a new `Result` with the transformed error.
117
+ * @template U The type of the transformed error.
118
+ * @param fn The transformation function to apply to the error value.
119
+ * @returns A new `Err` containing the transformed error.
120
+ */
121
+ mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<never, U>;
122
+ /**
123
+ * Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.
124
+ * @template U The type of the value in the resulting `Result`.
125
+ * @param _fn The transformation function (ignored in this implementation).
126
+ * @returns The original `Err` instance.
127
+ */
128
+ flatMap<U>(_fn: (value: never) => ResultDefinition<U, never>): ResultDefinition<never, E>;
129
+ /**
130
+ * Retrieves the error value contained in this `Err`.
131
+ * @returns The error value contained in this `Err`.
132
+ */
133
+ unwrapErr(): E;
134
+ /**
135
+ * Converts `Result` type to `Option` type.
136
+ * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
137
+ */
138
+ ok(): None;
139
+ }
3
140
 
4
141
  declare global {
5
142
  function match<T, E extends Error, R>(matcher: Result<T, E>, cases: {
@@ -62,3 +199,5 @@ declare global {
62
199
  */
63
200
  function None(): None;
64
201
  }
202
+
203
+ export { Err as E, Ok as O, type Result as R };
@@ -1,5 +1,142 @@
1
- import { O as Option, S as Some, N as None } from '../option-DpT8KCGE.js';
2
- import { R as Result, O as Ok, E as Err } from '../err-B7LUEZ0f.js';
1
+ import { O as Option, N as None, S as Some } from '../option-DpT8KCGE.js';
2
+
3
+ type OkType<T> = [T] extends [never] ? never : T;
4
+ type ErrType<E> = [E] extends [never] ? never : E;
5
+ /**
6
+ * Represents the result of an operation that can either succeed (`Ok`) or fail (`Err`).
7
+ */
8
+ type Result<T, E extends Error> = Ok<OkType<T>> | Err<ErrType<E>>;
9
+ /**
10
+ * Represents the result methods that must be implemented for success (`Ok`) or failure (`Err`).
11
+ */
12
+ interface ResultDefinition<T = never, E = never> {
13
+ isOk(): this is Ok<T>;
14
+ isErr(): this is Err<E extends Error ? E : Error>;
15
+ unwrap(): T;
16
+ unwrapErr(): E;
17
+ map<U>(fn: (value: T) => U): ResultDefinition<U, E>;
18
+ flatMap<U>(fn: (value: T) => ResultDefinition<U, E>): ResultDefinition<U, E>;
19
+ mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<T, U>;
20
+ ok(): Option<T>;
21
+ }
22
+
23
+ /**
24
+ * Represents a successful result (`Ok`) that contains a value.
25
+ * @template T The type of the value contained in this `Ok`.
26
+ */
27
+ declare class Ok<T> implements ResultDefinition<T, never> {
28
+ private value;
29
+ /**
30
+ * Creates a new `Ok` instance with the given value.
31
+ * @param value The value to wrap in the `Ok` instance.
32
+ */
33
+ constructor(value: T);
34
+ /**
35
+ * Checks if this result is an `Ok`.
36
+ * @returns `true` because this is an `Ok`.
37
+ */
38
+ isOk(): this is Ok<T>;
39
+ /**
40
+ * Checks if this result is an `Err`.
41
+ * @returns `false` because this is an `Ok`.
42
+ */
43
+ isErr(): this is Err<never>;
44
+ /**
45
+ * Retrieves the value contained in this `Ok`.
46
+ * @returns The value contained in this `Ok`.
47
+ */
48
+ unwrap(): T;
49
+ /**
50
+ * Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.
51
+ * @template U The type of the transformed value.
52
+ * @param fn The transformation function to apply to the value.
53
+ * @returns A new `Ok` containing the transformed value.
54
+ */
55
+ map<U>(fn: (value: T) => U): ResultDefinition<U, never>;
56
+ /**
57
+ * Applies a transformation function that returns a `Result` to the value contained in this `Ok`.
58
+ * @template U The type of the value in the resulting `Result`.
59
+ * @param fn The transformation function to apply to the value.
60
+ * @returns The result of applying the transformation function.
61
+ */
62
+ flatMap<U>(fn: (value: T) => ResultDefinition<U, never>): ResultDefinition<U, never>;
63
+ /**
64
+ * Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.
65
+ * @template U The type of the error (ignored for `Ok`).
66
+ * @param _fn The mapping function for errors (not used).
67
+ * @returns The original `Ok` instance.
68
+ */
69
+ mapErr<U extends Error>(_fn: (err: never) => U): ResultDefinition<T, U>;
70
+ /**
71
+ * Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.
72
+ * @throws An error because `unwrapErr` is called on an `Ok`.
73
+ */
74
+ unwrapErr(): never;
75
+ /**
76
+ * Converts `Result` type to `Option` type.
77
+ * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
78
+ */
79
+ ok(): Option<T>;
80
+ }
81
+
82
+ /**
83
+ * Represents a failed result (`Err`) that contains an error value.
84
+ * @template E The type of the error contained in this `Err`.
85
+ */
86
+ declare class Err<E extends Error> extends Error implements ResultDefinition<never, E> {
87
+ private error;
88
+ /**
89
+ * Creates a new `Err` instance with the given error value.
90
+ * @param error The error to wrap in the `Err` instance.
91
+ */
92
+ constructor(error: E | string);
93
+ /**
94
+ * Checks if this result is an `Ok`.
95
+ * @returns `false` because this is an `Err`.
96
+ */
97
+ isOk(): this is Ok<never>;
98
+ /**
99
+ * Checks if this result is an `Err`.
100
+ * @returns `true` because this is an `Err`.
101
+ */
102
+ isErr(): this is Err<E extends Error ? E : Error>;
103
+ /**
104
+ * Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.
105
+ * @throws An error because `unwrap` is called on an `Err`.
106
+ */
107
+ unwrap(): never;
108
+ /**
109
+ * Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.
110
+ * @template U The type of the value (ignored for `Err`).
111
+ * @param _fn The mapping function for values (not used).
112
+ * @returns The original `Err` instance.
113
+ */
114
+ map<U>(_fn: (value: never) => U): ResultDefinition<never, E>;
115
+ /**
116
+ * Maps the error value using a transformation function and returns a new `Result` with the transformed error.
117
+ * @template U The type of the transformed error.
118
+ * @param fn The transformation function to apply to the error value.
119
+ * @returns A new `Err` containing the transformed error.
120
+ */
121
+ mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<never, U>;
122
+ /**
123
+ * Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.
124
+ * @template U The type of the value in the resulting `Result`.
125
+ * @param _fn The transformation function (ignored in this implementation).
126
+ * @returns The original `Err` instance.
127
+ */
128
+ flatMap<U>(_fn: (value: never) => ResultDefinition<U, never>): ResultDefinition<never, E>;
129
+ /**
130
+ * Retrieves the error value contained in this `Err`.
131
+ * @returns The error value contained in this `Err`.
132
+ */
133
+ unwrapErr(): E;
134
+ /**
135
+ * Converts `Result` type to `Option` type.
136
+ * @returns `Some` if the result is `Ok`, `None` if the result is `Err`.
137
+ */
138
+ ok(): None;
139
+ }
3
140
 
4
141
  declare global {
5
142
  function match<T, E extends Error, R>(matcher: Result<T, E>, cases: {
@@ -62,3 +199,5 @@ declare global {
62
199
  */
63
200
  function None(): None;
64
201
  }
202
+
203
+ export { Err as E, Ok as O, type Result as R };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@consolidados/results",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Result types, ease and simple",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -1,128 +0,0 @@
1
- /**
2
- * Represents a successful result (`Ok`) that contains a value.
3
- * @template T The type of the value contained in this `Ok`.
4
- */
5
- declare class Ok<T> implements ResultDefinition<T, never> {
6
- private value;
7
- /**
8
- * Creates a new `Ok` instance with the given value.
9
- * @param value The value to wrap in the `Ok` instance.
10
- */
11
- constructor(value: T);
12
- /**
13
- * Checks if this result is an `Ok`.
14
- * @returns `true` because this is an `Ok`.
15
- */
16
- isOk(): this is Ok<T>;
17
- /**
18
- * Checks if this result is an `Err`.
19
- * @returns `false` because this is an `Ok`.
20
- */
21
- isErr(): this is Err<never>;
22
- /**
23
- * Retrieves the value contained in this `Ok`.
24
- * @returns The value contained in this `Ok`.
25
- */
26
- unwrap(): T;
27
- /**
28
- * Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.
29
- * @template U The type of the transformed value.
30
- * @param fn The transformation function to apply to the value.
31
- * @returns A new `Ok` containing the transformed value.
32
- */
33
- map<U>(fn: (value: T) => U): ResultDefinition<U, never>;
34
- /**
35
- * Applies a transformation function that returns a `Result` to the value contained in this `Ok`.
36
- * @template U The type of the value in the resulting `Result`.
37
- * @param fn The transformation function to apply to the value.
38
- * @returns The result of applying the transformation function.
39
- */
40
- flatMap<U>(fn: (value: T) => ResultDefinition<U, never>): ResultDefinition<U, never>;
41
- /**
42
- * Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.
43
- * @template U The type of the error (ignored for `Ok`).
44
- * @param _fn The mapping function for errors (not used).
45
- * @returns The original `Ok` instance.
46
- */
47
- mapErr<U extends Error>(_fn: (err: never) => U): ResultDefinition<T, U>;
48
- /**
49
- * Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.
50
- * @throws An error because `unwrapErr` is called on an `Ok`.
51
- */
52
- unwrapErr(): never;
53
- }
54
-
55
- type OkType<T> = [T] extends [never] ? never : T;
56
- type ErrType<E> = [E] extends [never] ? never : E;
57
- /**
58
- * Represents the result of an operation that can either succeed (`Ok`) or fail (`Err`).
59
- */
60
- type Result<T, E extends Error> = Ok<OkType<T>> | Err<ErrType<E>>;
61
- /**
62
- * Represents the result methods that must be implemented for success (`Ok`) or failure (`Err`).
63
- */
64
- interface ResultDefinition<T = never, E = never> {
65
- isOk(): this is Ok<T>;
66
- isErr(): this is Err<E extends Error ? E : Error>;
67
- unwrap(): T;
68
- unwrapErr(): E;
69
- map<U>(fn: (value: T) => U): ResultDefinition<U, E>;
70
- flatMap<U>(fn: (value: T) => ResultDefinition<U, E>): ResultDefinition<U, E>;
71
- mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<T, U>;
72
- }
73
-
74
- /**
75
- * Represents a failed result (`Err`) that contains an error value.
76
- * @template E The type of the error contained in this `Err`.
77
- */
78
- declare class Err<E extends Error> extends Error implements ResultDefinition<never, E> {
79
- private error;
80
- /**
81
- * Creates a new `Err` instance with the given error value.
82
- * @param error The error to wrap in the `Err` instance.
83
- */
84
- constructor(error: E | string);
85
- /**
86
- * Checks if this result is an `Ok`.
87
- * @returns `false` because this is an `Err`.
88
- */
89
- isOk(): this is Ok<never>;
90
- /**
91
- * Checks if this result is an `Err`.
92
- * @returns `true` because this is an `Err`.
93
- */
94
- isErr(): this is Err<E extends Error ? E : Error>;
95
- /**
96
- * Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.
97
- * @throws An error because `unwrap` is called on an `Err`.
98
- */
99
- unwrap(): never;
100
- /**
101
- * Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.
102
- * @template U The type of the value (ignored for `Err`).
103
- * @param _fn The mapping function for values (not used).
104
- * @returns The original `Err` instance.
105
- */
106
- map<U>(_fn: (value: never) => U): ResultDefinition<never, E>;
107
- /**
108
- * Maps the error value using a transformation function and returns a new `Result` with the transformed error.
109
- * @template U The type of the transformed error.
110
- * @param fn The transformation function to apply to the error value.
111
- * @returns A new `Err` containing the transformed error.
112
- */
113
- mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<never, U>;
114
- /**
115
- * Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.
116
- * @template U The type of the value in the resulting `Result`.
117
- * @param _fn The transformation function (ignored in this implementation).
118
- * @returns The original `Err` instance.
119
- */
120
- flatMap<U>(_fn: (value: never) => ResultDefinition<U, never>): ResultDefinition<never, E>;
121
- /**
122
- * Retrieves the error value contained in this `Err`.
123
- * @returns The error value contained in this `Err`.
124
- */
125
- unwrapErr(): E;
126
- }
127
-
128
- export { Err as E, Ok as O, type Result as R };