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

crustyjs

v1.0.1

Published

A simple library to provide error as a value and a rust-like optionality type.

Downloads

51

Readme

crustyjs

A simple library to provide error as a value and a rust-like optionality type.
This library has been inspired by Rust's enums Result and Option.

Why?

The main motivation is to provide TypeScript developers with a sort of interface allowing to have errors as values (aka Result).
The second motivation is to avoid the confusion deriving from undefined and null by using "struct-like" type (aka Option).
For this last one, the idea is to simply avoid using undefined and null alltogether.

Should my project use it?

This small library aims at providing a different developer experience. If you don't like try-catch or the fact that JS provides two null "types", probably this library can help you. Another positive point is that this tiny library has zero dependencies, so your dependency tree won't suffer from it.

How to install?

Run the following shell command inside your project.

npm install crustyjs

Errors as values, the Result type.

How to instantiate a Result?

  1. Quick 'n dirty - By derivation, passing a function that throws:

    import { Result, toResult } from "crustyjs";
    
    function evenOrThrow(n: number): number {
      if (n % 2 === 0) return n;
      throw Error(`${n} is not even.`);
    }
    
    function evenOrErr(n: number): Result<number, Error> {
      return toResult(() => evenOrThrow(n));
    }
    
    // returns Ok<number>
    evenOrErr(0);
    
    // returns Err<Error>
    evenOrErr(1);
    
    // returns Ok<number>
    evenOrErr(2);

    The same can be done for async functions by using toResultAsync.

    Please note that it is also possible to provide as second parameter a type converter for the Err.
    If provided, the converter must accept the error as an unknown parameter and transform it into the desired type E.

  2. Slow 'n clean - By manually rewriting the functions:

    import { Result, err, ok } from "crustyjs";
    
    function evenOrErr(n: number): Result<number, string> {
      if (n % 2 === 0) return ok(n);
      return err(`${n} is not even.`);
    }
    
    // returns Ok<number>
    evenOrErr(0);
    
    // returns Err<string>
    evenOrErr(1);
    
    // returns Ok<number>
    evenOrErr(2);

How to consume a Result?

  1. Recommended - Using the built-in pattern matching:

    import { err, ok } from "crustyjs";
    
    // returns the string "Ok(ok branch gets executed)"
    ok("ok branch gets executed").match(
      (val) => `Ok(${val})`,
      (err) => `Err(${err})`
    );
    
    // returns the string "Err(err branch gets executed)"
    err("err branch gets executed").match(
      (val) => `Ok(${val})`,
      (err) => `Err(${err})`
    );

    A string is returned beacuse the provided handlers are returning a string.

  2. Risky-er - using unwrap and unwrapOr (latter is safe):

    import { err, ok, Result } from "crustyjs";
    
    const testOk: Result<number, string> = ok(5);
    const testErr: Result<number, string> = err("some error");
    
    // returns 5
    testOk.unwrapOr(3);
    
    // returns 3
    testErr.unwrapOr(3);
    
    // since testOk is Ok, returns 5
    if (testOk.isOk()) testOk.unwrap();
    
    // since testErr is Err, it never executes the unwrap.
    if (testErr.isOk()) testErr.unwrap();
    
    // since testErr is Err, returns the string "some error"
    if (testErr.isErr()) testErr.unwrapErr();
    
    // BAD UNWRAPS - the line below throws a type error because testErr is of type Err.
    testErr.unwrap();

    Please note that no method like "unwrapErrOr" exists.
    When using unwrap or unwrapErr, please always remember to check the type.

Optional values, the Option type (a replacement to null and undefined).

How to instantiate an Option?

  1. Quick 'n dirty - Just wrap whatever "optional" value by using option:

    import { Option, option } from "crustyjs";
    
    function evenOrNullable(n: number): number | undefined {
      if (n % 2 === 0) return n;
    }
    
    function evenOption(n: number): Option<number> {
      return option(evenOrNullable(n));
    }
    
    // Some<number>
    evenOption(0);
    
    // None
    evenOption(1);
    
    // Some<number>
    evenOption(2);
  2. Clean 'n expressful - use some and none:

    import { Option, some, none } from "crustyjs";
    
    function evenOption(n: number): Option<number> {
      if (n % 2 === 0) return some(n);
      return none();
    }
    
    // Some<number>
    evenOption(0);
    
    // None
    evenOption(1);
    
    // Some<number>
    evenOption(2);

How to consume an Option?

  1. Recommended - use the buil-in pattern matching:

    import { none, some } from "crustyjs";
    
    // returns the string "Some(2)"
    some(2).match(
      (val) => `Some(${val})`,
      () => `None`
    );
    
    // returns the string "None"
    none().match(
      (val) => `Some(${val})`,
      () => `None`
    );
  2. Risky-er - using unwrap and unwrapOr (latter is safe):

    import { none, some, Option } from "crustyjs";
    
    const testSome: Option<number> = some(5);
    const testNone: Option<number> = none();
    
    // returns 5
    testSome.unwrapOr(3);
    
    // returns 3
    testNone.unwrapOr(3);
    
    // since testSome is Some, returns 5
    if (testSome.isSome()) testSome.unwrap();
    
    // since testNone is None, it never executes the unwrap.
    if (testNone.isSome()) testNone.unwrap();
    
    // since testNone is None, it prints to the console the string "None".
    if (testNone.isNone()) console.log("None");
    
    // BAD UNWRAPS - the line below throws a type error because testNone is of type None.
    testNone.unwrap();