@consolidados/results 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.release-it.json +20 -0
- package/README.md +361 -0
- package/dist/err-B7LUEZ0f.d.cts +128 -0
- package/dist/err-B7LUEZ0f.d.ts +128 -0
- package/dist/helpers/match.cjs +27 -0
- package/dist/helpers/match.cjs.map +1 -0
- package/dist/helpers/match.d.cts +13 -0
- package/dist/helpers/match.d.ts +13 -0
- package/dist/helpers/match.js +25 -0
- package/dist/helpers/match.js.map +1 -0
- package/dist/index.cjs +301 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +295 -0
- package/dist/index.js.map +1 -0
- package/dist/option/index.cjs +126 -0
- package/dist/option/index.cjs.map +1 -0
- package/dist/option/index.d.cts +2 -0
- package/dist/option/index.d.ts +2 -0
- package/dist/option/index.js +123 -0
- package/dist/option/index.js.map +1 -0
- package/dist/option/option.cjs +126 -0
- package/dist/option/option.cjs.map +1 -0
- package/dist/option/option.d.cts +25 -0
- package/dist/option/option.d.ts +25 -0
- package/dist/option/option.js +123 -0
- package/dist/option/option.js.map +1 -0
- package/dist/option-DpT8KCGE.d.cts +96 -0
- package/dist/option-DpT8KCGE.d.ts +96 -0
- package/dist/result/index.cjs +156 -0
- package/dist/result/index.cjs.map +1 -0
- package/dist/result/index.d.cts +2 -0
- package/dist/result/index.d.ts +2 -0
- package/dist/result/index.js +153 -0
- package/dist/result/index.js.map +1 -0
- package/dist/result/result.cjs +158 -0
- package/dist/result/result.cjs.map +1 -0
- package/dist/result/result.d.cts +27 -0
- package/dist/result/result.d.ts +27 -0
- package/dist/result/result.js +153 -0
- package/dist/result/result.js.map +1 -0
- package/dist/types/globals.cjs +4 -0
- package/dist/types/globals.cjs.map +1 -0
- package/dist/types/globals.d.cts +64 -0
- package/dist/types/globals.d.ts +64 -0
- package/dist/types/globals.js +3 -0
- package/dist/types/globals.js.map +1 -0
- package/index.ts +3 -0
- package/package.json +66 -0
- package/vite.config.ts +16 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { R as Result } from '../err-B7LUEZ0f.js';
|
|
2
|
+
import { O as Option } from '../option-DpT8KCGE.js';
|
|
3
|
+
|
|
4
|
+
declare function match<T, E extends Error, R>(matcher: Result<T, E>, cases: {
|
|
5
|
+
Ok: (value: T) => R;
|
|
6
|
+
Err: (error: E) => R;
|
|
7
|
+
}): R;
|
|
8
|
+
declare function match<T, R>(matcher: Option<T>, cases: {
|
|
9
|
+
Some: (value: T) => R;
|
|
10
|
+
None: () => R;
|
|
11
|
+
}): R;
|
|
12
|
+
|
|
13
|
+
export { match };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// src/helpers/match.ts
|
|
2
|
+
function match(matcher, cases) {
|
|
3
|
+
if ("isOk" in matcher && matcher.isOk()) {
|
|
4
|
+
if (!cases.Ok) throw new Error("Missing case for Ok");
|
|
5
|
+
return cases.Ok(matcher.unwrap());
|
|
6
|
+
}
|
|
7
|
+
if ("isErr" in matcher && matcher.isErr()) {
|
|
8
|
+
if (!cases.Err) throw new Error("Missing case for Err");
|
|
9
|
+
return cases.Err(matcher.unwrapErr());
|
|
10
|
+
}
|
|
11
|
+
if ("isSome" in matcher && matcher.isSome()) {
|
|
12
|
+
if (!cases.Some) throw new Error("Missing case for Some");
|
|
13
|
+
return cases.Some(matcher.unwrap());
|
|
14
|
+
}
|
|
15
|
+
if ("isNone" in matcher && matcher.isNone()) {
|
|
16
|
+
if (!cases.None) throw new Error("Missing case for None");
|
|
17
|
+
return cases.None();
|
|
18
|
+
}
|
|
19
|
+
throw new Error("Invalid matcher or missing case");
|
|
20
|
+
}
|
|
21
|
+
global.match = match;
|
|
22
|
+
|
|
23
|
+
export { match };
|
|
24
|
+
//# sourceMappingURL=match.js.map
|
|
25
|
+
//# sourceMappingURL=match.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/helpers/match.ts"],"names":[],"mappings":";AAiBO,SAAS,KAAA,CACf,SACA,KAMI,EAAA;AACJ,EAAA,IAAI,MAAU,IAAA,OAAA,IAAW,OAAQ,CAAA,IAAA,EAAQ,EAAA;AACxC,IAAA,IAAI,CAAC,KAAM,CAAA,EAAA,EAAU,MAAA,IAAI,MAAM,qBAAqB,CAAA;AACpD,IAAA,OAAO,KAAM,CAAA,EAAA,CAAG,OAAQ,CAAA,MAAA,EAAQ,CAAA;AAAA;AAGjC,EAAA,IAAI,OAAW,IAAA,OAAA,IAAW,OAAQ,CAAA,KAAA,EAAS,EAAA;AAC1C,IAAA,IAAI,CAAC,KAAM,CAAA,GAAA,EAAW,MAAA,IAAI,MAAM,sBAAsB,CAAA;AACtD,IAAA,OAAO,KAAM,CAAA,GAAA,CAAI,OAAQ,CAAA,SAAA,EAAgB,CAAA;AAAA;AAG1C,EAAA,IAAI,QAAY,IAAA,OAAA,IAAW,OAAQ,CAAA,MAAA,EAAU,EAAA;AAC5C,IAAA,IAAI,CAAC,KAAM,CAAA,IAAA,EAAY,MAAA,IAAI,MAAM,uBAAuB,CAAA;AACxD,IAAA,OAAO,KAAM,CAAA,IAAA,CAAK,OAAQ,CAAA,MAAA,EAAQ,CAAA;AAAA;AAGnC,EAAA,IAAI,QAAY,IAAA,OAAA,IAAW,OAAQ,CAAA,MAAA,EAAU,EAAA;AAC5C,IAAA,IAAI,CAAC,KAAM,CAAA,IAAA,EAAY,MAAA,IAAI,MAAM,uBAAuB,CAAA;AACxD,IAAA,OAAO,MAAM,IAAK,EAAA;AAAA;AAGnB,EAAM,MAAA,IAAI,MAAM,iCAAiC,CAAA;AAClD;AAEC,MAAA,CAAe,KAAQ,GAAA,KAAA","file":"match.js","sourcesContent":["import { Result } from \"../result\";\nimport { Option } from \"../option\";\n\nexport function match<T, E extends Error, R>(\n\tmatcher: Result<T, E>,\n\tcases: {\n\t\tOk: (value: T) => R;\n\t\tErr: (error: E) => R;\n\t},\n): R;\nexport function match<T, R>(\n\tmatcher: Option<T>,\n\tcases: {\n\t\tSome: (value: T) => R;\n\t\tNone: () => R;\n\t},\n): R;\nexport function match<T, E extends Error, R>(\n\tmatcher: Result<T, E> | Option<T>,\n\tcases: {\n\t\tOk?: (value: T) => R;\n\t\tErr?: (error: E) => R;\n\t\tSome?: (value: T) => R;\n\t\tNone?: () => R;\n\t},\n): R {\n\tif (\"isOk\" in matcher && matcher.isOk()) {\n\t\tif (!cases.Ok) throw new Error(\"Missing case for Ok\");\n\t\treturn cases.Ok(matcher.unwrap());\n\t}\n\n\tif (\"isErr\" in matcher && matcher.isErr()) {\n\t\tif (!cases.Err) throw new Error(\"Missing case for Err\");\n\t\treturn cases.Err(matcher.unwrapErr() as E);\n\t}\n\n\tif (\"isSome\" in matcher && matcher.isSome()) {\n\t\tif (!cases.Some) throw new Error(\"Missing case for Some\");\n\t\treturn cases.Some(matcher.unwrap());\n\t}\n\n\tif (\"isNone\" in matcher && matcher.isNone()) {\n\t\tif (!cases.None) throw new Error(\"Missing case for None\");\n\t\treturn cases.None();\n\t}\n\n\tthrow new Error(\"Invalid matcher or missing case\");\n}\n\n(global as any).match = match;\n"]}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/helpers/match.ts
|
|
4
|
+
function match(matcher, cases) {
|
|
5
|
+
if ("isOk" in matcher && matcher.isOk()) {
|
|
6
|
+
if (!cases.Ok) throw new Error("Missing case for Ok");
|
|
7
|
+
return cases.Ok(matcher.unwrap());
|
|
8
|
+
}
|
|
9
|
+
if ("isErr" in matcher && matcher.isErr()) {
|
|
10
|
+
if (!cases.Err) throw new Error("Missing case for Err");
|
|
11
|
+
return cases.Err(matcher.unwrapErr());
|
|
12
|
+
}
|
|
13
|
+
if ("isSome" in matcher && matcher.isSome()) {
|
|
14
|
+
if (!cases.Some) throw new Error("Missing case for Some");
|
|
15
|
+
return cases.Some(matcher.unwrap());
|
|
16
|
+
}
|
|
17
|
+
if ("isNone" in matcher && matcher.isNone()) {
|
|
18
|
+
if (!cases.None) throw new Error("Missing case for None");
|
|
19
|
+
return cases.None();
|
|
20
|
+
}
|
|
21
|
+
throw new Error("Invalid matcher or missing case");
|
|
22
|
+
}
|
|
23
|
+
global.match = match;
|
|
24
|
+
|
|
25
|
+
// src/option/__internal__/return-types/some.ts
|
|
26
|
+
var Some = class _Some {
|
|
27
|
+
/**
|
|
28
|
+
* Creates a new `Some` option with the given value.
|
|
29
|
+
* @param value The value to be wrapped in the `Some` option.
|
|
30
|
+
*/
|
|
31
|
+
constructor(value) {
|
|
32
|
+
this.value = value;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Checks if this option is a `Some`.
|
|
36
|
+
* @returns `true` if this option is a `Some`, otherwise `false`.
|
|
37
|
+
*/
|
|
38
|
+
isSome() {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Checks if this option is a `None`.
|
|
43
|
+
* @returns `false` because this is a `Some`.
|
|
44
|
+
*/
|
|
45
|
+
isNone() {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Unwraps the value held by this `Some` option.
|
|
50
|
+
* @returns The value held by this `Some` option.
|
|
51
|
+
*/
|
|
52
|
+
unwrap() {
|
|
53
|
+
return this.value;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Applies a transformation function to the value held by this `Some` option and returns a new `Option` with the transformed value.
|
|
57
|
+
* @template U The type of the transformed value.
|
|
58
|
+
* @param fn The transformation function to apply to the value.
|
|
59
|
+
* @returns A new `Some` option containing the transformed value.
|
|
60
|
+
*/
|
|
61
|
+
map(fn) {
|
|
62
|
+
return new _Some(fn(this.value));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Applies a transformation function that returns an `Option` to the value held by this `Some` option.
|
|
66
|
+
* @template U The type of the value in the resulting `Option`.
|
|
67
|
+
* @param fn The transformation function to apply to the value.
|
|
68
|
+
* @returns The result of applying the transformation function.
|
|
69
|
+
*/
|
|
70
|
+
flatMap(fn) {
|
|
71
|
+
return fn(this.value);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Returns the value held by this `Some` option, ignoring the default value provided.
|
|
75
|
+
* @param _ A default value (ignored in this implementation).
|
|
76
|
+
* @returns The value held by this `Some` option.
|
|
77
|
+
*/
|
|
78
|
+
unwrapOr(_) {
|
|
79
|
+
return this.value;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// src/option/__internal__/return-types/none.ts
|
|
84
|
+
var None = class _None {
|
|
85
|
+
/**
|
|
86
|
+
* Checks if this option is a `Some`.
|
|
87
|
+
* @returns `false` because this is a `None`.
|
|
88
|
+
*/
|
|
89
|
+
isSome() {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Checks if this option is a `None`.
|
|
94
|
+
* @returns `true` because this is a `None`.
|
|
95
|
+
*/
|
|
96
|
+
isNone() {
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Attempts to unwrap the value from this `None` option.
|
|
101
|
+
* @throws An error because `None` has no value.
|
|
102
|
+
*/
|
|
103
|
+
unwrap() {
|
|
104
|
+
throw new Error("Called unwrap on a None value");
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Applies a transformation function to the value (which does not exist) of this `None` option.
|
|
108
|
+
* @template U The type of the value that would have been returned.
|
|
109
|
+
* @param _fn The transformation function (ignored in this implementation).
|
|
110
|
+
* @returns A new `None` option.
|
|
111
|
+
*/
|
|
112
|
+
map(_fn) {
|
|
113
|
+
return new _None();
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Applies a transformation function that returns an `Option` to the value (which does not exist) of this `None` option.
|
|
117
|
+
* @template U The type of the value in the resulting `Option`.
|
|
118
|
+
* @param _fn The transformation function (ignored in this implementation).
|
|
119
|
+
* @returns A new `None` option.
|
|
120
|
+
*/
|
|
121
|
+
flatMap(_fn) {
|
|
122
|
+
return new _None();
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Returns the default value provided, since `None` has no value.
|
|
126
|
+
* @template T The type of the default value.
|
|
127
|
+
* @param defaultValue The value to return.
|
|
128
|
+
* @returns The default value provided.
|
|
129
|
+
*/
|
|
130
|
+
unwrapOr(defaultValue) {
|
|
131
|
+
return defaultValue;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
// src/option/option.ts
|
|
136
|
+
function Some2(value) {
|
|
137
|
+
return new Some(value);
|
|
138
|
+
}
|
|
139
|
+
function None2() {
|
|
140
|
+
return new None();
|
|
141
|
+
}
|
|
142
|
+
global.Some = Some2;
|
|
143
|
+
global.None = None2;
|
|
144
|
+
|
|
145
|
+
// src/result/__internal__/return-types/err.ts
|
|
146
|
+
var Err = class _Err extends Error {
|
|
147
|
+
error;
|
|
148
|
+
/**
|
|
149
|
+
* Creates a new `Err` instance with the given error value.
|
|
150
|
+
* @param error The error to wrap in the `Err` instance.
|
|
151
|
+
*/
|
|
152
|
+
constructor(error) {
|
|
153
|
+
super(typeof error === "string" ? error : error.message);
|
|
154
|
+
this.error = typeof error === "string" ? new Error(error) : error;
|
|
155
|
+
Object.setPrototypeOf(this, _Err.prototype);
|
|
156
|
+
if (Error.captureStackTrace) {
|
|
157
|
+
Error.captureStackTrace(this, _Err);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Checks if this result is an `Ok`.
|
|
162
|
+
* @returns `false` because this is an `Err`.
|
|
163
|
+
*/
|
|
164
|
+
isOk() {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Checks if this result is an `Err`.
|
|
169
|
+
* @returns `true` because this is an `Err`.
|
|
170
|
+
*/
|
|
171
|
+
isErr() {
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.
|
|
176
|
+
* @throws An error because `unwrap` is called on an `Err`.
|
|
177
|
+
*/
|
|
178
|
+
unwrap() {
|
|
179
|
+
throw new Error("Called unwrap on an Err value");
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.
|
|
183
|
+
* @template U The type of the value (ignored for `Err`).
|
|
184
|
+
* @param _fn The mapping function for values (not used).
|
|
185
|
+
* @returns The original `Err` instance.
|
|
186
|
+
*/
|
|
187
|
+
map(_fn) {
|
|
188
|
+
return this;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Maps the error value using a transformation function and returns a new `Result` with the transformed error.
|
|
192
|
+
* @template U The type of the transformed error.
|
|
193
|
+
* @param fn The transformation function to apply to the error value.
|
|
194
|
+
* @returns A new `Err` containing the transformed error.
|
|
195
|
+
*/
|
|
196
|
+
mapErr(fn) {
|
|
197
|
+
return new _Err(fn(this.error));
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.
|
|
201
|
+
* @template U The type of the value in the resulting `Result`.
|
|
202
|
+
* @param _fn The transformation function (ignored in this implementation).
|
|
203
|
+
* @returns The original `Err` instance.
|
|
204
|
+
*/
|
|
205
|
+
flatMap(_fn) {
|
|
206
|
+
return this;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Retrieves the error value contained in this `Err`.
|
|
210
|
+
* @returns The error value contained in this `Err`.
|
|
211
|
+
*/
|
|
212
|
+
unwrapErr() {
|
|
213
|
+
return this.error;
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
// src/result/__internal__/return-types/ok.ts
|
|
218
|
+
var Ok = class _Ok {
|
|
219
|
+
/**
|
|
220
|
+
* Creates a new `Ok` instance with the given value.
|
|
221
|
+
* @param value The value to wrap in the `Ok` instance.
|
|
222
|
+
*/
|
|
223
|
+
constructor(value) {
|
|
224
|
+
this.value = value;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Checks if this result is an `Ok`.
|
|
228
|
+
* @returns `true` because this is an `Ok`.
|
|
229
|
+
*/
|
|
230
|
+
isOk() {
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Checks if this result is an `Err`.
|
|
235
|
+
* @returns `false` because this is an `Ok`.
|
|
236
|
+
*/
|
|
237
|
+
isErr() {
|
|
238
|
+
return false;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Retrieves the value contained in this `Ok`.
|
|
242
|
+
* @returns The value contained in this `Ok`.
|
|
243
|
+
*/
|
|
244
|
+
unwrap() {
|
|
245
|
+
return this.value;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.
|
|
249
|
+
* @template U The type of the transformed value.
|
|
250
|
+
* @param fn The transformation function to apply to the value.
|
|
251
|
+
* @returns A new `Ok` containing the transformed value.
|
|
252
|
+
*/
|
|
253
|
+
map(fn) {
|
|
254
|
+
return new _Ok(fn(this.value));
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Applies a transformation function that returns a `Result` to the value contained in this `Ok`.
|
|
258
|
+
* @template U The type of the value in the resulting `Result`.
|
|
259
|
+
* @param fn The transformation function to apply to the value.
|
|
260
|
+
* @returns The result of applying the transformation function.
|
|
261
|
+
*/
|
|
262
|
+
flatMap(fn) {
|
|
263
|
+
return fn(this.value);
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.
|
|
267
|
+
* @template U The type of the error (ignored for `Ok`).
|
|
268
|
+
* @param _fn The mapping function for errors (not used).
|
|
269
|
+
* @returns The original `Ok` instance.
|
|
270
|
+
*/
|
|
271
|
+
// mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {
|
|
272
|
+
// return this;
|
|
273
|
+
mapErr(_fn) {
|
|
274
|
+
return this;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.
|
|
278
|
+
* @throws An error because `unwrapErr` is called on an `Ok`.
|
|
279
|
+
*/
|
|
280
|
+
unwrapErr() {
|
|
281
|
+
throw new Error("Called unwrapErr on an Ok value");
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
// src/result/result.ts
|
|
286
|
+
function Ok2(value) {
|
|
287
|
+
return new Ok(value);
|
|
288
|
+
}
|
|
289
|
+
function Err2(error) {
|
|
290
|
+
return new Err(error);
|
|
291
|
+
}
|
|
292
|
+
global.Ok = Ok2;
|
|
293
|
+
global.Err = Err2;
|
|
294
|
+
|
|
295
|
+
exports.Err = Err2;
|
|
296
|
+
exports.None = None2;
|
|
297
|
+
exports.Ok = Ok2;
|
|
298
|
+
exports.Some = Some2;
|
|
299
|
+
exports.match = match;
|
|
300
|
+
//# sourceMappingURL=index.cjs.map
|
|
301
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/helpers/match.ts","../src/option/__internal__/return-types/some.ts","../src/option/__internal__/return-types/none.ts","../src/option/option.ts","../src/result/__internal__/return-types/err.ts","../src/result/__internal__/return-types/ok.ts","../src/result/result.ts"],"names":["Some","None","Ok","Err"],"mappings":";;;AAiBO,SAAS,KAAA,CACf,SACA,KAMI,EAAA;AACJ,EAAA,IAAI,MAAU,IAAA,OAAA,IAAW,OAAQ,CAAA,IAAA,EAAQ,EAAA;AACxC,IAAA,IAAI,CAAC,KAAM,CAAA,EAAA,EAAU,MAAA,IAAI,MAAM,qBAAqB,CAAA;AACpD,IAAA,OAAO,KAAM,CAAA,EAAA,CAAG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AAAA;AAAA;AAAA;AAAA;AAAA,EAKxD,YAAoB,KAAU,EAAA;AAAV,IAAA,IAAA,CAAA,KAAA,GAAA,KAAA;AAAA;AAAW;AAAA;AAAA;AAAA;AAAA,EAK/B,IAAsB,GAAA;AACrB,IAAO,OAAA,IAAA;AAAA;AACR;AAAA;AAAA;AAAA;AAAA,EAMA,KAA4B,GAAA;AAC3B,IAAO,OAAA,KAAA;AAAA;AACR;AAAA;AAAA;AAAA;AAAA,EAMA,MAAY,GAAA;AACX,IAAA,OAAO,IAAK,CAAA,KAAA;AAAA;AACb;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAO,EAAiD,EAAA;AACvD,IAAA,OAAO,IAAI,GAAA,CAAG,EAAG,CAAA,IAAA,CAAK,KAAK,CAAC,CAAA;AAAA;AAC7B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,QACC,EAC6B,EAAA;AAC7B,IAAO,OAAA,EAAA,CAAG,KAAK,KAAK,CAAA;AAAA;AACrB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,OAAwB,GAAgD,EAAA;AACvE,IAAO,OAAA,IAAA;AAAA;AACR;AAAA;AAAA;AAAA;AAAA,EAMA,SAAmB,GAAA;AAClB,IAAM,MAAA,IAAI,MAAM,iCAAiC,CAAA;AAAA;AAEnD,CAAA;;;AC9DA,SAASC,IAAM,KAAqB,EAAA;AACnC,EAAO,OAAA,IAAI,GAAO,KAAK,CAAA;AACxB;AAYA,SAASC,KAAqB,KAA+B,EAAA;AAC5D,EAAO,OAAA,IAAI,IAAQ,KAAK,CAAA;AACzB;AAEC,MAAA,CAAe,EAAKD,GAAAA,GAAAA;AACpB,MAAA,CAAe,GAAMC,GAAAA,IAAAA","file":"index.cjs","sourcesContent":["import { Result } from \"../result\";\nimport { Option } from \"../option\";\n\nexport function match<T, E extends Error, R>(\n\tmatcher: Result<T, E>,\n\tcases: {\n\t\tOk: (value: T) => R;\n\t\tErr: (error: E) => R;\n\t},\n): R;\nexport function match<T, R>(\n\tmatcher: Option<T>,\n\tcases: {\n\t\tSome: (value: T) => R;\n\t\tNone: () => R;\n\t},\n): R;\nexport function match<T, E extends Error, R>(\n\tmatcher: Result<T, E> | Option<T>,\n\tcases: {\n\t\tOk?: (value: T) => R;\n\t\tErr?: (error: E) => R;\n\t\tSome?: (value: T) => R;\n\t\tNone?: () => R;\n\t},\n): R {\n\tif (\"isOk\" in matcher && matcher.isOk()) {\n\t\tif (!cases.Ok) throw new Error(\"Missing case for Ok\");\n\t\treturn cases.Ok(matcher.unwrap());\n\t}\n\n\tif (\"isErr\" in matcher && matcher.isErr()) {\n\t\tif (!cases.Err) throw new Error(\"Missing case for Err\");\n\t\treturn cases.Err(matcher.unwrapErr() as E);\n\t}\n\n\tif (\"isSome\" in matcher && matcher.isSome()) {\n\t\tif (!cases.Some) throw new Error(\"Missing case for Some\");\n\t\treturn cases.Some(matcher.unwrap());\n\t}\n\n\tif (\"isNone\" in matcher && matcher.isNone()) {\n\t\tif (!cases.None) throw new Error(\"Missing case for None\");\n\t\treturn cases.None();\n\t}\n\n\tthrow new Error(\"Invalid matcher or missing case\");\n}\n\n(global as any).match = match;\n","import { None } from \"./none\";\nimport { Option } from \"./option\";\n\n/**\n * Represents a `Some` option, which holds a value.\n * @template T The type of the value held by this `Some` instance.\n */\nexport class Some<T> {\n\t/**\n\t * Creates a new `Some` option with the given value.\n\t * @param value The value to be wrapped in the `Some` option.\n\t */\n\tconstructor(private value: T) {}\n\n\t/**\n\t * Checks if this option is a `Some`.\n\t * @returns `true` if this option is a `Some`, otherwise `false`.\n\t */\n\tisSome(): this is Some<T> {\n\t\treturn true;\n\t}\n\n\t/**\n\t * Checks if this option is a `None`.\n\t * @returns `false` because this is a `Some`.\n\t */\n\tisNone(): this is None {\n\t\treturn false;\n\t}\n\n\t/**\n\t * Unwraps the value held by this `Some` option.\n\t * @returns The value held by this `Some` option.\n\t */\n\tunwrap(): T {\n\t\treturn this.value;\n\t}\n\n\t/**\n\t * Applies a transformation function to the value held by this `Some` option and returns a new `Option` with the transformed value.\n\t * @template U The type of the transformed value.\n\t * @param fn The transformation function to apply to the value.\n\t * @returns A new `Some` option containing the transformed value.\n\t */\n\tmap<U>(fn: (value: T) => U): Option<U> {\n\t\treturn new Some(fn(this.value));\n\t}\n\n\t/**\n\t * Applies a transformation function that returns an `Option` to the value held by this `Some` option.\n\t * @template U The type of the value in the resulting `Option`.\n\t * @param fn The transformation function to apply to the value.\n\t * @returns The result of applying the transformation function.\n\t */\n\tflatMap<U>(fn: (value: T) => Option<U>): Option<U> {\n\t\treturn fn(this.value);\n\t}\n\n\t/**\n\t * Returns the value held by this `Some` option, ignoring the default value provided.\n\t * @param _ A default value (ignored in this implementation).\n\t * @returns The value held by this `Some` option.\n\t */\n\tunwrapOr(_: T): T {\n\t\treturn this.value;\n\t}\n}\n","import { Some } from \"./some\";\nimport { Option } from \"./option\";\n\n/**\n * Represents a `None` option, which holds no value.\n */\nexport class None {\n\t/**\n\t * Checks if this option is a `Some`.\n\t * @returns `false` because this is a `None`.\n\t */\n\tisSome(): this is Some<never> {\n\t\treturn false;\n\t}\n\n\t/**\n\t * Checks if this option is a `None`.\n\t * @returns `true` because this is a `None`.\n\t */\n\tisNone(): this is None {\n\t\treturn true;\n\t}\n\n\t/**\n\t * Attempts to unwrap the value from this `None` option.\n\t * @throws An error because `None` has no value.\n\t */\n\tunwrap(): never {\n\t\tthrow new Error(\"Called unwrap on a None value\");\n\t}\n\n\t/**\n\t * Applies a transformation function to the value (which does not exist) of this `None` option.\n\t * @template U The type of the value that would have been returned.\n\t * @param _fn The transformation function (ignored in this implementation).\n\t * @returns A new `None` option.\n\t */\n\tmap<U>(_fn: (value: never) => U): Option<U> {\n\t\treturn new None();\n\t}\n\n\t/**\n\t * Applies a transformation function that returns an `Option` to the value (which does not exist) of this `None` option.\n\t * @template U The type of the value in the resulting `Option`.\n\t * @param _fn The transformation function (ignored in this implementation).\n\t * @returns A new `None` option.\n\t */\n\tflatMap<U>(_fn: (value: never) => Option<U>): Option<U> {\n\t\treturn new None();\n\t}\n\n\t/**\n\t * Returns the default value provided, since `None` has no value.\n\t * @template T The type of the default value.\n\t * @param defaultValue The value to return.\n\t * @returns The default value provided.\n\t */\n\tunwrapOr<T>(defaultValue: T): T {\n\t\treturn defaultValue;\n\t}\n}\n","import {\n\tSome as SomeType,\n\tNone as NoneType,\n\ttype Option,\n} from \"./__internal__/return-types\";\n\n/**\n * Creates a new `Some` instance, representing an `Option` with a value.\n * @template T The type of the value contained in the `Some`.\n * @param value The value to wrap in the `Some` instance.\n * @returns A `Some` instance containing the given value.\n * @example\n * const option = Some(42);\n * console.log(option.isSome()); // true\n * console.log(option.unwrap()); // 42\n */\nfunction Some<T>(value: T): SomeType<T> {\n\treturn new SomeType(value);\n}\n\n/**\n * Creates a new `None` instance, representing an `Option` with no value.\n * @returns A `None` instance.\n * @example\n * const option = None();\n * console.log(option.isNone()); // true\n * console.log(option.unwrap()); // throws Error: \"Called unwrap on a None value\"\n */\nfunction None(): NoneType {\n\treturn new NoneType();\n}\n\n(global as any).Some = Some;\n(global as any).None = None;\n\nexport { None, Some, Option };\n","import type { ResultDefinition } from \"./result\";\nimport { 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\textends Error\n\timplements ResultDefinition<never, E>\n{\n\tprivate error: E;\n\t/**\n\t * Creates a new `Err` instance with the given error value.\n\t * @param error The error to wrap in the `Err` instance.\n\t */\n\tconstructor(error: E | string) {\n\t\tsuper(typeof error === \"string\" ? error : error.message);\n\t\tthis.error =\n\t\t\ttypeof error === \"string\" ? (new Error(error) as E) : (error as E);\n\t\tObject.setPrototypeOf(this, Err.prototype);\n\n\t\tif (Error.captureStackTrace) {\n\t\t\tError.captureStackTrace(this, Err);\n\t\t}\n\t}\n\n\t/**\n\t * Checks if this result is an `Ok`.\n\t * @returns `false` because this is an `Err`.\n\t */\n\tisOk(): this is Ok<never> {\n\t\treturn false;\n\t}\n\n\t/**\n\t * Checks if this result is an `Err`.\n\t * @returns `true` because this is an `Err`.\n\t */\n\tisErr(): this is Err<E extends Error ? E : Error> {\n\t\treturn true;\n\t}\n\n\t/**\n\t * Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.\n\t * @throws An error because `unwrap` is called on an `Err`.\n\t */\n\tunwrap(): never {\n\t\tthrow new Error(\"Called unwrap on an Err value\");\n\t}\n\n\t/**\n\t * Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.\n\t * @template U The type of the value (ignored for `Err`).\n\t * @param _fn The mapping function for values (not used).\n\t * @returns The original `Err` instance.\n\t */\n\tmap<U>(_fn: (value: never) => U): ResultDefinition<never, E> {\n\t\treturn this as unknown as ResultDefinition<never, E>;\n\t}\n\n\t/**\n\t * Maps the error value using a transformation function and returns a new `Result` with the transformed error.\n\t * @template U The type of the transformed error.\n\t * @param fn The transformation function to apply to the error value.\n\t * @returns A new `Err` containing the transformed error.\n\t */\n\tmapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<never, U> {\n\t\treturn new Err<U>(fn(this.error)) as unknown as ResultDefinition<never, U>;\n\t}\n\n\t/**\n\t * Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.\n\t * @template U The type of the value in the resulting `Result`.\n\t * @param _fn The transformation function (ignored in this implementation).\n\t * @returns The original `Err` instance.\n\t */\n\tflatMap<U>(\n\t\t_fn: (value: never) => ResultDefinition<U, never>,\n\t): ResultDefinition<never, E> {\n\t\treturn this as unknown as ResultDefinition<never, E>;\n\t}\n\n\t/**\n\t * Retrieves the error value contained in this `Err`.\n\t * @returns The error value contained in this `Err`.\n\t */\n\tunwrapErr(): E {\n\t\treturn this.error;\n\t}\n}\n","import type { ResultDefinition } from \"./result\";\nimport { 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\t/**\n\t * Creates a new `Ok` instance with the given value.\n\t * @param value The value to wrap in the `Ok` instance.\n\t */\n\tconstructor(private value: T) {}\n\t/**\n\t * Checks if this result is an `Ok`.\n\t * @returns `true` because this is an `Ok`.\n\t */\n\tisOk(): this is Ok<T> {\n\t\treturn true;\n\t}\n\n\t/**\n\t * Checks if this result is an `Err`.\n\t * @returns `false` because this is an `Ok`.\n\t */\n\tisErr(): this is Err<never> {\n\t\treturn false;\n\t}\n\n\t/**\n\t * Retrieves the value contained in this `Ok`.\n\t * @returns The value contained in this `Ok`.\n\t */\n\tunwrap(): T {\n\t\treturn this.value;\n\t}\n\n\t/**\n\t * Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.\n\t * @template U The type of the transformed value.\n\t * @param fn The transformation function to apply to the value.\n\t * @returns A new `Ok` containing the transformed value.\n\t */\n\tmap<U>(fn: (value: T) => U): ResultDefinition<U, never> {\n\t\treturn new Ok(fn(this.value)) as ResultDefinition<U, never>;\n\t}\n\n\t/**\n\t * Applies a transformation function that returns a `Result` to the value contained in this `Ok`.\n\t * @template U The type of the value in the resulting `Result`.\n\t * @param fn The transformation function to apply to the value.\n\t * @returns The result of applying the transformation function.\n\t */\n\tflatMap<U>(\n\t\tfn: (value: T) => ResultDefinition<U, never>,\n\t): ResultDefinition<U, never> {\n\t\treturn fn(this.value);\n\t}\n\n\t/**\n\t * Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.\n\t * @template U The type of the error (ignored for `Ok`).\n\t * @param _fn The mapping function for errors (not used).\n\t * @returns The original `Ok` instance.\n\t */\n\t// mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {\n\t// \treturn this;\n\tmapErr<U extends Error>(_fn: (err: never) => U): ResultDefinition<T, U> {\n\t\treturn this as unknown as ResultDefinition<T, U>;\n\t}\n\n\t/**\n\t * Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.\n\t * @throws An error because `unwrapErr` is called on an `Ok`.\n\t */\n\tunwrapErr(): never {\n\t\tthrow new Error(\"Called unwrapErr on an Ok value\");\n\t}\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"]}
|
package/dist/index.d.cts
ADDED
package/dist/index.d.ts
ADDED