Rust options for typescript
Here i'm using pnpm
but you can use any package manager you prefer.
$ pnpm i typescript-option -S
❓ Helper functions to help you deal with options
- wrap
- some
- none
⚡ All functions you can call on an option
- unwrap
- unwrap_or
- unwrap_or_else
- run
- transform
- map
- map_or
- map_or_else
📌 All types available
- Option
- Some
- None
🚀 Checkout some examples
-
unwrap_or_else: Uwraps an
Option<T>
intoT
or, a default function that returnsT
-
transform: Used to call functions that will apply a transformation to an
Option<T>
-
map: Used to call functions that takes the internal type of an
Option<T>
and returns anOption<U>
-
map_or: Used to call functions that takes the internal type of an
Option<T>
and returns anOption<U>
or returnU
, ifOption<T>
wasNone
-
map_or: Used to call functions that takes the internal type of an
Option<T>
and returns anOption<U>
or call an funtion returningU
, ifOption<T>
wasNone
-
Option: A type to that represent either the existance of something or nothing
-
Some: A type to that represent some value
-
None: A type to that represent nothing
function getUserPlan(userId: string): Option<UserPlan> { ... }
function getJwtPayload(token: string): Option<JwtPayload> { ... }
const token = wrap(cookies.get("access-token"))
const plan = token
.map(getJwtPayload)
.map((payload) => getUserPlan(payload.userId))
.run(sendUserPlanEmail)
.unwrap()
// Plan will be either Some<UserPlan> or None