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

@cprussin/option-result

v1.3.0

Published

Yet another Typescript clone of the rust `Option` & `Result` enums.

Downloads

220

Readme

@cprussin/option-result - v1.3.0

This package contains yet another Typescript clone of the rust Option & Result enums.

Installing

Use the package manager of your choice to install:

  • npm: npm install --save-dev @cprussin/option-result
  • pnpm: pnpm add -D @cprussin/option-result
  • yarn: yarn add -D @cprussin/option-result

Usage

This library mostly implements an as-close-to-identical API to the Rust types as possible.

Example

import { Some } from "@cprussin/option-result";
Some(5)
  .map((value) => value + 1)
  .match({
    Some: (value) => console.log(value),
    None: () => console.error("There's nothing here!"),
  });

FAQ

Motivation

But why?

Two main reasons:

  1. I didn't find any existing implementations I found to be satisfactory. They either lacked the strong typing guarantees I wanted, or they were incomplete and unmaintained, or I simply didn't like their stylistic preferences. For some reason or another I wasn't happy with any of the other implementations I tried.
  2. It's fun. Type-safe Option and Result are really rewarding types to implement. If you haven't tried building them yourself, I strongly recommend it.

Why use Rust's Option and Result instead of Haskell's Maybe and Either?

I opted to implement the Rust versions of these types and not the Haskell/Purescript versions for a few reasons:

  1. There are no type classes in Typescript, so approximated Maybe and Either implementations would have way more differences from their source versions than Option and Result instances.
  2. I love Haskell and Purescript (and other ML family languages) personally, but I also believe those to be far less approachable languages than Rust. I think the Rust versions are easier to understand and teach and the terminology is easier to grok for newcomers, at the expense of some degree of generic code. I use this library practically myself and while I value type safety, I also value approachability from developers other than myself, and I believe the Rust versions strike a better balance there than the Haskell/Purescript ones do.
  3. Purescript compiles to Javascript; as such I don't believe there's much value in cloning types from that language when you could just as easily write parts of your application in Purescript directly instead and not have to deal with the shortcomings of using those types in a language not designed for them.

Table of contents

Classes

Functions

Functions

Err

Err<T, E>(error): Result<T, E>

Construct a Result containing an error value.

Type parameters

| Name | Type | Description | | :------ | :------ | :------ | | T | extends Object | the type of success values | | E | extends Object | the type of the error value |

Parameters

| Name | Type | Description | | :------ | :------ | :------ | | error | E | the error contained by the Result |

Returns

Result<T, E>

a Result containing an error error

See

Defined in

result.ts:598


None

None<T>(): Option<T>

Construct an empty Option.

Type parameters

| Name | Type | Description | | :------ | :------ | :------ | | T | extends Object | the type contained by the Option |

Returns

Option<T>

an empty Option

See

Defined in

option.ts:589


Ok

Ok<T, E>(value): Result<T, E>

Construct a Result containing a success value.

Type parameters

| Name | Type | Description | | :------ | :------ | :------ | | T | extends Object | the type of the value | | E | extends Object | the type of error results |

Parameters

| Name | Type | Description | | :------ | :------ | :------ | | value | T | the value contained by the Result |

Returns

Result<T, E>

a Result containing value

See

Defined in

result.ts:580


Some

Some<T>(value): Option<T>

Construct an Option containing a value.

Type parameters

| Name | Type | Description | | :------ | :------ | :------ | | T | extends Object | the type contained by the Option |

Parameters

| Name | Type | Description | | :------ | :------ | :------ | | value | T | the value contained by the Option |

Returns

Option<T>

an Option containing value

See

Defined in

option.ts:578