@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 @@
|
|
|
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,SACJ,KAET,CAAA;AAAA,EACS,KAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAKR,YAAY,KAAmB,EAAA;AAC9B,IAAA,KAAA,CAAM,OAAO,KAAA,KAAU,QAAW,GAAA,KAAA,GAAQ,MAAM,OAAO,CAAA;AACvD,IAAA,IAAA,CAAK,QACJ,OAAO,KAAA,KAAU,WAAY,IAAI,KAAA,CAAM,KAAK,CAAW,GAAA,KAAA;AACxD,IAAO,MAAA,CAAA,cAAA,CAAe,IAAM,EAAA,IAAA,CAAI,SAAS,CAAA;AAEzC,IAAA,IAAI,MAAM,iBAAmB,EAAA;AAC5B,MAAM,KAAA,CAAA,iBAAA,CAAkB,MAAM,IAAG,CAAA;AAAA;AAClC;AACD;AAAA;AAAA;AAAA;AAAA,EAMA,IAA0B,GAAA;AACzB,IAAO,OAAA,KAAA;AAAA;AACR;AAAA;AAAA;AAAA;AAAA,EAMA,KAAkD,GAAA;AACjD,IAAO,OAAA,IAAA;AAAA;AACR;AAAA;AAAA;AAAA;AAAA,EAMA,MAAgB,GAAA;AACf,IAAM,MAAA,IAAI,MAAM,+BAA+B,CAAA;AAAA;AAChD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAO,GAAsD,EAAA;AAC5D,IAAO,OAAA,IAAA;AAAA;AACR;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAwB,EAA+C,EAAA;AACtE,IAAA,OAAO,IAAI,IAAA,CAAO,EAAG,CAAA,IAAA,CAAK,KAAK,CAAC,CAAA;AAAA;AACjC;AAAA;AAAA;AAAA;AAAA;AAAA;A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,CAAe,EAAKD,GAAAA,GAAAA;AACpB,MAAA,CAAe,GAAMC,GAAAA,IAAAA","file":"index.cjs","sourcesContent":["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"]}
|
|
@@ -0,0 +1,153 @@
|
|
|
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
|
+
|
|
73
|
+
// src/result/__internal__/return-types/ok.ts
|
|
74
|
+
var Ok = class _Ok {
|
|
75
|
+
/**
|
|
76
|
+
* Creates a new `Ok` instance with the given value.
|
|
77
|
+
* @param value The value to wrap in the `Ok` instance.
|
|
78
|
+
*/
|
|
79
|
+
constructor(value) {
|
|
80
|
+
this.value = value;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Checks if this result is an `Ok`.
|
|
84
|
+
* @returns `true` because this is an `Ok`.
|
|
85
|
+
*/
|
|
86
|
+
isOk() {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Checks if this result is an `Err`.
|
|
91
|
+
* @returns `false` because this is an `Ok`.
|
|
92
|
+
*/
|
|
93
|
+
isErr() {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Retrieves the value contained in this `Ok`.
|
|
98
|
+
* @returns The value contained in this `Ok`.
|
|
99
|
+
*/
|
|
100
|
+
unwrap() {
|
|
101
|
+
return this.value;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.
|
|
105
|
+
* @template U The type of the transformed value.
|
|
106
|
+
* @param fn The transformation function to apply to the value.
|
|
107
|
+
* @returns A new `Ok` containing the transformed value.
|
|
108
|
+
*/
|
|
109
|
+
map(fn) {
|
|
110
|
+
return new _Ok(fn(this.value));
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Applies a transformation function that returns a `Result` to the value contained in this `Ok`.
|
|
114
|
+
* @template U The type of the value in the resulting `Result`.
|
|
115
|
+
* @param fn The transformation function to apply to the value.
|
|
116
|
+
* @returns The result of applying the transformation function.
|
|
117
|
+
*/
|
|
118
|
+
flatMap(fn) {
|
|
119
|
+
return fn(this.value);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.
|
|
123
|
+
* @template U The type of the error (ignored for `Ok`).
|
|
124
|
+
* @param _fn The mapping function for errors (not used).
|
|
125
|
+
* @returns The original `Ok` instance.
|
|
126
|
+
*/
|
|
127
|
+
// mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {
|
|
128
|
+
// return this;
|
|
129
|
+
mapErr(_fn) {
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.
|
|
134
|
+
* @throws An error because `unwrapErr` is called on an `Ok`.
|
|
135
|
+
*/
|
|
136
|
+
unwrapErr() {
|
|
137
|
+
throw new Error("Called unwrapErr on an Ok value");
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// src/result/result.ts
|
|
142
|
+
function Ok2(value) {
|
|
143
|
+
return new Ok(value);
|
|
144
|
+
}
|
|
145
|
+
function Err2(error) {
|
|
146
|
+
return new Err(error);
|
|
147
|
+
}
|
|
148
|
+
global.Ok = Ok2;
|
|
149
|
+
global.Err = Err2;
|
|
150
|
+
|
|
151
|
+
export { Err2 as Err, Ok2 as Ok };
|
|
152
|
+
//# sourceMappingURL=index.js.map
|
|
153
|
+
//# 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,SACJ,KAET,CAAA;AAAA,EACS,KAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAKR,YAAY,KAAmB,EAAA;AAC9B,IAAA,KAAA,CAAM,OAAO,KAAA,KAAU,QAAW,GAAA,KAAA,GAAQ,MAAM,OAAO,CAAA;AACvD,IAAA,IAAA,CAAK,QACJ,OAAO,KAAA,KAAU,WAAY,IAAI,KAAA,CAAM,KAAK,CAAW,GAAA,KAAA;AACxD,IAAO,MAAA,CAAA,cAAA,CAAe,IAAM,EAAA,IAAA,CAAI,SAAS,CAAA;AAEzC,IAAA,IAAI,MAAM,iBAAmB,EAAA;AAC5B,MAAM,KAAA,CAAA,iBAAA,CAAkB,MAAM,IAAG,CAAA;AAAA;AAClC;AACD;AAAA;AAAA;AAAA;AAAA,EAMA,IAA0B,GAAA;AACzB,IAAO,OAAA,KAAA;AAAA;AACR;AAAA;AAAA;AAAA;AAAA,EAMA,KAAkD,GAAA;AACjD,IAAO,OAAA,IAAA;AAAA;AACR;AAAA;AAAA;AAAA;AAAA,EAMA,MAAgB,GAAA;AACf,IAAM,MAAA,IAAI,MAAM,+BAA+B,CAAA;AAAA;AAChD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IAAO,GAAsD,EAAA;AAC5D,IAAO,OAAA,IAAA;AAAA;AACR;AAAA;AAAA;AAAA;AA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A,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.js","sourcesContent":["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"]}
|
|
@@ -0,0 +1,158 @@
|
|
|
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
|
+
|
|
75
|
+
// src/result/__internal__/return-types/ok.ts
|
|
76
|
+
var Ok = class _Ok {
|
|
77
|
+
/**
|
|
78
|
+
* Creates a new `Ok` instance with the given value.
|
|
79
|
+
* @param value The value to wrap in the `Ok` instance.
|
|
80
|
+
*/
|
|
81
|
+
constructor(value) {
|
|
82
|
+
this.value = value;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Checks if this result is an `Ok`.
|
|
86
|
+
* @returns `true` because this is an `Ok`.
|
|
87
|
+
*/
|
|
88
|
+
isOk() {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Checks if this result is an `Err`.
|
|
93
|
+
* @returns `false` because this is an `Ok`.
|
|
94
|
+
*/
|
|
95
|
+
isErr() {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Retrieves the value contained in this `Ok`.
|
|
100
|
+
* @returns The value contained in this `Ok`.
|
|
101
|
+
*/
|
|
102
|
+
unwrap() {
|
|
103
|
+
return this.value;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.
|
|
107
|
+
* @template U The type of the transformed value.
|
|
108
|
+
* @param fn The transformation function to apply to the value.
|
|
109
|
+
* @returns A new `Ok` containing the transformed value.
|
|
110
|
+
*/
|
|
111
|
+
map(fn) {
|
|
112
|
+
return new _Ok(fn(this.value));
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Applies a transformation function that returns a `Result` to the value contained in this `Ok`.
|
|
116
|
+
* @template U The type of the value in the resulting `Result`.
|
|
117
|
+
* @param fn The transformation function to apply to the value.
|
|
118
|
+
* @returns The result of applying the transformation function.
|
|
119
|
+
*/
|
|
120
|
+
flatMap(fn) {
|
|
121
|
+
return fn(this.value);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.
|
|
125
|
+
* @template U The type of the error (ignored for `Ok`).
|
|
126
|
+
* @param _fn The mapping function for errors (not used).
|
|
127
|
+
* @returns The original `Ok` instance.
|
|
128
|
+
*/
|
|
129
|
+
// mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {
|
|
130
|
+
// return this;
|
|
131
|
+
mapErr(_fn) {
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.
|
|
136
|
+
* @throws An error because `unwrapErr` is called on an `Ok`.
|
|
137
|
+
*/
|
|
138
|
+
unwrapErr() {
|
|
139
|
+
throw new Error("Called unwrapErr on an Ok value");
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// src/result/result.ts
|
|
144
|
+
function Ok2(value) {
|
|
145
|
+
return new Ok(value);
|
|
146
|
+
}
|
|
147
|
+
function Err2(error) {
|
|
148
|
+
return new Err(error);
|
|
149
|
+
}
|
|
150
|
+
global.Ok = Ok2;
|
|
151
|
+
global.Err = Err2;
|
|
152
|
+
|
|
153
|
+
exports.Err = Err2;
|
|
154
|
+
exports.ErrType = Err;
|
|
155
|
+
exports.Ok = Ok2;
|
|
156
|
+
exports.OkType = Ok;
|
|
157
|
+
//# sourceMappingURL=result.cjs.map
|
|
158
|
+
//# 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":";;;AAOa,IAAA,GAAA,GAAN,MAAM,IAAA,SACJ,KAET,CAAA;AAAA,EACS,KAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAKR,YAAY,KAAmB,EAAA;AAC9B,IAAA,KAAA,CAAM,OAAO,KAAA,KAAU,QAAW,GAAA,KAAA,GAAQ,MAAM,OAAO,CAAA;AACvD,IAAA,IAAA,CAAK,QACJ,OAAO,KAAA,KAAU,WAAY,IAAI,KAAA,CAAM,KAAK,CAAW,GAAA,KAAA;AACxD,IAAO,MAAA,CAAA,cAAA,CAAe,IAAM,EAAA,IAAA,CAAI,SAAS,CAAA;AAEzC,IAAA,IAAI,MAAM,iBAAmB,EAAA;AAC5B,MAAM,KAAA,CAAA,iBAAA,CAAkB,MAAM,IAAG,CAAA;AAAA;AAClC;AACD;AAAA;AAAA;AAAA;AAAA,EAMA,IAA0B,GAAA;AACzB,IAAO,OAAA,KAAA;AAAA;AACR;AAAA;AAAA;AAAA;AAAA,EA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G,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;;;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 { 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"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { E as Err$1, O as Ok$1 } from '../err-B7LUEZ0f.cjs';
|
|
2
|
+
export { R as Result } from '../err-B7LUEZ0f.cjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a new `Ok` instance, representing a successful result.
|
|
6
|
+
* @template T The type of the value contained in the `Ok`.
|
|
7
|
+
* @param value The value to wrap in the `Ok` instance.
|
|
8
|
+
* @returns An `Ok` instance containing the given value.
|
|
9
|
+
* @example
|
|
10
|
+
* const result = Ok(42);
|
|
11
|
+
* console.log(result.isOk()); // true
|
|
12
|
+
* console.log(result.unwrap()); // 42
|
|
13
|
+
*/
|
|
14
|
+
declare function Ok<T>(value: T): Ok$1<T>;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new `Err` instance, representing a failed result.
|
|
17
|
+
* @template E The type of the error contained in the `Err`.
|
|
18
|
+
* @param error The error to wrap in the `Err` instance.
|
|
19
|
+
* @returns An `Err` instance containing the given error.
|
|
20
|
+
* @example
|
|
21
|
+
* const result = Err("Something went wrong");
|
|
22
|
+
* console.log(result.isErr()); // true
|
|
23
|
+
* console.log(result.unwrapErr()); // "Something went wrong"
|
|
24
|
+
*/
|
|
25
|
+
declare function Err<E extends Error>(error: E | string): Err$1<E>;
|
|
26
|
+
|
|
27
|
+
export { Err, Err$1 as ErrType, Ok, Ok$1 as OkType };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { E as Err$1, O as Ok$1 } from '../err-B7LUEZ0f.js';
|
|
2
|
+
export { R as Result } from '../err-B7LUEZ0f.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a new `Ok` instance, representing a successful result.
|
|
6
|
+
* @template T The type of the value contained in the `Ok`.
|
|
7
|
+
* @param value The value to wrap in the `Ok` instance.
|
|
8
|
+
* @returns An `Ok` instance containing the given value.
|
|
9
|
+
* @example
|
|
10
|
+
* const result = Ok(42);
|
|
11
|
+
* console.log(result.isOk()); // true
|
|
12
|
+
* console.log(result.unwrap()); // 42
|
|
13
|
+
*/
|
|
14
|
+
declare function Ok<T>(value: T): Ok$1<T>;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new `Err` instance, representing a failed result.
|
|
17
|
+
* @template E The type of the error contained in the `Err`.
|
|
18
|
+
* @param error The error to wrap in the `Err` instance.
|
|
19
|
+
* @returns An `Err` instance containing the given error.
|
|
20
|
+
* @example
|
|
21
|
+
* const result = Err("Something went wrong");
|
|
22
|
+
* console.log(result.isErr()); // true
|
|
23
|
+
* console.log(result.unwrapErr()); // "Something went wrong"
|
|
24
|
+
*/
|
|
25
|
+
declare function Err<E extends Error>(error: E | string): Err$1<E>;
|
|
26
|
+
|
|
27
|
+
export { Err, Err$1 as ErrType, Ok, Ok$1 as OkType };
|
|
@@ -0,0 +1,153 @@
|
|
|
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
|
+
|
|
73
|
+
// src/result/__internal__/return-types/ok.ts
|
|
74
|
+
var Ok = class _Ok {
|
|
75
|
+
/**
|
|
76
|
+
* Creates a new `Ok` instance with the given value.
|
|
77
|
+
* @param value The value to wrap in the `Ok` instance.
|
|
78
|
+
*/
|
|
79
|
+
constructor(value) {
|
|
80
|
+
this.value = value;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Checks if this result is an `Ok`.
|
|
84
|
+
* @returns `true` because this is an `Ok`.
|
|
85
|
+
*/
|
|
86
|
+
isOk() {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Checks if this result is an `Err`.
|
|
91
|
+
* @returns `false` because this is an `Ok`.
|
|
92
|
+
*/
|
|
93
|
+
isErr() {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Retrieves the value contained in this `Ok`.
|
|
98
|
+
* @returns The value contained in this `Ok`.
|
|
99
|
+
*/
|
|
100
|
+
unwrap() {
|
|
101
|
+
return this.value;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.
|
|
105
|
+
* @template U The type of the transformed value.
|
|
106
|
+
* @param fn The transformation function to apply to the value.
|
|
107
|
+
* @returns A new `Ok` containing the transformed value.
|
|
108
|
+
*/
|
|
109
|
+
map(fn) {
|
|
110
|
+
return new _Ok(fn(this.value));
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Applies a transformation function that returns a `Result` to the value contained in this `Ok`.
|
|
114
|
+
* @template U The type of the value in the resulting `Result`.
|
|
115
|
+
* @param fn The transformation function to apply to the value.
|
|
116
|
+
* @returns The result of applying the transformation function.
|
|
117
|
+
*/
|
|
118
|
+
flatMap(fn) {
|
|
119
|
+
return fn(this.value);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.
|
|
123
|
+
* @template U The type of the error (ignored for `Ok`).
|
|
124
|
+
* @param _fn The mapping function for errors (not used).
|
|
125
|
+
* @returns The original `Ok` instance.
|
|
126
|
+
*/
|
|
127
|
+
// mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {
|
|
128
|
+
// return this;
|
|
129
|
+
mapErr(_fn) {
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.
|
|
134
|
+
* @throws An error because `unwrapErr` is called on an `Ok`.
|
|
135
|
+
*/
|
|
136
|
+
unwrapErr() {
|
|
137
|
+
throw new Error("Called unwrapErr on an Ok value");
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
// src/result/result.ts
|
|
142
|
+
function Ok2(value) {
|
|
143
|
+
return new Ok(value);
|
|
144
|
+
}
|
|
145
|
+
function Err2(error) {
|
|
146
|
+
return new Err(error);
|
|
147
|
+
}
|
|
148
|
+
global.Ok = Ok2;
|
|
149
|
+
global.Err = Err2;
|
|
150
|
+
|
|
151
|
+
export { Err2 as Err, Err as ErrType, Ok2 as Ok, Ok as OkType };
|
|
152
|
+
//# sourceMappingURL=result.js.map
|
|
153
|
+
//# sourceMappingURL=result.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/result/__internal__/return-types/err.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KAAA;AAAA;AAEd;;;ACnFa,IAAA,EAAA,GAAN,MAAM,GAA4C,CAAA;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;;;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 { 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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","file":"globals.cjs"}
|