@l3dev/result 0.2.1 → 0.2.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.
Files changed (2) hide show
  1. package/README.md +120 -0
  2. package/package.json +3 -2
package/README.md ADDED
@@ -0,0 +1,120 @@
1
+ ## Description
2
+
3
+ A `Result` based error handling library. Inspired by Rust's `Result` type and [neverthrow](https://www.npmjs.com/package/neverthrow).
4
+
5
+ This package aims to simplify neverthrow's API while still offering the result type-safety.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @l3dev/result
11
+ ```
12
+
13
+ ```bash
14
+ pnpm add @l3dev/result
15
+ ```
16
+
17
+ ## Documentation
18
+
19
+ ### `ok`
20
+
21
+ Creates an `Ok` object, alias: `Result.ok`
22
+
23
+ ```ts
24
+ ok<T>(value: T): Ok<T>
25
+ ```
26
+
27
+ #### Example
28
+
29
+ ```ts
30
+ import { ok } from "@l3dev/result";
31
+
32
+ const result = ok(42);
33
+
34
+ result.ok; // true
35
+ result.value; // 42
36
+ ```
37
+
38
+ > Use `NONE` as a shorthand for a null ok result, it is equivalent to `Ok<null>`
39
+
40
+ ### `err`
41
+
42
+ Create an `Err` object, alias: `Result.err`
43
+
44
+ ```ts
45
+ err<T>(type: T): Err<T, null>
46
+ err<T, D>(type: T, context: D): Err<T, D>
47
+ ```
48
+
49
+ #### Example
50
+
51
+ ```ts
52
+ import { err } from "@l3dev/result";
53
+
54
+ const result = err("error");
55
+
56
+ result.ok; // false
57
+ result.type; // 'error'
58
+ result.context; // null
59
+
60
+ const resultWithContext = err("error", { message: "Something went wrong" });
61
+
62
+ result.ok; // false
63
+ result.type; // 'error'
64
+ result.context; // { message: 'Something went wrong' }
65
+ ```
66
+
67
+ ### `Result.isOk`
68
+
69
+ Returns `true` if the result is an `Ok` object
70
+
71
+ ```ts
72
+ isOk(result: ReturnResult<any, any>): boolean
73
+ ```
74
+
75
+ ### `Result.isErr`
76
+
77
+ Returns `true` if the result is an `Err` object
78
+
79
+ ```ts
80
+ isErr(result: ReturnResult<any, any>): boolean
81
+ ```
82
+
83
+ ### `Result.fn`
84
+
85
+ Wraps a function requiring that the ReturnType be a `ReturnResult` type.
86
+
87
+ ```ts
88
+ fn<T, F extends (...) => T>(fn: F): F
89
+ ```
90
+
91
+ #### Example
92
+
93
+ ```ts
94
+ import { ok, err, Result } from "@l3dev/result";
95
+
96
+ const myFunction = Result.fn((roll: number) => {
97
+ if (roll < 1 || roll > 6) {
98
+ return err("Invalid roll");
99
+ }
100
+ return ok(roll);
101
+ });
102
+
103
+ const result = myFunction(3); // Ok<3> | Err<"Invalid roll", null>
104
+ ```
105
+
106
+ ### `Result.fromPromise`
107
+
108
+ Catches any errors thrown by a promise into `Err` objects.
109
+
110
+ ```ts
111
+ fromPromise<T>(promise: T): Promise<ReturnResult<Awaited<T>, ResultErrorDefinition<null, { error: TError }>>>
112
+ ```
113
+
114
+ ### `Result.unwrapOrDefault`
115
+
116
+ Unwraps the result, returning the value if it is an `Ok` object, or the default value if it is an `Err` object.
117
+
118
+ ```ts
119
+ unwrapOrDefault<T>(result: ReturnResult<T, any>, defaultValue: T): T
120
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@l3dev/result",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Type-safe result-based error handling",
5
5
  "type": "module",
6
6
  "main": "./build/index.js",
@@ -16,7 +16,8 @@
16
16
  }
17
17
  },
18
18
  "files": [
19
- "build"
19
+ "build",
20
+ "README.md"
20
21
  ],
21
22
  "author": "l3dotdev",
22
23
  "license": "MIT",