@consolidados/results 0.1.2 → 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.
Files changed (46) hide show
  1. package/README.md +2 -1
  2. package/dist/helpers/match.cjs +27 -0
  3. package/dist/helpers/match.cjs.map +1 -0
  4. package/dist/helpers/match.d.cts +13 -0
  5. package/dist/helpers/match.d.ts +13 -0
  6. package/dist/helpers/match.js +25 -0
  7. package/dist/helpers/match.js.map +1 -0
  8. package/dist/index.cjs +316 -0
  9. package/dist/index.cjs.map +1 -0
  10. package/dist/index.d.cts +5 -0
  11. package/dist/index.d.ts +5 -0
  12. package/dist/index.js +312 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/option/index.cjs +126 -0
  15. package/dist/option/index.cjs.map +1 -0
  16. package/dist/option/index.d.cts +2 -0
  17. package/dist/option/index.d.ts +2 -0
  18. package/dist/option/index.js +123 -0
  19. package/dist/option/index.js.map +1 -0
  20. package/dist/option/option.cjs +126 -0
  21. package/dist/option/option.cjs.map +1 -0
  22. package/dist/option/option.d.cts +25 -0
  23. package/dist/option/option.d.ts +25 -0
  24. package/dist/option/option.js +123 -0
  25. package/dist/option/option.js.map +1 -0
  26. package/dist/option-DpT8KCGE.d.cts +96 -0
  27. package/dist/option-DpT8KCGE.d.ts +96 -0
  28. package/dist/result/index.cjs +170 -0
  29. package/dist/result/index.cjs.map +1 -0
  30. package/dist/result/index.d.cts +3 -0
  31. package/dist/result/index.d.ts +3 -0
  32. package/dist/result/index.js +168 -0
  33. package/dist/result/index.js.map +1 -0
  34. package/dist/result/result.cjs +170 -0
  35. package/dist/result/result.cjs.map +1 -0
  36. package/dist/result/result.d.cts +28 -0
  37. package/dist/result/result.d.ts +28 -0
  38. package/dist/result/result.js +168 -0
  39. package/dist/result/result.js.map +1 -0
  40. package/dist/types/globals.cjs +4 -0
  41. package/dist/types/globals.cjs.map +1 -0
  42. package/dist/types/globals.d.cts +203 -0
  43. package/dist/types/globals.d.ts +203 -0
  44. package/dist/types/globals.js +3 -0
  45. package/dist/types/globals.js.map +1 -0
  46. package/package.json +2 -2
