|
| 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 | + let 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 | +#### Wrap |
| 91 | + |
| 92 | +With the `wrap` method, you can achieve something like the question mark operator in Rust. |
| 93 | + |
| 94 | +```diff |
| 95 | +const op = (x: number, y: number): number => |
| 96 | + r.match( |
| 97 | +- div(x, y), |
| 98 | +- radio => r.match( |
| 99 | +- ln(radio), |
| 100 | +- ln => r.match( |
| 101 | +- sqrt(ln), |
| 102 | +- sqrt => sqrt, |
| 103 | +- (err) => { throw err }, |
| 104 | +- ), |
| 105 | +- (err) => { throw err }, |
| 106 | +- ), |
| 107 | ++ r.wrap(() => { |
| 108 | ++ let _ratio = unwrap(div(x, y)) |
| 109 | ++ let _ln = unwrap(ln(_ratio)) |
| 110 | ++ return sqrt(_ln) |
| 111 | ++ }), |
| 112 | ++ (value) => value, |
| 113 | + (err) => { throw err }, |
| 114 | + ) |
| 115 | +``` |
| 116 | + |
| 117 | +## License |
| 118 | + |
| 119 | +[MIT](../../LICENSE.md) |
0 commit comments