|
1 | | -# @moeru/eslint-config |
2 | | - |
3 | | -> Small and tree-shakeable Result/Option implementation. |
4 | | -
|
5 | | -## Usage |
6 | | - |
7 | | -### Result |
8 | | - |
9 | | -> compare with [Rust by Example](https://doc.rust-lang.org/rust-by-example/std/option.html) |
10 | | -
|
11 | | -```ts |
12 | | -import type { Option } from '@moeru/results' |
13 | | - |
14 | | -import { none, option as o, some } from '@moeru/results' |
15 | | - |
16 | | -const checkedDivision = (dividend: number, divisor: number): Option<number> => |
17 | | - divisor === 0 |
18 | | - ? none |
19 | | - : some(dividend / divisor) |
20 | | - |
21 | | -const tryDivision = (dividend: number, divisor: number) => |
22 | | - o.match( |
23 | | - checkedDivision(dividend, divisor), |
24 | | - quotient => `${dividend} / ${divisor} = ${quotient}`, |
25 | | - () => { throw new Error(`${dividend} / ${divisor} failed!`) }, |
26 | | - ) |
27 | | - |
28 | | -const main = () => { |
29 | | - tryDivision(4, 2) // "4 / 2 = 2" |
30 | | - tryDivision(1, 0) // throw error |
31 | | - |
32 | | - const optionalFloat = some(0) |
33 | | - |
34 | | - console.log(`${optionalFloat} unwraps to ${o.unwrap(optionalFloat)}`) |
35 | | - console.log(`${none} unwraps to ${o.unwrap(none)}`) // throw error |
36 | | -} |
37 | | -``` |
38 | | - |
39 | | -### Result |
40 | | - |
41 | | -> compare with [Rust by Example](https://doc.rust-lang.org/rust-by-example/std/result.html) |
42 | | -
|
43 | | -```ts |
44 | | -import type { Result } from '@moeru/results' |
45 | | - |
46 | | -import { err, ok, result as r } from '@moeru/results' |
47 | | - |
48 | | -enum MathError { |
49 | | - DivisionByZero = 'DivisionByZero', |
50 | | - NegativeSquareRoot = 'NegativeSquareRoot', |
51 | | - NonPositiveLogarithm = 'NonPositiveLogarithm', |
52 | | -} |
53 | | - |
54 | | -type MathResult = Result<number, MathError> |
55 | | - |
56 | | -const div = (x: number, y: number): MathResult => |
57 | | - y === 0 |
58 | | - ? err(MathError.DivisionByZero) |
59 | | - : ok(x / y) |
60 | | - |
61 | | -const sqrt = (x: number): MathResult => |
62 | | - x < 0 |
63 | | - ? err(MathError.NegativeSquareRoot) |
64 | | - : ok(Math.sqrt(x)) |
65 | | - |
66 | | -const ln = (x: number): MathResult => |
67 | | - x <= 0 |
68 | | - ? err(MathError.NonPositiveLogarithm) |
69 | | - : ok(Math.log(x)) |
70 | | - |
71 | | -const op = (x: number, y: number): number => |
72 | | - r.match( |
73 | | - div(x, y), |
74 | | - radio => r.match( |
75 | | - ln(radio), |
76 | | - ln => r.match( |
77 | | - sqrt(ln), |
78 | | - sqrt => sqrt, |
79 | | - (err) => { throw err }, |
80 | | - ), |
81 | | - (err) => { throw err }, |
82 | | - ), |
83 | | - (err) => { throw err }, |
84 | | - ) |
85 | | - |
86 | | -const main = () => |
87 | | - console.log(op(1, 10)) |
88 | | -``` |
89 | | - |
90 | | -### Methods |
91 | | - |
92 | | -Most of the methods are the same as in Rust. |
93 | | - |
94 | | -We provide different import points for Option and Result: |
95 | | - |
96 | | -```ts |
97 | | -import { option as o, result as r } from '@moeru/results' |
98 | | -// import * as o from '@moeru/results/option' |
99 | | -// import * as r from '@moeru/results/result' |
100 | | - |
101 | | -console.log(Object.keys(o)) |
102 | | -console.log(Object.keys(r)) |
103 | | -``` |
104 | | - |
105 | | -###### extract |
106 | | - |
107 | | -Extracted values or errors. |
108 | | - |
109 | | -```ts |
110 | | -import { option as o, ok, result as r, some } from '@moeru/results' |
111 | | - |
112 | | -o.extract(some('foo') as Option<string>) // string | undefined |
113 | | -r.extract(ok('bar') as Result<string, Error>) // string | Error |
114 | | -``` |
115 | | - |
116 | | -###### from |
117 | | - |
118 | | -Convert common types / function results to Option or Result. |
119 | | - |
120 | | -```ts |
121 | | -import { option as o, result as r } from '@moeru/results' |
122 | | - |
123 | | -o.from('foo' as 'foo' | undefined) // Option<'foo'> |
124 | | - |
125 | | -export const tryParseURL = (url: string) => |
126 | | - r.from(() => new URL(url)) // Result<string, Error> |
127 | | -``` |
128 | | - |
129 | | -###### wrap |
130 | | - |
131 | | -You can achieve something like the question mark operator in Rust. |
132 | | - |
133 | | -Let's modify the Result Example above to use `wrap` / `unwrap` instead of `match`: |
134 | | - |
135 | | -```diff |
136 | | -const op = (x: number, y: number): number => |
137 | | - r.match( |
138 | | -- div(x, y), |
139 | | -- radio => r.match( |
140 | | -- ln(radio), |
141 | | -- ln => r.match( |
142 | | -- sqrt(ln), |
143 | | -- sqrt => sqrt, |
144 | | -- (err) => { throw err }, |
145 | | -- ), |
146 | | -- (err) => { throw err }, |
147 | | -- ), |
148 | | -+ r.wrap(() => { |
149 | | -+ let _ratio = unwrap(div(x, y)) |
150 | | -+ let _ln = unwrap(ln(_ratio)) |
151 | | -+ return sqrt(_ln) |
152 | | -+ }), |
153 | | -+ (value) => value, |
154 | | - (err) => { throw err }, |
155 | | - ) |
156 | | -``` |
157 | | - |
158 | | -## License |
159 | | - |
160 | | -[MIT](../../LICENSE.md) |
| 1 | +https://std.moeru.ai/docs/packages/results |
0 commit comments