@@ -0,0 +1,170 @@
1
+ 'use strict';
2
+
3
+ // src/result/__internal__/return-types/err.ts
4
+ var Err = class _Err extends Error {
5
+ error;
6
+ /**
7
+ * Creates a new `Err` instance with the given error value.
8
+ * @param error The error to wrap in the `Err` instance.
9
+ */
10
+ constructor(error) {
11
+ super(typeof error === "string" ? error : error.message);
12
+ this.error = typeof error === "string" ? new Error(error) : error;
13
+ Object.setPrototypeOf(this, _Err.prototype);
14
+ if (Error.captureStackTrace) {
15
+ Error.captureStackTrace(this, _Err);
16
+ }
17
+ }
18
+ /**
19
+ * Checks if this result is an `Ok`.
20
+ * @returns `false` because this is an `Err`.
21
+ */
22
+ isOk() {
23
+ return false;
24
+ }
25
+ /**
26
+ * Checks if this result is an `Err`.
27
+ * @returns `true` because this is an `Err`.
28
+ */
29
+ isErr() {
30
+ return true;
31
+ }
32
+ /**
33
+ * Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.
34
+ * @throws An error because `unwrap` is called on an `Err`.
35
+ */
36
+ unwrap() {
37
+ throw new Error("Called unwrap on an Err value");
38
+ }
39
+ /**
40
+ * Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.
41
+ * @template U The type of the value (ignored for `Err`).
42
+ * @param _fn The mapping function for values (not used).
43
+ * @returns The original `Err` instance.
44
+ */
45
+ map(_fn) {
46
+ return this;
47
+ }
48
+ /**
49
+ * Maps the error value using a transformation function and returns a new `Result` with the transformed error.
50
+ * @template U The type of the transformed error.
51
+ * @param fn The transformation function to apply to the error value.
52
+ * @returns A new `Err` containing the transformed error.
53
+ */
54
+ mapErr(fn) {
55
+ return new _Err(fn(this.error));
56
+ }
57
+ /**
58
+ * Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.
59
+ * @template U The type of the value in the resulting `Result`.
60
+ * @param _fn The transformation function (ignored in this implementation).
61
+ * @returns The original `Err` instance.
62
+ */
63
+ flatMap(_fn) {
64
+ return this;
65
+ }
66
+ /**
67
+ * Retrieves the error value contained in this `Err`.
68
+ * @returns The error value contained in this `Err`.
69
+ */
70
+ unwrapErr() {
71
+ return this.error;
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
+ }
80
+ };
81
+
82
+ // src/result/__internal__/return-types/ok.ts
83
+ var Ok = class _Ok {
84
+ /**
85
+ * Creates a new `Ok` instance with the given value.
86
+ * @param value The value to wrap in the `Ok` instance.
87
+ */
88
+ constructor(value) {
89
+ this.value = value;
90
+ }
91
+ /**
92
+ * Checks if this result is an `Ok`.
93
+ * @returns `true` because this is an `Ok`.
94
+ */
95
+ isOk() {
96
+ return true;
97
+ }
98
+ /**
99
+ * Checks if this result is an `Err`.
100
+ * @returns `false` because this is an `Ok`.
101
+ */
102
+ isErr() {
103
+ return false;
104
+ }
105
+ /**
106
+ * Retrieves the value contained in this `Ok`.
107
+ * @returns The value contained in this `Ok`.
108
+ */
109
+ unwrap() {
110
+ return this.value;
111
+ }
112
+ /**
113
+ * Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.
114
+ * @template U The type of the transformed value.
115
+ * @param fn The transformation function to apply to the value.
116
+ * @returns A new `Ok` containing the transformed value.
117
+ */
118
+ map(fn) {
119
+ return new _Ok(fn(this.value));
120
+ }
121
+ /**
122
+ * Applies a transformation function that returns a `Result` to the value contained in this `Ok`.
123
+ * @template U The type of the value in the resulting `Result`.
124
+ * @param fn The transformation function to apply to the value.
125
+ * @returns The result of applying the transformation function.
126
+ */
127
+ flatMap(fn) {
128
+ return fn(this.value);
129
+ }
130
+ /**
131
+ * Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.
132
+ * @template U The type of the error (ignored for `Ok`).
133
+ * @param _fn The mapping function for errors (not used).
134
+ * @returns The original `Ok` instance.
135
+ */
136
+ // mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {
137
+ // return this;
138
+ mapErr(_fn) {
139
+ return this;
140
+ }
141
+ /**
142
+ * Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.
143
+ * @throws An error because `unwrapErr` is called on an `Ok`.
144
+ */
145
+ unwrapErr() {
146
+ throw new Error("Called unwrapErr on an Ok value");
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
+ }
158
+ };
159
+
160
+ // src/result/result.ts
161
+ function Ok2(value) {
162
+ return new Ok(value);
163
+ }
164
+ function Err2(error) {
165
+ return new Err(error);
166
+ }
167
+ global.Ok = Ok2;
168
+ global.Err = Err2;
169
+ //# sourceMappingURL=index.cjs.map
170
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +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,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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D,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.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"]}
@@ -0,0 +1,3 @@
1
+ export { Err, Ok } from './result.cjs';
2
+ export { R as Result } from '../types/globals.cjs';
3
+ import '../option-DpT8KCGE.cjs';
@@ -0,0 +1,3 @@
1
+ export { Err, Ok } from './result.js';
2
+ export { R as Result } from '../types/globals.js';
3
+ import '../option-DpT8KCGE.js';
@@ -0,0 +1,168 @@
1
+ // src/result/__internal__/return-types/err.ts
2
+ var Err = class _Err extends Error {
3
+ error;
4
+ /**
5
+ * Creates a new `Err` instance with the given error value.
6
+ * @param error The error to wrap in the `Err` instance.
7
+ */
8
+ constructor(error) {
9
+ super(typeof error === "string" ? error : error.message);
10
+ this.error = typeof error === "string" ? new Error(error) : error;
11
+ Object.setPrototypeOf(this, _Err.prototype);
12
+ if (Error.captureStackTrace) {
13
+ Error.captureStackTrace(this, _Err);
14
+ }
15
+ }
16
+ /**
17
+ * Checks if this result is an `Ok`.
18
+ * @returns `false` because this is an `Err`.
19
+ */
20
+ isOk() {
21
+ return false;
22
+ }
23
+ /**
24
+ * Checks if this result is an `Err`.
25
+ * @returns `true` because this is an `Err`.
26
+ */
27
+ isErr() {
28
+ return true;
29
+ }
30
+ /**
31
+ * Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.
32
+ * @throws An error because `unwrap` is called on an `Err`.
33
+ */
34
+ unwrap() {
35
+ throw new Error("Called unwrap on an Err value");
36
+ }
37
+ /**
38
+ * Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.
39
+ * @template U The type of the value (ignored for `Err`).
40
+ * @param _fn The mapping function for values (not used).
41
+ * @returns The original `Err` instance.
42
+ */
43
+ map(_fn) {
44
+ return this;
45
+ }
46
+ /**
47
+ * Maps the error value using a transformation function and returns a new `Result` with the transformed error.
48
+ * @template U The type of the transformed error.
49
+ * @param fn The transformation function to apply to the error value.
50
+ * @returns A new `Err` containing the transformed error.
51
+ */
52
+ mapErr(fn) {
53
+ return new _Err(fn(this.error));
54
+ }
55
+ /**
56
+ * Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.
57
+ * @template U The type of the value in the resulting `Result`.
58
+ * @param _fn The transformation function (ignored in this implementation).
59
+ * @returns The original `Err` instance.
60
+ */
61
+ flatMap(_fn) {
62
+ return this;
63
+ }
64
+ /**
65
+ * Retrieves the error value contained in this `Err`.
66
+ * @returns The error value contained in this `Err`.
67
+ */
68
+ unwrapErr() {
69
+ return this.error;
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
+ }
78
+ };
79
+
80
+ // src/result/__internal__/return-types/ok.ts
81
+ var Ok = class _Ok {
82
+ /**
83
+ * Creates a new `Ok` instance with the given value.
84
+ * @param value The value to wrap in the `Ok` instance.
85
+ */
86
+ constructor(value) {
87
+ this.value = value;
88
+ }
89
+ /**
90
+ * Checks if this result is an `Ok`.
91
+ * @returns `true` because this is an `Ok`.
92
+ */
93
+ isOk() {
94
+ return true;
95
+ }
96
+ /**
97
+ * Checks if this result is an `Err`.
98
+ * @returns `false` because this is an `Ok`.
99
+ */
100
+ isErr() {
101
+ return false;
102
+ }
103
+ /**
104
+ * Retrieves the value contained in this `Ok`.
105
+ * @returns The value contained in this `Ok`.
106
+ */
107
+ unwrap() {
108
+ return this.value;
109
+ }
110
+ /**
111
+ * Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.
112
+ * @template U The type of the transformed value.
113
+ * @param fn The transformation function to apply to the value.
114
+ * @returns A new `Ok` containing the transformed value.
115
+ */
116
+ map(fn) {
117
+ return new _Ok(fn(this.value));
118
+ }
119
+ /**
120
+ * Applies a transformation function that returns a `Result` to the value contained in this `Ok`.
121
+ * @template U The type of the value in the resulting `Result`.
122
+ * @param fn The transformation function to apply to the value.
123
+ * @returns The result of applying the transformation function.
124
+ */
125
+ flatMap(fn) {
126
+ return fn(this.value);
127
+ }
128
+ /**
129
+ * Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.
130
+ * @template U The type of the error (ignored for `Ok`).
131
+ * @param _fn The mapping function for errors (not used).
132
+ * @returns The original `Ok` instance.
133
+ */
134
+ // mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {
135
+ // return this;
136
+ mapErr(_fn) {
137
+ return this;
138
+ }
139
+ /**
140
+ * Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.
141
+ * @throws An error because `unwrapErr` is called on an `Ok`.
142
+ */
143
+ unwrapErr() {
144
+ throw new Error("Called unwrapErr on an Ok value");
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
+ }
156
+ };
157
+
158
+ // src/result/result.ts
159
+ function Ok2(value) {
160
+ return new Ok(value);
161
+ }
162
+ function Err2(error) {
163
+ return new Err(error);
164
+ }
165
+ global.Ok = Ok2;
166
+ global.Err = Err2;
167
+ //# sourceMappingURL=index.js.map
168
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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,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;AAA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AAM,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"]}
@@ -0,0 +1,170 @@
1
+ 'use strict';
2
+
3
+ // src/result/__internal__/return-types/err.ts
4
+ var Err = class _Err extends Error {
5
+ error;
6
+ /**
7
+ * Creates a new `Err` instance with the given error value.
8
+ * @param error The error to wrap in the `Err` instance.
9
+ */
10
+ constructor(error) {
11
+ super(typeof error === "string" ? error : error.message);
12
+ this.error = typeof error === "string" ? new Error(error) : error;
13
+ Object.setPrototypeOf(this, _Err.prototype);
14
+ if (Error.captureStackTrace) {
15
+ Error.captureStackTrace(this, _Err);
16
+ }
17
+ }
18
+ /**
19
+ * Checks if this result is an `Ok`.
20
+ * @returns `false` because this is an `Err`.
21
+ */
22
+ isOk() {
23
+ return false;
24
+ }
25
+ /**
26
+ * Checks if this result is an `Err`.
27
+ * @returns `true` because this is an `Err`.
28
+ */
29
+ isErr() {
30
+ return true;
31
+ }
32
+ /**
33
+ * Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.
34
+ * @throws An error because `unwrap` is called on an `Err`.
35
+ */
36
+ unwrap() {
37
+ throw new Error("Called unwrap on an Err value");
38
+ }
39
+ /**
40
+ * Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.
41
+ * @template U The type of the value (ignored for `Err`).
42
+ * @param _fn The mapping function for values (not used).
43
+ * @returns The original `Err` instance.
44
+ */
45
+ map(_fn) {
46
+ return this;
47
+ }
48
+ /**
49
+ * Maps the error value using a transformation function and returns a new `Result` with the transformed error.
50
+ * @template U The type of the transformed error.
51
+ * @param fn The transformation function to apply to the error value.
52
+ * @returns A new `Err` containing the transformed error.
53
+ */
54
+ mapErr(fn) {
55
+ return new _Err(fn(this.error));
56
+ }
57
+ /**
58
+ * Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.
59
+ * @template U The type of the value in the resulting `Result`.
60
+ * @param _fn The transformation function (ignored in this implementation).
61
+ * @returns The original `Err` instance.
62
+ */
63
+ flatMap(_fn) {
64
+ return this;
65
+ }
66
+ /**
67
+ * Retrieves the error value contained in this `Err`.
68
+ * @returns The error value contained in this `Err`.
69
+ */
70
+ unwrapErr() {
71
+ return this.error;
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
+ }
80
+ };
81
+
82
+ // src/result/__internal__/return-types/ok.ts
83
+ var Ok = class _Ok {
84
+ /**
85
+ * Creates a new `Ok` instance with the given value.
86
+ * @param value The value to wrap in the `Ok` instance.
87
+ */
88
+ constructor(value) {
89
+ this.value = value;
90
+ }
91
+ /**
92
+ * Checks if this result is an `Ok`.
93
+ * @returns `true` because this is an `Ok`.
94
+ */
95
+ isOk() {
96
+ return true;
97
+ }
98
+ /**
99
+ * Checks if this result is an `Err`.
100
+ * @returns `false` because this is an `Ok`.
101
+ */
102
+ isErr() {
103
+ return false;
104
+ }
105
+ /**
106
+ * Retrieves the value contained in this `Ok`.
107
+ * @returns The value contained in this `Ok`.
108
+ */
109
+ unwrap() {
110
+ return this.value;
111
+ }
112
+ /**
113
+ * Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.
114
+ * @template U The type of the transformed value.
115
+ * @param fn The transformation function to apply to the value.
116
+ * @returns A new `Ok` containing the transformed value.
117
+ */
118
+ map(fn) {
119
+ return new _Ok(fn(this.value));
120
+ }
121
+ /**
122
+ * Applies a transformation function that returns a `Result` to the value contained in this `Ok`.
123
+ * @template U The type of the value in the resulting `Result`.
124
+ * @param fn The transformation function to apply to the value.
125
+ * @returns The result of applying the transformation function.
126
+ */
127
+ flatMap(fn) {
128
+ return fn(this.value);
129
+ }
130
+ /**
131
+ * Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.
132
+ * @template U The type of the error (ignored for `Ok`).
133
+ * @param _fn The mapping function for errors (not used).
134
+ * @returns The original `Ok` instance.
135
+ */
136
+ // mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {
137
+ // return this;
138
+ mapErr(_fn) {
139
+ return this;
140
+ }
141
+ /**
142
+ * Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.
143
+ * @throws An error because `unwrapErr` is called on an `Ok`.
144
+ */
145
+ unwrapErr() {
146
+ throw new Error("Called unwrapErr on an Ok value");
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
+ }
158
+ };
159
+
160
+ // src/result/result.ts
161
+ function Ok2(value) {
162
+ return new Ok(value);
163
+ }
164
+ function Err2(error) {
165
+ return new Err(error);
166
+ }
167
+ global.Ok = Ok2;
168
+ global.Err = Err2;
169
+ //# sourceMappingURL=result.cjs.map
170
+ //# sourceMappingURL=result.cjs.map
@@ -0,0 +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,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,OAA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"./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"]}
@@ -0,0 +1,28 @@
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';
4
+
5
+ /**
6
+ * Creates a new `Ok` instance, representing a successful result.
7
+ * @template T The type of the value contained in the `Ok`.
8
+ * @param value The value to wrap in the `Ok` instance.
9
+ * @returns An `Ok` instance containing the given value.
10
+ * @example
11
+ * const result = Ok(42);
12
+ * console.log(result.isOk()); // true
13
+ * console.log(result.unwrap()); // 42
14
+ */
15
+ declare function Ok<T>(value: T): Ok$1<T>;
16
+ /**
17
+ * Creates a new `Err` instance, representing a failed result.
18
+ * @template E The type of the error contained in the `Err`.
19
+ * @param error The error to wrap in the `Err` instance.
20
+ * @returns An `Err` instance containing the given error.
21
+ * @example
22
+ * const result = Err("Something went wrong");
23
+ * console.log(result.isErr()); // true
24
+ * console.log(result.unwrapErr()); // "Something went wrong"
25
+ */
26
+ declare function Err<E extends Error>(error: E | string): Err$1<E>;
27
+
28
+ export { Err, Err$1 as ErrType, Ok, Ok$1 as OkType };
@@ -0,0 +1,28 @@
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';
4
+
5
+ /**
6
+ * Creates a new `Ok` instance, representing a successful result.
7
+ * @template T The type of the value contained in the `Ok`.
8
+ * @param value The value to wrap in the `Ok` instance.
9
+ * @returns An `Ok` instance containing the given value.
10
+ * @example
11
+ * const result = Ok(42);
12
+ * console.log(result.isOk()); // true
13
+ * console.log(result.unwrap()); // 42
14
+ */
15
+ declare function Ok<T>(value: T): Ok$1<T>;
16
+ /**
17
+ * Creates a new `Err` instance, representing a failed result.
18
+ * @template E The type of the error contained in the `Err`.
19
+ * @param error The error to wrap in the `Err` instance.
20
+ * @returns An `Err` instance containing the given error.
21
+ * @example
22
+ * const result = Err("Something went wrong");
23
+ * console.log(result.isErr()); // true
24
+ * console.log(result.unwrapErr()); // "Something went wrong"
25
+ */
26
+ declare function Err<E extends Error>(error: E | string): Err$1<E>;
27
+
28
+ export { Err, Err$1 as ErrType, Ok, Ok$1 as OkType };