npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

opt-res

v1.0.3

Published

Optional value and Result error handling types, heavily inspired by Rust.

Downloads

8

Readme

Replace null checks with the Option type, and elegantly handle optional data.

Replace exceptions with functional error handling using the Result type.

This library is heavily inspired by the excellent Option and Result enums from Rustlang.

Option

and: (optb: Option<T>) => Option<T>

Returns None if the option is None, otherwise returns optb.


andThen: <U>(map: map<T, Option<U>>) => Option<U>

Returns None if the option is None, otherwise calls map with the wrapped value and returns the result.


contains: <T2 extends T>(value: T2) => this is Option<T2>

Returns true if the option is a Some value containing the given value. Uses strict equality.


filter: (filter: filter<T>) => Option<T>

Returns None if the option is None, otherwise calls predicate with the wrapped value and returns:

  • Some<T> if predicate returns true (keeping the wrapped value), and
  • None if predicate returns false.

This function works similar to filtering an array. You can imagine the Option<T> being an iterator over one or zero elements. filter() lets you decide which elements to keep.


flatten: () => FlattenOption<T>

Converts from Option<Option<T>> to Option<T>.

Flattening only removes one level of nesting at a time:


isNone: () => this is _None<T>

Returns true if the option is a None value.


isSome: () => this is _Some<T>

Returns true if the option is a Some value.


isSomeAnd: (condition: filter<T>) => boolean

Returns true if the option is a Some and the value inside matches a predicate.


iter: () => T[]

Converts the option to an array

  • Array has length 1 and contains inner value if the option is a Some.
  • Array is empty if the option is a None value.

map: <U>(map: map<T, U>) => Option<U>

Maps an Option<T> to Option<U> by applying a function to a contained value.


okOr: <E>(err: E) => Result<E, T>

Transforms the Option<T> into a Result<T, E>, mapping Some<T> to Ok<T> and None to Err<E>.


or: (optb: Option<T>) => Option<T>

Returns the option if it contains a value, otherwise returns optb.


unwrap: () => T

Returns the contained Some value, consuming the self value.

Panics if the self value equals None. Because this function may panic, its use is generally discouraged.


unwrapOr: (fallback: T) => T

Returns the contained Some value or a provided default.


Result Type

and: (res: Result<E, T>) => Result<E, T>

Returns res if the result is Ok, otherwise returns the Err value of self.


andThen: <T2>(map: map<T, Result<E, T2>>) => Result<E, T2>

Calls map if the result is Ok, otherwise returns the Err value of self.


contains: <T2 extends T>(value: T2) => this is Result<E, T2>

Returns true if the result is an Ok value containing the given value. Uses strict equals.


containsErr: <E2 extends E>(err: E2) => this is Result<E2, T>

Returns true if the result is an Err value containing the given value. Uses strict equals.


err: () => Option<E>

Converts from Result<E, T> to Option<E>.


flatten: () => FlattenResult<E, T>

Converts from Result<E, Result<E, T>> to Result<E, T>.


isErr: () => this is _Err<E, T>

Returns true if the result is Err.


isOk: () => this is _Ok<E, T>

Returns true if the result is Ok.


iter: () => T[]

Converts the result to an array

  • Array has length 1 and contains inner value if the result is a Ok.
  • Array is empty if the result is a Err value.

map: <T2>(map: map<T, T2>) => Result<E, T2>

Maps a Result<E, T> to Result<E, T2> by applying a function to a contained Ok value, leaving an Err value untouched.


mapErr: <E2>(map: map<E, E2>) => Result<E2, T>

Maps a Result<E, T> to Result<E2, T> by applying a function to a contained Err value, leaving an Ok value untouched.


ok: () => Option<T>

Converts from Result<E, T> to Option<T>.


or: (result: Result<E, T>) => Result<E, T>

Returns res if the result is Err, otherwise returns the Ok value of self.


orElse: (map: map<E, Result<E, T>>) => Result<E, T>

Calls map if the result is Err, otherwise returns the Ok value of self.


reduce: <O>(errMap: map<E, O>, map: map<T, O>) => O

Calls map on the inner value of an Ok, or errMap on the inner value of an Err. map and errMap should produce the same output type.


unwrap: () => T

Returns the contained Ok value, consuming the self value.

Panics if the self value equals Err. Because this function may panic, its use is generally discouraged.


unwrapErr: () => E

Returns the contained Err value, consuming the self value.

Panics if the self value equals Ok. Because this function may panic, its use is generally discouraged.


unwrapOr: (arg: T) => T

Returns the contained Ok value or a provided default.


unwrapOrElse: (map: map<E, T>) => T

Returns the contained Ok value or computes it by calling map on the inner value of the Err.