@theateros/result 0.0.1 → 0.0.2

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.
@@ -0,0 +1,2 @@
1
+ "use strict";var l=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var c=Object.prototype.hasOwnProperty;var T=(r,e)=>{for(var t in e)l(r,t,{get:e[t],enumerable:!0})},w=(r,e,t,s)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of b(e))!c.call(r,o)&&o!==t&&l(r,o,{get:()=>e[o],enumerable:!(s=m(e,o))||s.enumerable});return r};var S=r=>w(l({},"__esModule",{value:!0}),r);var _={};T(_,{Result:()=>k});module.exports=S(_);var i=Symbol.for("@theateros/result"),p=Symbol.for("@theateros/result/ok"),a=Symbol.for("@theateros/result/err"),f=Symbol.for("@theateros/result/unset-default-value"),k;(v=>{function r(n){return{_type:i,_kind:p,value:n}}v.ok=r;function e(n){return{_type:i,_kind:a,error:n}}v.err=e;function t(n){return n instanceof Object&&"_type"in n&&"_kind"in n&&n._type===i}v.is=t;function s(n){return t(n)?n._kind===p:!1}v.isOk=s;function o(n){return t(n)?n._kind===a:!1}v.isErr=o;function x(n,u=f){if(s(n))return n.value;if(u===f)throw n.error;return u}v.unwrap=x;function d(n,u){return(...E)=>{try{let y=n(...E);return r(y)}catch(y){return e(u?u(y):y)}}}v.safe=d})(k||={});0&&(module.exports={Result});
2
+ //# sourceMappingURL=result.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/result.ts"],"sourcesContent":["const resultSymbol: unique symbol = Symbol.for('@theateros/result')\nconst okSymbol: unique symbol = Symbol.for('@theateros/result/ok')\nconst errSymbol: unique symbol = Symbol.for('@theateros/result/err')\nconst unsetDefaultValueSymbol: unique symbol = Symbol.for('@theateros/result/unset-default-value')\n\n/**\n * The Ok type represents a successful result.\n *\n * @typeparam T - The value type.\n */\nexport type Ok<T> = {\n /**\n * The type of the result. Allows us to brand the type of the result and ensure that\n * that is comming from the @theateros/result package.\n */\n _type: typeof resultSymbol\n\n /**\n * The kind of the result. Allows us to brand the kind of the result and ensure that\n * that is comming from the @theateros/result package.\n */\n _kind: typeof okSymbol\n\n /**\n * The value of the result.\n */\n value: T\n\n /**\n * The error of the result. Should be never because the result is OK.\n */\n error?: never\n}\n\n/**\n * The Err type represents a failed result.\n *\n * @typeparam E - The error type.\n */\nexport type Err<E> = {\n /**\n * The type of the result. Allows us to brand the type of the result and ensure that\n * that is comming from the @theateros/result package.\n */\n _type: typeof resultSymbol\n\n /**\n * The kind of the result. Allows us to brand the kind of the result and ensure that\n * that is comming from the @theateros/result package.\n */\n _kind: typeof errSymbol\n\n /**\n * The error of the result.\n */\n error: E\n\n /**\n * The value of the result. Should be never because the result is an error.\n */\n value?: never\n}\n\n/**\n * The Result type represents a result of a computation.\n *\n * @typeparam T - The value type.\n * @typeparam E - The error type.\n */\nexport type Result<T, E> = Ok<T> | Err<E>\n\nexport namespace Result {\n /**\n * Create a new Ok result.\n *\n * @typeparam T - The value type.\n * @param value - The value of the result.\n * @returns A new Ok result.\n */\n export function ok<T>(value: T): Ok<T> {\n return { _type: resultSymbol, _kind: okSymbol, value }\n }\n\n /**\n * Create a new Err result.\n *\n * @typeparam E - The error type.\n * @param error - The error of the result.\n * @returns A new Err result.\n */\n export function err<E>(error: E): Err<E> {\n return { _type: resultSymbol, _kind: errSymbol, error }\n }\n\n /**\n * Test if the given result is a result.\n *\n * @param result - The result to check.\n *\n * @typeparam T - The value type.\n * @typeparam E - The error type.\n *\n * @returns True if the result is a result, false otherwise.\n */\n export function is<T = unknown, E = unknown>(result: unknown): result is Result<T, E> {\n return result instanceof Object && '_type' in result && '_kind' in result && result._type === resultSymbol\n }\n\n /**\n * Test if the given result is an Ok result.\n *\n * @param result - The result to check.\n *\n * @typeparam T - The value type.\n *\n * @returns True if the result is an Ok result, false otherwise.\n */\n export function isOk<T = unknown>(result: unknown): result is Ok<T> {\n if (is(result)) {\n return result._kind === okSymbol\n }\n\n return false\n }\n\n /**\n * Test if the given result is an Err result.\n *\n * @param result - The result to check.\n *\n * @typeparam E - The error type.\n *\n * @returns True if the result is an Err result, false otherwise.\n */\n export function isErr<E = unknown>(result: unknown): result is Err<E> {\n if (is(result)) {\n return result._kind === errSymbol\n }\n\n return false\n }\n\n /**\n * Unwrap the value of the given result.\n *\n * @param result - The result to unwrap.\n *\n * @typeparam T - The value type.\n * @typeparam E - The error type.\n *\n * @throws If the result is an Err result and the default value is the unset default value (default value\n * is not provided), the error of the result is thrown.\n *\n * @returns The value of the result.\n */\n export function unwrap<T = unknown, E = unknown>(\n result: Result<T, E>,\n defaultValue: T | typeof unsetDefaultValueSymbol = unsetDefaultValueSymbol,\n ): T {\n if (isOk(result)) {\n return result.value\n }\n\n if (defaultValue === unsetDefaultValueSymbol) {\n throw result.error\n }\n\n return defaultValue\n }\n\n /**\n * A simple helper that represents any function.\n *\n * @typeparam Args - The arguments type.\n * @typeparam Return - The return type.\n */\n // biome-ignore lint/suspicious/noExplicitAny: could be any function here\n type AnyFn = (...args: any[]) => any\n\n /**\n * Safe wrap a function in a Result.\n *\n * @param fn - The function to safe wrap.\n * @param onError - The function to handle the error.\n *\n * @typeparam T - The value type.\n * @typeparam E - The error type.\n *\n * @returns A new function that wraps the given function in a Result.\n */\n export function safe<Fn extends AnyFn, E = unknown>(fn: Fn, onError?: (error: unknown) => E) {\n return (...args: Parameters<Fn>): Result<ReturnType<Fn>, E> => {\n try {\n const value = fn(...args) as ReturnType<Fn>\n\n return ok(value)\n } catch (error) {\n if (onError) {\n return err(onError(error))\n }\n\n return err(error as E)\n }\n }\n }\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,YAAAE,IAAA,eAAAC,EAAAH,GAAA,IAAMI,EAA8B,OAAO,IAAI,mBAAmB,EAC5DC,EAA0B,OAAO,IAAI,sBAAsB,EAC3DC,EAA2B,OAAO,IAAI,uBAAuB,EAC7DC,EAAyC,OAAO,IAAI,uCAAuC,EAoEhFL,MAAV,CAQE,SAASM,EAAMC,EAAiB,CACrC,MAAO,CAAE,MAAOL,EAAc,MAAOC,EAAU,MAAAI,CAAM,CACvD,CAFOP,EAAS,GAAAM,EAWT,SAASE,EAAOC,EAAkB,CACvC,MAAO,CAAE,MAAOP,EAAc,MAAOE,EAAW,MAAAK,CAAM,CACxD,CAFOT,EAAS,IAAAQ,EAcT,SAASE,EAA6BC,EAAyC,CACpF,OAAOA,aAAkB,QAAU,UAAWA,GAAU,UAAWA,GAAUA,EAAO,QAAUT,CAChG,CAFOF,EAAS,GAAAU,EAaT,SAASE,EAAkBD,EAAkC,CAClE,OAAID,EAAGC,CAAM,EACJA,EAAO,QAAUR,EAGnB,EACT,CANOH,EAAS,KAAAY,EAiBT,SAASC,EAAmBF,EAAmC,CACpE,OAAID,EAAGC,CAAM,EACJA,EAAO,QAAUP,EAGnB,EACT,CANOJ,EAAS,MAAAa,EAqBT,SAASC,EACdH,EACAI,EAAmDV,EAChD,CACH,GAAIO,EAAKD,CAAM,EACb,OAAOA,EAAO,MAGhB,GAAII,IAAiBV,EACnB,MAAMM,EAAO,MAGf,OAAOI,CACT,CAbOf,EAAS,OAAAc,EAmCT,SAASE,EAAoCC,EAAQC,EAAiC,CAC3F,MAAO,IAAIC,IAAoD,CAC7D,GAAI,CACF,IAAMZ,EAAQU,EAAG,GAAGE,CAAI,EAExB,OAAOb,EAAGC,CAAK,CACjB,OAASE,EAAO,CACd,OACSD,EADLU,EACSA,EAAQT,CAAK,EAGfA,CAHgB,CAI7B,CACF,CACF,CAdOT,EAAS,KAAAgB,IAvHDhB,IAAA","names":["result_exports","__export","Result","__toCommonJS","resultSymbol","okSymbol","errSymbol","unsetDefaultValueSymbol","ok","value","err","error","is","result","isOk","isErr","unwrap","defaultValue","safe","fn","onError","args"]}
@@ -0,0 +1,146 @@
1
+ declare const resultSymbol: unique symbol;
2
+ declare const okSymbol: unique symbol;
3
+ declare const errSymbol: unique symbol;
4
+ declare const unsetDefaultValueSymbol: unique symbol;
5
+ /**
6
+ * The Ok type represents a successful result.
7
+ *
8
+ * @typeparam T - The value type.
9
+ */
10
+ type Ok<T> = {
11
+ /**
12
+ * The type of the result. Allows us to brand the type of the result and ensure that
13
+ * that is comming from the @theateros/result package.
14
+ */
15
+ _type: typeof resultSymbol;
16
+ /**
17
+ * The kind of the result. Allows us to brand the kind of the result and ensure that
18
+ * that is comming from the @theateros/result package.
19
+ */
20
+ _kind: typeof okSymbol;
21
+ /**
22
+ * The value of the result.
23
+ */
24
+ value: T;
25
+ /**
26
+ * The error of the result. Should be never because the result is OK.
27
+ */
28
+ error?: never;
29
+ };
30
+ /**
31
+ * The Err type represents a failed result.
32
+ *
33
+ * @typeparam E - The error type.
34
+ */
35
+ type Err<E> = {
36
+ /**
37
+ * The type of the result. Allows us to brand the type of the result and ensure that
38
+ * that is comming from the @theateros/result package.
39
+ */
40
+ _type: typeof resultSymbol;
41
+ /**
42
+ * The kind of the result. Allows us to brand the kind of the result and ensure that
43
+ * that is comming from the @theateros/result package.
44
+ */
45
+ _kind: typeof errSymbol;
46
+ /**
47
+ * The error of the result.
48
+ */
49
+ error: E;
50
+ /**
51
+ * The value of the result. Should be never because the result is an error.
52
+ */
53
+ value?: never;
54
+ };
55
+ /**
56
+ * The Result type represents a result of a computation.
57
+ *
58
+ * @typeparam T - The value type.
59
+ * @typeparam E - The error type.
60
+ */
61
+ type Result<T, E> = Ok<T> | Err<E>;
62
+ declare namespace Result {
63
+ /**
64
+ * Create a new Ok result.
65
+ *
66
+ * @typeparam T - The value type.
67
+ * @param value - The value of the result.
68
+ * @returns A new Ok result.
69
+ */
70
+ export function ok<T>(value: T): Ok<T>;
71
+ /**
72
+ * Create a new Err result.
73
+ *
74
+ * @typeparam E - The error type.
75
+ * @param error - The error of the result.
76
+ * @returns A new Err result.
77
+ */
78
+ export function err<E>(error: E): Err<E>;
79
+ /**
80
+ * Test if the given result is a result.
81
+ *
82
+ * @param result - The result to check.
83
+ *
84
+ * @typeparam T - The value type.
85
+ * @typeparam E - The error type.
86
+ *
87
+ * @returns True if the result is a result, false otherwise.
88
+ */
89
+ export function is<T = unknown, E = unknown>(result: unknown): result is Result<T, E>;
90
+ /**
91
+ * Test if the given result is an Ok result.
92
+ *
93
+ * @param result - The result to check.
94
+ *
95
+ * @typeparam T - The value type.
96
+ *
97
+ * @returns True if the result is an Ok result, false otherwise.
98
+ */
99
+ export function isOk<T = unknown>(result: unknown): result is Ok<T>;
100
+ /**
101
+ * Test if the given result is an Err result.
102
+ *
103
+ * @param result - The result to check.
104
+ *
105
+ * @typeparam E - The error type.
106
+ *
107
+ * @returns True if the result is an Err result, false otherwise.
108
+ */
109
+ export function isErr<E = unknown>(result: unknown): result is Err<E>;
110
+ /**
111
+ * Unwrap the value of the given result.
112
+ *
113
+ * @param result - The result to unwrap.
114
+ *
115
+ * @typeparam T - The value type.
116
+ * @typeparam E - The error type.
117
+ *
118
+ * @throws If the result is an Err result and the default value is the unset default value (default value
119
+ * is not provided), the error of the result is thrown.
120
+ *
121
+ * @returns The value of the result.
122
+ */
123
+ export function unwrap<T = unknown, E = unknown>(result: Result<T, E>, defaultValue?: T | typeof unsetDefaultValueSymbol): T;
124
+ /**
125
+ * A simple helper that represents any function.
126
+ *
127
+ * @typeparam Args - The arguments type.
128
+ * @typeparam Return - The return type.
129
+ */
130
+ type AnyFn = (...args: any[]) => any;
131
+ /**
132
+ * Safe wrap a function in a Result.
133
+ *
134
+ * @param fn - The function to safe wrap.
135
+ * @param onError - The function to handle the error.
136
+ *
137
+ * @typeparam T - The value type.
138
+ * @typeparam E - The error type.
139
+ *
140
+ * @returns A new function that wraps the given function in a Result.
141
+ */
142
+ export function safe<Fn extends AnyFn, E = unknown>(fn: Fn, onError?: (error: unknown) => E): (...args: Parameters<Fn>) => Result<ReturnType<Fn>, E>;
143
+ export { };
144
+ }
145
+
146
+ export { type Err, type Ok, Result };
package/dist/result.d.ts CHANGED
@@ -7,7 +7,7 @@ declare const unsetDefaultValueSymbol: unique symbol;
7
7
  *
