rust-option
brings Option / Result / match from Rust to Javascript
Install
npm i -S rust-option
Usage
Nearly all methods are similar to the Option and Result
this lib will not implement all methods from Rust's Option and Result, see Note
let x = let y = Nonelet z = let w = // Note: matches are exhaustivelet ret = // ret is 'match and get the Some(2)'
See more examples in Test and Examples.
Test
npm test
match
function
The match
function provides match syntax similar to Rust, but in JavaScript way.
equal(===) considered to be match
handle type match
more matches
value | match |
---|---|
1 | 1, Number |
NaN | NaN, Number |
'yeah' | 'yeah', 'ea', String |
false | false, Boolean |
function f(){} | Function |
new Date('2000-01-01') | new Date('2000-01-01'), Date |
[1,2,4] | Array |
/foo/ | RegExp |
new Set | Set |
new Map | Map |
new WeakMap | WeakMap |
new WeakSet | WeakSet |
Symbol.iterator | Symbol |
arguments | Arguments |
new Error | Error |
{a:1, b:2 } | object, {a: 1}, {a: Number} |
Some(1) | Some(1),Some |
Ok(1) | Ok(1),Ok |
Err(1) | Err(1),Err |
None | None |
Some({a:1, b:2 }) | Some(object),Some({a: 1}),Some |
given | value | match |
---|---|---|
class A {} | new A | A |
class B extends A {} | new B | A,B |
non-exhaustive match throws Error
// this will throws 'non-exhaustive patterns' Error
default branch can get the matchee
handle nested match
let z = let w =
the ?
use .$
instead of ?
$ === 1 // similar to Ok(1).unwrap() and Ok(1)? try $ catch err errmessage === 'I am Err' $ === 1 // similar to Some(1).unwrap() and Some(1)? try None$ catch err err instanceof NoneError
more helper functions
makeMatch
makeMatchbranches:T | , deep:boolean=false:T;
actually,
is
x
equal
so you can not just use ===
or ==
to compare Option and Result, but the Option and Result itself provides an API call 'equal' for that.
// equal // deepEqual
optionify / resultify
await a function and make its output or Error to be Option or Result.
const content = await
optionifySync / resultifySync
wrap a function and make its output or Error to be Option or Result.
const content = './f'
deepEqual
deepEqual in Javascript is not a piece of cake.
deepEqual in this lib is provided by lodash.isequal.
let ox = let oy = ox // falseox // true
Examples
an examples with React on StackBlitz.
Note
Unlike Rust, JavaScript doesn't have the 'Ownership' feature, so some API like 'as_ref ' is not necessary.
implement for Option
- isSome
- isNone
- expect
- unwrap
- unwrapOr
- unwrapOrElse
- map
- mapOr
- mapOrElse
- okOr
- okOrElse
- and
- andThen
- filter
- or
- orElse
- xor
- transpose
- ?
not implement for Option
as_refas_mutas_pin_refas_pin_mutiteriter_mutget_or_insertget_or_insert_withtakereplaceclonedunwrap_or_defaultderef
implement for Result
- isOk
- isErr
- ok
- err
- map
- mapOrElse
- mapErr
- and
- andThen
- or
- orElse
- unwrapOr
- unwrapOrElse
- unwrap
- expect
- unwrapErr
- expectErr
- transpose
- ?
not implement for Result
as_refas_mutiteriter_mutunwrap_or_defaultderef_okderef_errderef
License
MIT