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

rusultts

v2.0.0

Published

Rust Result Implementation for Typescript, simply. i.e. Modern error handling library.

Downloads

3

Readme

rusultTs

Rust Result Implementation for Typescript, simply. i.e. Modern error handling library. (no dependencies, pure Typescript code about 200 lines) 100% [coverage]

Coverage lines Coverage functions Coverage branches Coverage statements

NPM Version NPM Downloads CI License run on repl.it [changelog]


Installation

npm install rusultts

or

yarn add rusultts

Examples

import { Result, Ok, Err } from 'rusultts';

// Result<T>: any type can be into it,
// This is just the generic type.

// I chose <T> as object.

type SomeType = { foo: string, bar: number };

// Also these are just some example functions.

function tryParse(token: string): Result<SomeType> {

  // ... doing heavy stuffs ...

  if (somethingWrong) { // happended

    // so returns an Error-Object implementing Result<T>

    return Err.new(`something wrong...`, null);

  }

  // Or returns an Ok Object containing value for <SomeType>

  return Ok.new({ foo: 'any', bar: 999 });

}

// tryParse() wrapping function

function verify(token: string): Result<boolean> {

  // ↓ automatic throwing new Error(), or retuns the <SomeType> directly.

  const someType = tryParse(token).unwrap();

  // ... doing more stuffs ...

  // another unwrap

  const isItGood = tryGetBool(...).unwrap();

  // ...

  return Ok.new(isItGood);
}

try {

  // if unwrap is possible, you get a sign that you can use an obvious try catch statement.

  const bool = verify(someToken).unwrap();

} catch(e) {

  // ↓ can get [`string`, 'E | null'] type value.

  const [msg, value] = Err.eSplit(e);

  // message of error & contained value<E>
  // this value is `null` because of Result<T> = ResultBox<T, null>

  console.log(msg, value);

}

ResultBox

type Result<T> = ResultBox<T, null>;
import { ResultBox, Ok, Err } from 'rusultts';

// simple example
// ResultBox<T, E>: <E> equals containing user value for Error statement. it can be any type.

function divide(a: number, b: number): ResultBox<number, number> {
  if (b === 0) {
    return Err.new(`b cannot be `, b);
  }
  return Ok.new(a / b);
}

const val = divide(4, 2).unwrap(); // 4 / 2 = 2
const err = divide(4, 0); // 4 / 0, so error statement.

console.log(err.isErr); // true

// returns contained value<number> = 0
const getValueE = err.unwrap_err();

// if state is error, returns input value = 10
const getDefault = err.unwrap_or(10);

// like .map((x) => y) for value<E>
// ↓ will return 1
const getMapped = err.unwrap_or_else((eV: number) => eV + 1);

try {
  err.unwrap();
} catch (e) {
  const [errMessage, valueE] = Err.eSplit(e);

  // print `b cannot be :--> -1` out.
  console.log(errMessage, (-1 + valueE) as number);
}

Advanced

./errors.ts

import { createErrorSet } from 'rusultts';

// you can easily set all errors.

export default createErrorSet({
  notFound: 'not found',
  somethingWrong: 'something wrong...',
  wrongHeader: 'please fix your header.',
  undefinedValue: 'this value is undefined:',
  dividedByZero: 'do not divide by Zero.',
  dividedByNegative: 'well, you did divide as Negative value.',
});
import { ResultBox, Ok, Err } from 'rusultts';

import err from './errors'; // import errors

function divide(a: number, b: number): ResultBox<number, number> {
  if (b === 0) {
    return err.new('dividedByZero', b); // autocompleted string argument
  } else if (b < 0) {
    return err.new('dividedByNegative', b);
  }
  return Ok.new(a / b);
}

try {
  divide(4, -2).unwrap(); // dividedByNegative error occurs.
} catch (e) {
  // you can do error type matching.
  const val1 = err.match(e, 'dividedByZero').unwrap(); // this will return undefined.
  const val2 = err.match(e, 'dividedByNegative').unwrap(); // this will return value of number type, `-2`
  const val3 = err.match({ is: 'not errorType' }, 'dividedByNegative').unwrap(); // throw new Error
}

License

MIT