Package Exports
- @binarymuse/ts-stdlib
- @binarymuse/ts-stdlib/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@binarymuse/ts-stdlib) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
ts-stdlib
@binarymuse/ts-stdlib is a set of classes, utilities, and types to make working with TypeScript a little bit nicer. These concepts can be found in many different languages, although many of the implementations here are inspired by Rust.
The library includes:
Option<T>- a type that represents a value (Some<T>) or the absense of one (None)Result<T, E>- a type that represents a successful result (Ok<T>) or an err (Err<E>)Rc<T>- a reference counted resource
Installation
npm install @binarymuse/ts-stdlib
# or
pnpm add @binarymuse/ts-stdlib
# or
yarn add @binarymuse/ts-stdlibOption<T>
An Option<T> has two variants:
Some<T>, representing the existance of the inner valueNone, representing the absense of an inner value
Creating an option
Some(value: T): Option<T>- create aSomefrom a value; note that callingSome(undefined)orSome(null)will returnNoneNone- reference to the singletonNonevalue
Querying the inner value
Option<T>.isSome(): booleanReturns
trueif the option isSome,falseotherwiseOption<T>.isSomeAnd(fn: (value: T) => boolean): booleanReturns
trueif the option isSomeand callingfnwith the inner value returnstrue,falseotherwiseOption<T>.isNone(): booleanReturns
trueif the option isNone,falseotherwiseOption<T>.isNoneOr(fn: (value: T) => boolean): booleanReturns true if the option is
Noneor callingfnwith the inner value returnstrue,falseotherwiseOption<T>.unwrap(): TReturns the underlying value if the option is
Some, otherwise throws an exceptionOption<T>.expect(msg: string): TReturns the underlying value if the option is
Some, otherwise throws an exception with a custom messageOption<T>.unwrapOr(default: T): TReturns the underlying value if the option is
Some, otherwise returnsdefaultOption<T>.unwrapOrElse(fn: () => T): TReturns the underlying value if the option is
Some, otherwise callsfnand returns its return valueOption<T>.filter(fn: (T) => boolean): Option<T>Returns
Noneif the option isNone, otherwise callsfnwith the inner value and returns:Some<T>with the original wrapped value iffnreturns trueNoneiffnreturns false
Option<T>.match<U>(cases: { some: (value: T) => U; none: () => U }): UReturns the result of calling
cases.some()with the inner value if the option isSome, otherwise returns the result of callingcases.none()Option<T>.equals(other: Option<T>): booleanReturns true if both options are
Someand their inner values are equal using the JavaScript==operator, or if both options areNone. As a special case, if both options areSomeand their inner values are alsoSome, their inner values are compared withequals().Option<T>.strictEquals(other: Option<T>): booleanReturns true if both options are
Someand their inner values are equal using the JavaScript===operator, or if both options areNone. As a special case, if both options areSomeand their inner values are alsoSome, their inner values are compared withstrictEquals().
Transforming options
Option<T>.map<U>(fn: (value: T) => U): Option<U>Returns an
Option<U>by mapping the inner value of the source option withfnOption<T>.mapOr<U>(defaultValue: U, mapFn: (value: T) => U): Option<U>Returns an option wrapping the provided
defaultValueif the option isNone, or callsmapFnwith the inner value and returns a new option wrapping its return valuemapOrElse: <U>(defaultFn: () => U, mapFn: (value: T) => U): Option<U>Returns an option wrapping the return value of
defaultFnif the option isNone, or callsmapFnwith the inner value and returns a new option wrapping its return valueOption<T>.and<U>(other: Option<U>): Option<U>Returns
Noneif the source option isNone, otherwise returnsotherOption<T>.andThen<U>(fn: (value: T) => Option<U>): Option<U>Returns
Noneif the source option isNone, otherwise callsfnwith the inner value and returns the result.Option<T>.or(other: Option<T>): Option<T>Returns the source option if it is
Some, otherwise returnsotherOption<T>.orElse(fn: () => Option<T>): Option<T>Returns the source option if it is
Some, otherwise callsfnand returns the resultOption<T>.xor(other: Option<T>) => Option<T>Returns the source option or
otherif exactly one of them isSome, otherwise returnsNoneOption<T>.flatten(): Option<T>Converts from
Option<Option<T>>toOption<T>. Only one level of nesting is removed.