Option
Modelling optional values without null checks.
package @smitejs/fp
Option<Value> represents a value that may or may not be present. It is some (a present value) or none (absent), and it removes if (value != null) branching from your pipelines by carrying the check inside the API.
Constructing
Option.some(value)— an explicit present value.Option.none()— an explicit absence.Option.fromNullable(value)— mapsnull/undefinedtonone, anything else tosome. This is the usual entry point.
Transforming
.map, .flatMap, and .filter operate only on present values and short- circuit on none. .tap runs a side effect only when present and returns the same option.
Reading
.isSome() / .isNone() branch explicitly. .unwrapOr(fallback) returns the value or the fallback, and .unwrapOrElse(fn) defers computing the fallback until it is needed.
ts
const name = Option.fromNullable(" Ada ")
.map((value) => value.trim())
.filter((value) => value.length > 0)
.unwrapOr("anonymous");ts
"Ada"
"anonymous",tested in packages/fp/src/index.test.ts:72