@consolidados/results 0.1.2 → 0.1.3
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/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/package.json +2 -2
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a successful result (`Ok`) that contains a value.
|
|
3
|
+
* @template T The type of the value contained in this `Ok`.
|
|
4
|
+
*/
|
|
5
|
+
declare class Ok<T> implements ResultDefinition<T, never> {
|
|
6
|
+
private value;
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new `Ok` instance with the given value.
|
|
9
|
+
* @param value The value to wrap in the `Ok` instance.
|
|
10
|
+
*/
|
|
11
|
+
constructor(value: T);
|
|
12
|
+
/**
|
|
13
|
+
* Checks if this result is an `Ok`.
|
|
14
|
+
* @returns `true` because this is an `Ok`.
|
|
15
|
+
*/
|
|
16
|
+
isOk(): this is Ok<T>;
|
|
17
|
+
/**
|
|
18
|
+
* Checks if this result is an `Err`.
|
|
19
|
+
* @returns `false` because this is an `Ok`.
|
|
20
|
+
*/
|
|
21
|
+
isErr(): this is Err<never>;
|
|
22
|
+
/**
|
|
23
|
+
* Retrieves the value contained in this `Ok`.
|
|
24
|
+
* @returns The value contained in this `Ok`.
|
|
25
|
+
*/
|
|
26
|
+
unwrap(): T;
|
|
27
|
+
/**
|
|
28
|
+
* Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.
|
|
29
|
+
* @template U The type of the transformed value.
|
|
30
|
+
* @param fn The transformation function to apply to the value.
|
|
31
|
+
* @returns A new `Ok` containing the transformed value.
|
|
32
|
+
*/
|
|
33
|
+
map<U>(fn: (value: T) => U): ResultDefinition<U, never>;
|
|
34
|
+
/**
|
|
35
|
+
* Applies a transformation function that returns a `Result` to the value contained in this `Ok`.
|
|
36
|
+
* @template U The type of the value in the resulting `Result`.
|
|
37
|
+
* @param fn The transformation function to apply to the value.
|
|
38
|
+
* @returns The result of applying the transformation function.
|
|
39
|
+
*/
|
|
40
|
+
flatMap<U>(fn: (value: T) => ResultDefinition<U, never>): ResultDefinition<U, never>;
|
|
41
|
+
/**
|
|
42
|
+
* Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.
|
|
43
|
+
* @template U The type of the error (ignored for `Ok`).
|
|
44
|
+
* @param _fn The mapping function for errors (not used).
|
|
45
|
+
* @returns The original `Ok` instance.
|
|
46
|
+
*/
|
|
47
|
+
mapErr<U extends Error>(_fn: (err: never) => U): ResultDefinition<T, U>;
|
|
48
|
+
/**
|
|
49
|
+
* Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.
|
|
50
|
+
* @throws An error because `unwrapErr` is called on an `Ok`.
|
|
51
|
+
*/
|
|
52
|
+
unwrapErr(): never;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
type OkType<T> = [T] extends [never] ? never : T;
|
|
56
|
+
type ErrType<E> = [E] extends [never] ? never : E;
|
|
57
|
+
/**
|
|
58
|
+
* Represents the result of an operation that can either succeed (`Ok`) or fail (`Err`).
|
|
59
|
+
*/
|
|
60
|
+
type Result<T, E extends Error> = Ok<OkType<T>> | Err<ErrType<E>>;
|
|
61
|
+
/**
|
|
62
|
+
* Represents the result methods that must be implemented for success (`Ok`) or failure (`Err`).
|
|
63
|
+
*/
|
|
64
|
+
interface ResultDefinition<T = never, E = never> {
|
|
65
|
+
isOk(): this is Ok<T>;
|
|
66
|
+
isErr(): this is Err<E extends Error ? E : Error>;
|
|
67
|
+
unwrap(): T;
|
|
68
|
+
unwrapErr(): E;
|
|
69
|
+
map<U>(fn: (value: T) => U): ResultDefinition<U, E>;
|
|
70
|
+
flatMap<U>(fn: (value: T) => ResultDefinition<U, E>): ResultDefinition<U, E>;
|
|
71
|
+
mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<T, U>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Represents a failed result (`Err`) that contains an error value.
|
|
76
|
+
* @template E The type of the error contained in this `Err`.
|
|
77
|
+
*/
|
|
78
|
+
declare class Err<E extends Error> extends Error implements ResultDefinition<never, E> {
|
|
79
|
+
private error;
|
|
80
|
+
/**
|
|
81
|
+
* Creates a new `Err` instance with the given error value.
|
|
82
|
+
* @param error The error to wrap in the `Err` instance.
|
|
83
|
+
*/
|
|
84
|
+
constructor(error: E | string);
|
|
85
|
+
/**
|
|
86
|
+
* Checks if this result is an `Ok`.
|
|
87
|
+
* @returns `false` because this is an `Err`.
|
|
88
|
+
*/
|
|
89
|
+
isOk(): this is Ok<never>;
|
|
90
|
+
/**
|
|
91
|
+
* Checks if this result is an `Err`.
|
|
92
|
+
* @returns `true` because this is an `Err`.
|
|
93
|
+
*/
|
|
94
|
+
isErr(): this is Err<E extends Error ? E : Error>;
|
|
95
|
+
/**
|
|
96
|
+
* Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.
|
|
97
|
+
* @throws An error because `unwrap` is called on an `Err`.
|
|
98
|
+
*/
|
|
99
|
+
unwrap(): never;
|
|
100
|
+
/**
|
|
101
|
+
* Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.
|
|
102
|
+
* @template U The type of the value (ignored for `Err`).
|
|
103
|
+
* @param _fn The mapping function for values (not used).
|
|
104
|
+
* @returns The original `Err` instance.
|
|
105
|
+
*/
|
|
106
|
+
map<U>(_fn: (value: never) => U): ResultDefinition<never, E>;
|
|
107
|
+
/**
|
|
108
|
+
* Maps the error value using a transformation function and returns a new `Result` with the transformed error.
|
|
109
|
+
* @template U The type of the transformed error.
|
|
110
|
+
* @param fn The transformation function to apply to the error value.
|
|
111
|
+
* @returns A new `Err` containing the transformed error.
|
|
112
|
+
*/
|
|
113
|
+
mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<never, U>;
|
|
114
|
+
/**
|
|
115
|
+
* Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.
|
|
116
|
+
* @template U The type of the value in the resulting `Result`.
|
|
117
|
+
* @param _fn The transformation function (ignored in this implementation).
|
|
118
|
+
* @returns The original `Err` instance.
|
|
119
|
+
*/
|
|
120
|
+
flatMap<U>(_fn: (value: never) => ResultDefinition<U, never>): ResultDefinition<never, E>;
|
|
121
|
+
/**
|
|
122
|
+
* Retrieves the error value contained in this `Err`.
|
|
123
|
+
* @returns The error value contained in this `Err`.
|
|
124
|
+
*/
|
|
125
|
+
unwrapErr(): E;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export { Err as E, Ok as O, type Result as R };
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a successful result (`Ok`) that contains a value.
|
|
3
|
+
* @template T The type of the value contained in this `Ok`.
|
|
4
|
+
*/
|
|
5
|
+
declare class Ok<T> implements ResultDefinition<T, never> {
|
|
6
|
+
private value;
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new `Ok` instance with the given value.
|
|
9
|
+
* @param value The value to wrap in the `Ok` instance.
|
|
10
|
+
*/
|
|
11
|
+
constructor(value: T);
|
|
12
|
+
/**
|
|
13
|
+
* Checks if this result is an `Ok`.
|
|
14
|
+
* @returns `true` because this is an `Ok`.
|
|
15
|
+
*/
|
|
16
|
+
isOk(): this is Ok<T>;
|
|
17
|
+
/**
|
|
18
|
+
* Checks if this result is an `Err`.
|
|
19
|
+
* @returns `false` because this is an `Ok`.
|
|
20
|
+
*/
|
|
21
|
+
isErr(): this is Err<never>;
|
|
22
|
+
/**
|
|
23
|
+
* Retrieves the value contained in this `Ok`.
|
|
24
|
+
* @returns The value contained in this `Ok`.
|
|
25
|
+
*/
|
|
26
|
+
unwrap(): T;
|
|
27
|
+
/**
|
|
28
|
+
* Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value.
|
|
29
|
+
* @template U The type of the transformed value.
|
|
30
|
+
* @param fn The transformation function to apply to the value.
|
|
31
|
+
* @returns A new `Ok` containing the transformed value.
|
|
32
|
+
*/
|
|
33
|
+
map<U>(fn: (value: T) => U): ResultDefinition<U, never>;
|
|
34
|
+
/**
|
|
35
|
+
* Applies a transformation function that returns a `Result` to the value contained in this `Ok`.
|
|
36
|
+
* @template U The type of the value in the resulting `Result`.
|
|
37
|
+
* @param fn The transformation function to apply to the value.
|
|
38
|
+
* @returns The result of applying the transformation function.
|
|
39
|
+
*/
|
|
40
|
+
flatMap<U>(fn: (value: T) => ResultDefinition<U, never>): ResultDefinition<U, never>;
|
|
41
|
+
/**
|
|
42
|
+
* Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned.
|
|
43
|
+
* @template U The type of the error (ignored for `Ok`).
|
|
44
|
+
* @param _fn The mapping function for errors (not used).
|
|
45
|
+
* @returns The original `Ok` instance.
|
|
46
|
+
*/
|
|
47
|
+
mapErr<U extends Error>(_fn: (err: never) => U): ResultDefinition<T, U>;
|
|
48
|
+
/**
|
|
49
|
+
* Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown.
|
|
50
|
+
* @throws An error because `unwrapErr` is called on an `Ok`.
|
|
51
|
+
*/
|
|
52
|
+
unwrapErr(): never;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
type OkType<T> = [T] extends [never] ? never : T;
|
|
56
|
+
type ErrType<E> = [E] extends [never] ? never : E;
|
|
57
|
+
/**
|
|
58
|
+
* Represents the result of an operation that can either succeed (`Ok`) or fail (`Err`).
|
|
59
|
+
*/
|
|
60
|
+
type Result<T, E extends Error> = Ok<OkType<T>> | Err<ErrType<E>>;
|
|
61
|
+
/**
|
|
62
|
+
* Represents the result methods that must be implemented for success (`Ok`) or failure (`Err`).
|
|
63
|
+
*/
|
|
64
|
+
interface ResultDefinition<T = never, E = never> {
|
|
65
|
+
isOk(): this is Ok<T>;
|
|
66
|
+
isErr(): this is Err<E extends Error ? E : Error>;
|
|
67
|
+
unwrap(): T;
|
|
68
|
+
unwrapErr(): E;
|
|
69
|
+
map<U>(fn: (value: T) => U): ResultDefinition<U, E>;
|
|
70
|
+
flatMap<U>(fn: (value: T) => ResultDefinition<U, E>): ResultDefinition<U, E>;
|
|
71
|
+
mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<T, U>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Represents a failed result (`Err`) that contains an error value.
|
|
76
|
+
* @template E The type of the error contained in this `Err`.
|
|
77
|
+
*/
|
|
78
|
+
declare class Err<E extends Error> extends Error implements ResultDefinition<never, E> {
|
|
79
|
+
private error;
|
|
80
|
+
/**
|
|
81
|
+
* Creates a new `Err` instance with the given error value.
|
|
82
|
+
* @param error The error to wrap in the `Err` instance.
|
|
83
|
+
*/
|
|
84
|
+
constructor(error: E | string);
|
|
85
|
+
/**
|
|
86
|
+
* Checks if this result is an `Ok`.
|
|
87
|
+
* @returns `false` because this is an `Err`.
|
|
88
|
+
*/
|
|
89
|
+
isOk(): this is Ok<never>;
|
|
90
|
+
/**
|
|
91
|
+
* Checks if this result is an `Err`.
|
|
92
|
+
* @returns `true` because this is an `Err`.
|
|
93
|
+
*/
|
|
94
|
+
isErr(): this is Err<E extends Error ? E : Error>;
|
|
95
|
+
/**
|
|
96
|
+
* Retrieves the value contained in this result. Since this is an `Err`, an error is thrown.
|
|
97
|
+
* @throws An error because `unwrap` is called on an `Err`.
|
|
98
|
+
*/
|
|
99
|
+
unwrap(): never;
|
|
100
|
+
/**
|
|
101
|
+
* Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned.
|
|
102
|
+
* @template U The type of the value (ignored for `Err`).
|
|
103
|
+
* @param _fn The mapping function for values (not used).
|
|
104
|
+
* @returns The original `Err` instance.
|
|
105
|
+
*/
|
|
106
|
+
map<U>(_fn: (value: never) => U): ResultDefinition<never, E>;
|
|
107
|
+
/**
|
|
108
|
+
* Maps the error value using a transformation function and returns a new `Result` with the transformed error.
|
|
109
|
+
* @template U The type of the transformed error.
|
|
110
|
+
* @param fn The transformation function to apply to the error value.
|
|
111
|
+
* @returns A new `Err` containing the transformed error.
|
|
112
|
+
*/
|
|
113
|
+
mapErr<U extends Error>(fn: (err: E) => U): ResultDefinition<never, U>;
|
|
114
|
+
/**
|
|
115
|
+
* Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`.
|
|
116
|
+
* @template U The type of the value in the resulting `Result`.
|
|
117
|
+
* @param _fn The transformation function (ignored in this implementation).
|
|
118
|
+
* @returns The original `Err` instance.
|
|
119
|
+
*/
|
|
120
|
+
flatMap<U>(_fn: (value: never) => ResultDefinition<U, never>): ResultDefinition<never, E>;
|
|
121
|
+
/**
|
|
122
|
+
* Retrieves the error value contained in this `Err`.
|
|
123
|
+
* @returns The error value contained in this `Err`.
|
|
124
|
+
*/
|
|
125
|
+
unwrapErr(): E;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export { Err as E, Ok as O, type Result as R };
|
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
exports.match = match;
|
|
26
|
+
//# sourceMappingURL=match.cjs.map
|
|
27
|
+
//# sourceMappingURL=match.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/helpers/match.ts"],"names":[],"mappings":";;;AAiBO,SAAS,KAAA,CACd,SACA,KAMG,EAAA;AACH,EAAA,IAAI,MAAU,IAAA,OAAA,IAAW,OAAQ,CAAA,IAAA,EAAQ,EAAA;AACvC,IAAA,IAAI,CAAC,KAAM,CAAA,EAAA,EAAU,MAAA,IAAI,MAAM,qBAAqB,CAAA;AACpD,IAAA,OAAO,KAAM,CAAA,EAAA,CAAG,OAAQ,CAAA,MAAA,EAAQ,CAAA;AAAA;AAGlC,EAAA,IAAI,OAAW,IAAA,OAAA,IAAW,OAAQ,CAAA,KAAA,EAAS,EAAA;AACzC,IAAA,IAAI,CAAC,KAAM,CAAA,GAAA,EAAW,MAAA,IAAI,MAAM,sBAAsB,CAAA;AACtD,IAAA,OAAO,KAAM,CAAA,GAAA,CAAI,OAAQ,CAAA,SAAA,EAAgB,CAAA;AAAA;AAG3C,EAAA,IAAI,QAAY,IAAA,OAAA,IAAW,OAAQ,CAAA,MAAA,EAAU,EAAA;AAC3C,IAAA,IAAI,CAAC,KAAM,CAAA,IAAA,EAAY,MAAA,IAAI,MAAM,uBAAuB,CAAA;AACxD,IAAA,OAAO,KAAM,CAAA,IAAA,CAAK,OAAQ,CAAA,MAAA,EAAQ,CAAA;AAAA;AAGpC,EAAA,IAAI,QAAY,IAAA,OAAA,IAAW,OAAQ,CAAA,MAAA,EAAU,EAAA;AAC3C,IAAA,IAAI,CAAC,KAAM,CAAA,IAAA,EAAY,MAAA,IAAI,MAAM,uBAAuB,CAAA;AACxD,IAAA,OAAO,MAAM,IAAK,EAAA;AAAA;AAGpB,EAAM,MAAA,IAAI,MAAM,iCAAiC,CAAA;AACnD;AAGC,MAAA,CAAe,KAAQ,GAAA,KAAA","file":"match.cjs","sourcesContent":["import type { Option } from \"../option\";\nimport type { Result } from \"../result\";\n\nexport function match<T, E extends Error, R>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => R;\n Err: (error: E) => R;\n },\n): R;\nexport function match<T, R>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => R;\n None: () => R;\n },\n): R;\nexport function match<T, E extends Error, R>(\n matcher: Result<T, E> | Option<T>,\n cases: {\n Ok?: (value: T) => R;\n Err?: (error: E) => R;\n Some?: (value: T) => R;\n None?: () => R;\n },\n): R {\n if (\"isOk\" in matcher && matcher.isOk()) {\n if (!cases.Ok) throw new Error(\"Missing case for Ok\");\n return cases.Ok(matcher.unwrap());\n }\n\n if (\"isErr\" in matcher && matcher.isErr()) {\n if (!cases.Err) throw new Error(\"Missing case for Err\");\n return cases.Err(matcher.unwrapErr() as E);\n }\n\n if (\"isSome\" in matcher && matcher.isSome()) {\n if (!cases.Some) throw new Error(\"Missing case for Some\");\n return cases.Some(matcher.unwrap());\n }\n\n if (\"isNone\" in matcher && matcher.isNone()) {\n if (!cases.None) throw new Error(\"Missing case for None\");\n return cases.None();\n }\n\n throw new Error(\"Invalid matcher or missing case\");\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).match = match;\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { O as Option } from '../option-DpT8KCGE.cjs';
|
|
2
|
+
import { R as Result } from '../err-B7LUEZ0f.cjs';
|
|
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,13 @@
|
|
|
1
|
+
import { O as Option } from '../option-DpT8KCGE.js';
|
|
2
|
+
import { R as Result } from '../err-B7LUEZ0f.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,CACd,SACA,KAMG,EAAA;AACH,EAAA,IAAI,MAAU,IAAA,OAAA,IAAW,OAAQ,CAAA,IAAA,EAAQ,EAAA;AACvC,IAAA,IAAI,CAAC,KAAM,CAAA,EAAA,EAAU,MAAA,IAAI,MAAM,qBAAqB,CAAA;AACpD,IAAA,OAAO,KAAM,CAAA,EAAA,CAAG,OAAQ,CAAA,MAAA,EAAQ,CAAA;AAAA;AAGlC,EAAA,IAAI,OAAW,IAAA,OAAA,IAAW,OAAQ,CAAA,KAAA,EAAS,EAAA;AACzC,IAAA,IAAI,CAAC,KAAM,CAAA,GAAA,EAAW,MAAA,IAAI,MAAM,sBAAsB,CAAA;AACtD,IAAA,OAAO,KAAM,CAAA,GAAA,CAAI,OAAQ,CAAA,SAAA,EAAgB,CAAA;AAAA;AAG3C,EAAA,IAAI,QAAY,IAAA,OAAA,IAAW,OAAQ,CAAA,MAAA,EAAU,EAAA;AAC3C,IAAA,IAAI,CAAC,KAAM,CAAA,IAAA,EAAY,MAAA,IAAI,MAAM,uBAAuB,CAAA;AACxD,IAAA,OAAO,KAAM,CAAA,IAAA,CAAK,OAAQ,CAAA,MAAA,EAAQ,CAAA;AAAA;AAGpC,EAAA,IAAI,QAAY,IAAA,OAAA,IAAW,OAAQ,CAAA,MAAA,EAAU,EAAA;AAC3C,IAAA,IAAI,CAAC,KAAM,CAAA,IAAA,EAAY,MAAA,IAAI,MAAM,uBAAuB,CAAA;AACxD,IAAA,OAAO,MAAM,IAAK,EAAA;AAAA;AAGpB,EAAM,MAAA,IAAI,MAAM,iCAAiC,CAAA;AACnD;AAGC,MAAA,CAAe,KAAQ,GAAA,KAAA","file":"match.js","sourcesContent":["import type { Option } from \"../option\";\nimport type { Result } from \"../result\";\n\nexport function match<T, E extends Error, R>(\n matcher: Result<T, E>,\n cases: {\n Ok: (value: T) => R;\n Err: (error: E) => R;\n },\n): R;\nexport function match<T, R>(\n matcher: Option<T>,\n cases: {\n Some: (value: T) => R;\n None: () => R;\n },\n): R;\nexport function match<T, E extends Error, R>(\n matcher: Result<T, E> | Option<T>,\n cases: {\n Ok?: (value: T) => R;\n Err?: (error: E) => R;\n Some?: (value: T) => R;\n None?: () => R;\n },\n): R {\n if (\"isOk\" in matcher && matcher.isOk()) {\n if (!cases.Ok) throw new Error(\"Missing case for Ok\");\n return cases.Ok(matcher.unwrap());\n }\n\n if (\"isErr\" in matcher && matcher.isErr()) {\n if (!cases.Err) throw new Error(\"Missing case for Err\");\n return cases.Err(matcher.unwrapErr() as E);\n }\n\n if (\"isSome\" in matcher && matcher.isSome()) {\n if (!cases.Some) throw new Error(\"Missing case for Some\");\n return cases.Some(matcher.unwrap());\n }\n\n if (\"isNone\" in matcher && matcher.isNone()) {\n if (!cases.None) throw new Error(\"Missing case for None\");\n return cases.None();\n }\n\n throw new Error(\"Invalid matcher or missing case\");\n}\n\n// biome-ignore lint/suspicious/noExplicitAny: <explanation>\n(global as any).match = match;\n"]}
|
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
|