8
8
  * @typeparam T - The value type.
9
9
  */
10
- export type Ok<T> = {
10
+ type Ok<T> = {
11
11
  /**
12
12
  * The type of the result. Allows us to brand the type of the result and ensure that
13
13
  * that is comming from the @theateros/result package.
@@ -32,7 +32,7 @@ export type Ok<T> = {
32
32
  *
33
33
  * @typeparam E - The error type.
34
34
  */
35
- export type Err<E> = {
35
+ type Err<E> = {
36
36
  /**
37
37
  * The type of the result. Allows us to brand the type of the result and ensure that
38
38
  * that is comming from the @theateros/result package.
@@ -58,8 +58,8 @@ export type Err<E> = {
58
58
  * @typeparam T - The value type.
59
59
  * @typeparam E - The error type.
60
60
  */
61
- export type Result<T, E> = Ok<T> | Err<E>;
62
- export declare namespace Result {
61
+ type Result<T, E> = Ok<T> | Err<E>;
62
+ declare namespace Result {
63
63
  /**
64
64
  * Create a new Ok result.
65
65
  *
@@ -140,7 +140,7 @@ export declare namespace Result {
140
140
  * @returns A new function that wraps the given function in a Result.
141
141
  */
142
142
  export function safe<Fn extends AnyFn, E = unknown>(fn: Fn, onError?: (error: unknown) => E): (...args: Parameters<Fn>) => Result<ReturnType<Fn>, E>;
143
- export {};
143
+ export { };
144
144
  }
145
- export {};
146
- //# sourceMappingURL=result.d.ts.map
145
+
146
+ export { type Err, type Ok, Result };
package/dist/result.js CHANGED
@@ -1,128 +1,2 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Result = void 0;
4
- const resultSymbol = Symbol.for('@theateros/result');
5
- const okSymbol = Symbol.for('@theateros/result/ok');
6
- const errSymbol = Symbol.for('@theateros/result/err');
7
- const unsetDefaultValueSymbol = Symbol.for('@theateros/result/unset-default-value');
8
- var Result;
9
- (function (Result) {
10
- /**
11
- * Create a new Ok result.
12
- *
13
- * @typeparam T - The value type.
14
- * @param value - The value of the result.
15
- * @returns A new Ok result.
16
- */
17
- function ok(value) {
18
- return { _type: resultSymbol, _kind: okSymbol, value };
19
- }
20
- Result.ok = ok;
21
- /**
22
- * Create a new Err result.
23
- *
24
- * @typeparam E - The error type.
25
- * @param error - The error of the result.
26
- * @returns A new Err result.
27
- */
28
- function err(error) {
29
- return { _type: resultSymbol, _kind: errSymbol, error };
30
- }
31
- Result.err = err;
32
- /**
33
- * Test if the given result is a result.
34
- *
35
- * @param result - The result to check.
36
- *
37
- * @typeparam T - The value type.
38
- * @typeparam E - The error type.
39
- *
40
- * @returns True if the result is a result, false otherwise.
41
- */
42
- function is(result) {
43
- return result instanceof Object && '_type' in result && '_kind' in result && result._type === resultSymbol;
44
- }
45
- Result.is = is;
46
- /**
47
- * Test if the given result is an Ok result.
48
- *
49
- * @param result - The result to check.
50
- *
51
- * @typeparam T - The value type.
52
- *
53
- * @returns True if the result is an Ok result, false otherwise.
54
- */
55
- function isOk(result) {
56
- if (is(result)) {
57
- return result._kind === okSymbol;
58
- }
59
- return false;
60
- }
61
- Result.isOk = isOk;
62
- /**
63
- * Test if the given result is an Err result.
64
- *
65
- * @param result - The result to check.
66
- *
67
- * @typeparam E - The error type.
68
- *
69
- * @returns True if the result is an Err result, false otherwise.
70
- */
71
- function isErr(result) {
72
- if (is(result)) {
73
- return result._kind === errSymbol;
74
- }
75
- return false;
76
- }
77
- Result.isErr = isErr;
78
- /**
79
- * Unwrap the value of the given result.
80
- *
81
- * @param result - The result to unwrap.
82
- *
83
- * @typeparam T - The value type.
84
- * @typeparam E - The error type.
85
- *
86
- * @throws If the result is an Err result and the default value is the unset default value (default value
87
- * is not provided), the error of the result is thrown.
88
- *
89
- * @returns The value of the result.
90
- */
91
- function unwrap(result, defaultValue = unsetDefaultValueSymbol) {
92
- if (isOk(result)) {
93
- return result.value;
94
- }
95
- if (defaultValue === unsetDefaultValueSymbol) {
96
- throw result.error;
97
- }
98
- return defaultValue;
99
- }
100
- Result.unwrap = unwrap;
101
- /**
102
- * Safe wrap a function in a Result.
103
- *
104
- * @param fn - The function to safe wrap.
105
- * @param onError - The function to handle the error.
106
- *
107
- * @typeparam T - The value type.
108
- * @typeparam E - The error type.
109
- *
110
- * @returns A new function that wraps the given function in a Result.
111
- */
112
- function safe(fn, onError) {
113
- return (...args) => {
114
- try {
115
- const value = fn(...args);
116
- return ok(value);
117
- }
118
- catch (error) {
119
- if (onError) {
120
- return err(onError(error));
121
- }
122
- return err(error);
123
- }
124
- };
125
- }
126
- Result.safe = safe;
127
- })(Result || (exports.Result = Result = {}));
1
+ var t=Symbol.for("@theateros/result"),s=Symbol.for("@theateros/result/ok"),y=Symbol.for("@theateros/result/err"),l=Symbol.for("@theateros/result/unset-default-value"),f;(b=>{function i(n){return{_type:t,_kind:s,value:n}}b.ok=i;function o(n){return{_type:t,_kind:y,error:n}}b.err=o;function u(n){return n instanceof Object&&"_type"in n&&"_kind"in n&&n._type===t}b.is=u;function p(n){return u(n)?n._kind===s:!1}b.isOk=p;function k(n){return u(n)?n._kind===y:!1}b.isErr=k;function E(n,e=l){if(p(n))return n.value;if(e===l)throw n.error;return e}b.unwrap=E;function m(n,e){return(...a)=>{try{let r=n(...a);return i(r)}catch(r){return o(e?e(r):r)}}}b.safe=m})(f||={});export{f as Result};
128
2
  //# sourceMappingURL=result.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"result.js","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":";;;AAAA,MAAM,YAAY,GAAkB,MAAM,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAA;AACnE,MAAM,QAAQ,GAAkB,MAAM,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAA;AAClE,MAAM,SAAS,GAAkB,MAAM,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAA;AACpE,MAAM,uBAAuB,GAAkB,MAAM,CAAC,GAAG,CAAC,uCAAuC,CAAC,CAAA;AAoElG,IAAiB,MAAM,CAsItB;AAtID,WAAiB,MAAM;IACrB;;;;;;OAMG;IACH,SAAgB,EAAE,CAAI,KAAQ;QAC5B,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,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;;OAUG;IACH,SAAgB,IAAI,CAAgC,EAAM,EAAE,OAA+B;QACzF,OAAO,CAAC,GAAG,IAAoB,EAA6B,EAAE;YAC5D,IAAI,CAAC;gBACH,MAAM,KAAK,GAAG,EAAE,CAAC,GAAG,IAAI,CAAmB,CAAA;gBAE3C,OAAO,EAAE,CAAC,KAAK,CAAC,CAAA;YAClB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,IAAI,OAAO,EAAE,CAAC;oBACZ,OAAO,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAA;gBAC5B,CAAC;gBAED,OAAO,GAAG,CAAC,KAAU,CAAC,CAAA;YACxB,CAAC;QACH,CAAC,CAAA;IACH,CAAC;IAde,WAAI,OAcnB,CAAA;AACH,CAAC,EAtIgB,MAAM,sBAAN,MAAM,QAsItB"}
1
+ {"version":3,"sources":["../src/result.ts"],"sourcesContent":["const resultSymbol: unique symbol = Symbol.for('@theateros/result')\nconst okSymbol: unique symbol = Symbol.for('@theateros/result/ok')\nconst errSymbol: unique symbol = Symbol.for('@theateros/result/err')\nconst unsetDefaultValueSymbol: unique symbol = Symbol.for('@theateros/result/unset-default-value')\n\n/**\n * The Ok type represents a successful result.\n *\n * @typeparam T - The value type.\n */\nexport type Ok<T> = {\n /**\n * The type of the result. Allows us to brand the type of the result and ensure that\n * that is comming from the @theateros/result package.\n */\n _type: typeof resultSymbol\n\n /**\n * The kind of the result. Allows us to brand the kind of the result and ensure that\n * that is comming from the @theateros/result package.\n */\n _kind: typeof okSymbol\n\n /**\n * The value of the result.\n */\n value: T\n\n /**\n * The error of the result. Should be never because the result is OK.\n */\n error?: never\n}\n\n/**\n * The Err type represents a failed result.\n *\n * @typeparam E - The error type.\n */\nexport type Err<E> = {\n /**\n * The type of the result. Allows us to brand the type of the result and ensure that\n * that is comming from the @theateros/result package.\n */\n _type: typeof resultSymbol\n\n /**\n * The kind of the result. Allows us to brand the kind of the result and ensure that\n * that is comming from the @theateros/result package.\n */\n _kind: typeof errSymbol\n\n /**\n * The error of the result.\n */\n error: E\n\n /**\n * The value of the result. Should be never because the result is an error.\n */\n value?: never\n}\n\n/**\n * The Result type represents a result of a computation.\n *\n * @typeparam T - The value type.\n * @typeparam E - The error type.\n */\nexport type Result<T, E> = Ok<T> | Err<E>\n\nexport namespace Result {\n /**\n * Create a new Ok result.\n *\n * @typeparam T - The value type.\n * @param value - The value of the result.\n * @returns A new Ok result.\n */\n export function ok<T>(value: T): Ok<T> {\n return { _type: resultSymbol, _kind: okSymbol, value }\n }\n\n /**\n * Create a new Err result.\n *\n * @typeparam E - The error type.\n * @param error - The error of the result.\n * @returns A new Err result.\n */\n export function err<E>(error: E): Err<E> {\n return { _type: resultSymbol, _kind: errSymbol, error }\n }\n\n /**\n * Test if the given result is a result.\n *\n * @param result - The result to check.\n *\n * @typeparam T - The value type.\n * @typeparam E - The error type.\n *\n * @returns True if the result is a result, false otherwise.\n */\n export function is<T = unknown, E = unknown>(result: unknown): result is Result<T, E> {\n return result instanceof Object && '_type' in result && '_kind' in result && result._type === resultSymbol\n }\n\n /**\n * Test if the given result is an Ok result.\n *\n * @param result - The result to check.\n *\n * @typeparam T - The value type.\n *\n * @returns True if the result is an Ok result, false otherwise.\n */\n export function isOk<T = unknown>(result: unknown): result is Ok<T> {\n if (is(result)) {\n return result._kind === okSymbol\n }\n\n return false\n }\n\n /**\n * Test if the given result is an Err result.\n *\n * @param result - The result to check.\n *\n * @typeparam E - The error type.\n *\n * @returns True if the result is an Err result, false otherwise.\n */\n export function isErr<E = unknown>(result: unknown): result is Err<E> {\n if (is(result)) {\n return result._kind === errSymbol\n }\n\n return false\n }\n\n /**\n * Unwrap the value of the given result.\n *\n * @param result - The result to unwrap.\n *\n * @typeparam T - The value type.\n * @typeparam E - The error type.\n *\n * @throws If the result is an Err result and the default value is the unset default value (default value\n * is not provided), the error of the result is thrown.\n *\n * @returns The value of the result.\n */\n export function unwrap<T = unknown, E = unknown>(\n result: Result<T, E>,\n defaultValue: T | typeof unsetDefaultValueSymbol = unsetDefaultValueSymbol,\n ): T {\n if (isOk(result)) {\n return result.value\n }\n\n if (defaultValue === unsetDefaultValueSymbol) {\n throw result.error\n }\n\n return defaultValue\n }\n\n /**\n * A simple helper that represents any function.\n *\n * @typeparam Args - The arguments type.\n * @typeparam Return - The return type.\n */\n // biome-ignore lint/suspicious/noExplicitAny: could be any function here\n type AnyFn = (...args: any[]) => any\n\n /**\n * Safe wrap a function in a Result.\n *\n * @param fn - The function to safe wrap.\n * @param onError - The function to handle the error.\n *\n * @typeparam T - The value type.\n * @typeparam E - The error type.\n *\n * @returns A new function that wraps the given function in a Result.\n */\n export function safe<Fn extends AnyFn, E = unknown>(fn: Fn, onError?: (error: unknown) => E) {\n return (...args: Parameters<Fn>): Result<ReturnType<Fn>, E> => {\n try {\n const value = fn(...args) as ReturnType<Fn>\n\n return ok(value)\n } catch (error) {\n if (onError) {\n return err(onError(error))\n }\n\n return err(error as E)\n }\n }\n }\n}\n"],"mappings":"AAAA,IAAMA,EAA8B,OAAO,IAAI,mBAAmB,EAC5DC,EAA0B,OAAO,IAAI,sBAAsB,EAC3DC,EAA2B,OAAO,IAAI,uBAAuB,EAC7DC,EAAyC,OAAO,IAAI,uCAAuC,EAoEhFC,MAAV,CAQE,SAASC,EAAMC,EAAiB,CACrC,MAAO,CAAE,MAAON,EAAc,MAAOC,EAAU,MAAAK,CAAM,CACvD,CAFOF,EAAS,GAAAC,EAWT,SAASE,EAAOC,EAAkB,CACvC,MAAO,CAAE,MAAOR,EAAc,MAAOE,EAAW,MAAAM,CAAM,CACxD,CAFOJ,EAAS,IAAAG,EAcT,SAASE,EAA6BC,EAAyC,CACpF,OAAOA,aAAkB,QAAU,UAAWA,GAAU,UAAWA,GAAUA,EAAO,QAAUV,CAChG,CAFOI,EAAS,GAAAK,EAaT,SAASE,EAAkBD,EAAkC,CAClE,OAAID,EAAGC,CAAM,EACJA,EAAO,QAAUT,EAGnB,EACT,CANOG,EAAS,KAAAO,EAiBT,SAASC,EAAmBF,EAAmC,CACpE,OAAID,EAAGC,CAAM,EACJA,EAAO,QAAUR,EAGnB,EACT,CANOE,EAAS,MAAAQ,EAqBT,SAASC,EACdH,EACAI,EAAmDX,EAChD,CACH,GAAIQ,EAAKD,CAAM,EACb,OAAOA,EAAO,MAGhB,GAAII,IAAiBX,EACnB,MAAMO,EAAO,MAGf,OAAOI,CACT,CAbOV,EAAS,OAAAS,EAmCT,SAASE,EAAoCC,EAAQC,EAAiC,CAC3F,MAAO,IAAIC,IAAoD,CAC7D,GAAI,CACF,IAAMZ,EAAQU,EAAG,GAAGE,CAAI,EAExB,OAAOb,EAAGC,CAAK,CACjB,OAASE,EAAO,CACd,OACSD,EADLU,EACSA,EAAQT,CAAK,EAGfA,CAHgB,CAI7B,CACF,CACF,CAdOJ,EAAS,KAAAW,IAvHDX,IAAA","names":["resultSymbol","okSymbol","errSymbol","unsetDefaultValueSymbol","Result","ok","value","err","error","is","result","isOk","isErr","unwrap","defaultValue","safe","fn","onError","args"]}
package/package.json CHANGED
@@ -1,22 +1,25 @@
1
1
  {
2
2
  "name": "@theateros/result",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "module": "dist/result.js",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "lint": "biome check .",
8
8
  "test": "bun test",
9
- "build:dist": "tsc",
9
+ "build:dist": "rm -rf dist && tsup",
10
10
  "release": "release-it"
11
11
  },
12
12
  "devDependencies": {
13
13
  "@types/bun": "catalog:",
14
14
  "typescript": "catalog:",
15
- "release-it": "catalog:"
15
+ "release-it": "catalog:",
16
+ "tsup": "catalog:"
16
17
  },
17
18
  "exports": {
18
19
  ".": {
19
20
  "types": "./dist/result.d.ts",
21
+ "import": "./dist/result.js",
22
+ "require": "./dist/result.cjs",
20
23
  "default": "./dist/result.js"
21
24
  }
22
25
  },
package/tsup.config.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { defineConfig } from 'tsup'
2
+ import { baseConfig } from '../../tsup.base'
3
+
4
+ export default defineConfig({
5
+ entry: ['src/result.ts'],
6
+ ...baseConfig,
7
+ })
@@ -1 +0,0 @@
1
- {"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../src/result.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,YAAY,EAAE,OAAO,MAAwC,CAAA;AACnE,QAAA,MAAM,QAAQ,EAAE,OAAO,MAA2C,CAAA;AAClE,QAAA,MAAM,SAAS,EAAE,OAAO,MAA4C,CAAA;AACpE,QAAA,MAAM,uBAAuB,EAAE,OAAO,MAA4D,CAAA;AAElG;;;;GAIG;AACH,MAAM,MAAM,EAAE,CAAC,CAAC,IAAI;IAClB;;;OAGG;IACH,KAAK,EAAE,OAAO,YAAY,CAAA;IAE1B;;;OAGG;IACH,KAAK,EAAE,OAAO,QAAQ,CAAA;IAEtB;;OAEG;IACH,KAAK,EAAE,CAAC,CAAA;IAER;;OAEG;IACH,KAAK,CAAC,EAAE,KAAK,CAAA;CACd,CAAA;AAED;;;;GAIG;AACH,MAAM,MAAM,GAAG,CAAC,CAAC,IAAI;IACnB;;;OAGG;IACH,KAAK,EAAE,OAAO,YAAY,CAAA;IAE1B;;;OAGG;IACH,KAAK,EAAE,OAAO,SAAS,CAAA;IAEvB;;OAEG;IACH,KAAK,EAAE,CAAC,CAAA;IAER;;OAEG;IACH,KAAK,CAAC,EAAE,KAAK,CAAA;CACd,CAAA;AAED;;;;;GAKG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAA;AAEzC,yBAAiB,MAAM,CAAC;IACtB;;;;;;OAMG;IACH,MAAM,UAAU,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAErC;IAED;;;;;;OAMG;IACH,MAAM,UAAU,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,GAAG,CAAC,CA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