flatlock 1.0.1

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/src/result.js ADDED
@@ -0,0 +1,35 @@
1
+ /**
2
+ * @template T
3
+ * @typedef {Object} OkResult
4
+ * @property {true} ok
5
+ * @property {T} value
6
+ */
7
+
8
+ /**
9
+ * @typedef {Object} ErrResult
10
+ * @property {false} ok
11
+ * @property {Error} error
12
+ */
13
+
14
+ /**
15
+ * @template T
16
+ * @typedef {OkResult<T> | ErrResult} Result
17
+ */
18
+
19
+ /**
20
+ * Create a successful Result
21
+ * @template T
22
+ * @param {T} value - The success value
23
+ * @returns {OkResult<T>}
24
+ */
25
+ export const Ok = (value) => ({ ok: true, value });
26
+
27
+ /**
28
+ * Create a failed Result
29
+ * @param {Error | string} error - The error
30
+ * @returns {ErrResult}
31
+ */
32
+ export const Err = (error) => ({
33
+ ok: false,
34
+ error: error instanceof Error ? error : new Error(error)
35
+ });