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

rust-result-js

v1.2.0

Published

Implementation of Rust's Result object used for error handling

Downloads

3

Readme

rust-result-js

rust Result object implemented in javascript.

Table of Contents

Installation

via npm⚡:

$ npm i rust-result-js

or yarn⚡:

$ yarn add rust-result-js

or pnpm⚡:

$ pnpm add rust-result-js

Usage

This package contains 2 implementations- with createResult and without. Its error handling is identical to rust's: expect, unwrap, map...

However, since Javascript's convention is not snake-case, some methods differ by their name - all those with snake case were converted to camelCase.

examples:

  • map_err = mapErr in js
  • is_ok = isOk in js
  • unwrap_err = unwrapErr in js
  • etc...

Example

Unlike rust, in javascript, in order to return a value you must use the return keyword.

import { Ok, Err } from 'rust-result-js';

const toNumber = (str) => {
    const parsed = +str;
    if (!parsed && parsed !== 0) return Err("could not parse");
    return Ok(parsed);
}

const six = toNumber("6").expect("failed parsing 6");
console.log(`formatted number is ${six}`);

// throws error: 
// panics with expect() - failed parsing someInput : could not parse
const someInput = toNumber("not a number").expect("failed parsing someInput");

Using createResult

For those who HATE using return:

import { createResult } from 'rust-result-js';

const toNumber = str => createResult((ok, err) => {
    const parsed = +str;
    if (!parsed && parsed !== 0) err("could not parse");
    ok(parsed);
})

const four = toNumber("4").expect("failed parsing 4");
console.log(`formatted number is ${four}`);

This way it looks a lot more like the original rust syntax, but overall, it seems less aesthetic.

Note: when using createResult, you shouldn't use return with ok and err.

Typescript

This package also supports typescript, as it introduces the Result abstract class

import {Ok, Err, Result} from 'rust-result-js';

const fetchSomething = async (): Result<string[], string> => {
    try {
        const todosFetch = await fetch('http://something:1234/todos');
        const todos: string[] = await todosFetch.json();
        return Ok(todos);
    } catch (e) {
        return Err("error fetching todos");
    }
}

Available functions

NOTE: available functions API is identical to rust's original implementation.

This is what implemented so far in this library:

API References

isOk

function isOk(): boolean{/*...*/}

returns true if the Result is Ok, else returns false.

const x = Ok(5);
const y = Err('oops! something went wrong');

console.log(x.isOk()) // true
console.log(y.isOk()) // false

isErr

function isErr(): boolean{/*...*/}

Opposite of isOk, returns true if Result is Err, else returns false

const x = Ok('im Ok!');
const y = Err('oops! something went wrong');

console.log(x.isErr()) // false
console.log(y.isErr()) // true

expect

function expect(msg: string): T{/*...*/}

where T - type of Ok value

Returns the contained Ok value. If Err, throws error with msg including the errorValue.

const x = Err("emergency failure");
const val = x.expect("testing expect");
//output: panics with expect() - testing expect : emergency failure.
const x = Ok(6).expect("error with x"); // x = 6
console.log(x) //output: 6

expectErr

function expectErr(msg: string): K{/*...*/}

where K - type of Err value

Returns the contained Err value. If Ok, throws error with msg including Ok's value

const x = Ok(6).expectErr("error with x");
//output: [Error] panics with expectErr() : error with x : 6 
const x = Err("my error value").expectErr("testing expect");
console.log(x) // output: "my error value"

unwrap

function unwrap(): T{/*...*/}

where T type = Ok value's type

Returns the contained Ok value, throws if the value is an Err.

const x = Err(3);
x.unwrap();
// this will throw an error
const x = Ok(3);
console.log(x.unwrap())
//output: 3

unwrapErr

function unwrapErr(): K{/*...*/}

where K is Err value's Type

Returns the contained Err value, throws if the value is an Ok.

map

function map<T1>(fn: (val: T) => T1): Result<T1, K>{/*...*/}

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

const three = Ok('3');
const threeAsNumber = three.map(val => parseInt(val));

console.log(typeof val.unwrap());
//output: number

mapErr

function mapErr<K1>(fn: (err: K) => K1): Result<T, K1>{/*...*/}

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

const errMsg = Err('this is just a message')
const decoratedError = errMsg.mapErr(val => `[ERROR]: ${val}`);
console.log(decoratedError.unwrapErr());
//output: [Error]: this is just a message

mapOr

function mapOr<T1>(defaultValue: T1, fn: (val: T) => T1): T1{/*...*/}

where T1 new Ok value's type

Returns the provided default (if Err), or applies a function to the contained value (if Ok).

const weirdFetch = async () => {
    await sleep(1000);
    const shouldCrash = Math.random() < .5;
    if (shouldCrash) {
        return Err(null)
    }
    return Ok({
        name: 'Deez Nuts',
        id: '666666666'
    });
}

const fetchedPerson = await weirdFetch();
const person = fetchedPerson.mapOr(
    {lname: '', fname: '', id: '0'},
    person => {
        const [fname, ...lname] = person.name.split(' ');
        return {
            id: person.id,
            fname,
            lname: lname.join(' '),
        }
    }
)

console.log(person)
//output: 
// in case of Err: 
//  {lname: '', fname: '', id: '0'}
// in case of Ok:  
//  {lname: 'Nuts', fname: 'Deez', id: '6666666666'}

contains

function contains(value: T): boolean{/*...*/}

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

NOTE: It uses Lodash.isEqual to compare deep between objects.

const x = Ok({a: {b: 3}});

console.log(x.contains({a: {b: 3}})) // returns true

containsErr

function containsErr(errValue: K): boolean{/*...*/}

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

NOTE: It uses Lodash.isEqual to compare deep between objects.

const x = Err({a: {b: 3}});

console.log(x.containsErr({a: {b: 3}})) // returns true

Contributions

First, if you read this far you deserve congratulations.

Every contributor is welcome. There is a need to keep implementing more functions provided by rust's Result Object (at least those which we can implement).

Code Structure

The Ok and Err are basically wrappers for 2 classes, both of which are inheriting from an abstract class Result<T,K>.

Should you choose to add more functionality, please do the following:

  1. Add abstract function to Result<T,K>
abstract class Result<T, K> {
    abstract myFucntion()
}
  1. Implement it in both OkResult and ErrResult classes respectively.

Branch Convention

For writing features - please branch out from dev under feature/ (e.g. feature/contains_or)

For fixing bugs - please branch out from dev under bugfix/ (e.g. bugfix/create-result-for-async)