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

@stead08/optres

v0.1.6

Published

A library for optimizing error or undefined handling in JavaScript and TypeScript using Rust-like Result and Option types.

Downloads

1

Readme

OPT-RES

Error handling like rust in typescript

Description

This library provides a way to handle errors like rust in typescript.
There is a npm package with the same name, but I decided on the name of the package without knowing it existed. sorry!

I referenced these in the implementation of the library:

  • 【TypeScript】Result型を定義してエラーハンドリングを楽にする https://zenn.dev/junki555/articles/4ab67fc78ce64c
  • Option in std::option - Rust
    https://doc.rust-lang.org/std/option/enum.Option.html
  • Result in std::result - Rust
    https://doc.rust-lang.org/std/result/enum.Result.html

Usage

import {fetchWithResult, Optional, Result} from "@stead08/optres";

// divide function returns Optional<number>
function divide (a: number, b: number): Optional<number> {
    if (b === 0) {
        return Optional.none();
    }
    return Optional.some(a / b);
}

function test_divide_by_zero () {
    const result = divide(1, 0);
    result.when({
        some: (value) => console.log(value),
        none: () => console.log('error')
    });
}
test_divide_by_zero(); // error

// Using Optional<T> is useful when you want to return a value that may be null or undefined.
// you can assure that the value is not null or undefined by using Optional<T> with unwrap() method.
// Example:
const optional: Optional<number> = Optional.some(123);
const result: number = optional.unwrap();

// using when() method is useful when you want to handle both some and none cases.
// Example:
const optional2: Optional<number> = Optional.some(123);
const result2: number = optional2.when({
    some: (value) => value + 1,
    none: () => 0,
});

// you can also use Result<T, E> instead of Optional<T> for better error handling.
// Result<T, E> is a type that represents either success (Ok) or failure (Err).
// Result<T, E> is often used for error handling.
// Example:
function divide_returns_result (a: number, b: number): Result<number, Error> {
    if (b === 0) {
        return Result.err(new Error('divide by zero'));
    }
    return Result.ok(a / b);
}

function test_divide_by_zero_returns_result () {
    const result = divide_returns_result(1, 0);
    result.when({
        ok: (value) => console.log(value),
        err: (err) => console.log(err)
    });
}

test_divide_by_zero_returns_result(); // Error: divide by zero

// fetchWithResult returns Result<Response, Error>
async function fetchWithResultExample () {
    const response = await fetchWithResult(new URL('https://www.google.com'));
    const result = response.ok();
    console.log(result);
}