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-ts

v6.0.3

Published

Rusty TypeScript

Downloads

16

Readme

rust-ts

NPM Build Docs

TypeScript implementations of Rust std modules and some rust-like wrappers.

This doc only covers basic usage examples, check the documentation for all methods and details.

Documentation

Contents

Installation

# npm
npm i rust-ts
# yarn
yarn add rust-ts
# pnpm
pnpm i rust-ts

Key differences

All _then/_else callback variants have been combined with their value counterparts. For example, if you wanted to do:

Some(42).unwrap_or_else(() => 5);

You can simply do:

Some(42).unwrap_or(() => 5);
Some(42).unwrap_or(5);

match and if let implementations are also a bit different.

Rust:

let num = Some(42);
if let Some(x) = num {...}

let res = Ok(10);
match res {
    Ok(x) => {...},
    Err(e) => {...},
};

rust-ts equivalent:

let option = Some(42);
option.some(x => {...});
// Also possible with Result values
let result = Ok(42);
result.ok(x => {...});

let res = Ok(10);
res.match({
    ok: x => {...},
    err: e => {...},
});

Methods with callbacks have async variants with the syntax {name}_async, allowing passing async callbacks or Promise values.

Example:

const add_one_async = async (x: number) => x + 1;
const result = await Some(1).map_async(add_one_async); // result = Some(2)

Modules

Result

Result<T, E> is the type used for returning and propagating errors. It is an union type with the variants, Ok<T, E>, representing success and containing a value, and Err<E>, representing error and containing an error value.

type Result<T, E> = Ok<T, E> | Err<T, E>;
Usage
import {Ok, Err, Result} from "rust-ts";

const divide = (x: number, y: number): Result<number, string> =>
    y === 0 ? Err("Can't divide by zero") : Ok(x / y);

divide(10, 5)
    .and(z => divide(z, 2))
    .map(z => [z, z + 1])
    .match({
        ok: ([v, x]) => console.log(v + x),
        err: e => console.log(e),
    });

Option

Option<T> represents an optional value: every Option is either Some and contains a value, or None, and does not.

type Option<T> = Some<T> | None<T>;
Usage
import {Some, None, Option, match} from "rust-ts";

const divide = (x: number, y: number): Option<number> => (y === 0 ? None() : Some(x / y));

const result = divide(2.0, 3.0);

match(result, {
    some: x => console.log(x),
    none: () => console.log("Cannot divide by zero"),
});

match

match and match_async are available as instance methods for Result and Option.

Usage
import {Some, None, Option, Err, Ok, Result} from "rust-ts";

const add = (x: number, y: number): Option<number> => (y === 0 ? None() : Some(x + y));

const divide = (x: number, y: number): Result<number, string> =>
    y === 0 ? Err("Cannot divide by zero") : Ok(x + y);

const option = add(3, 4);
option.match({
    some: x => console.log(x),
    none: () => console.log("none"),
});

const result = option.ok_or("error");
result.map(x => x + 1).and(x => divide(x, 2));
result.match({
    ok: x => console.log(x),
    err: e => console.log(e),
});

fetch

fetchr is a wrapper around fetch that returns a Promise<Result<T, E>> with the data or error values instead of a Promise<Response>.

It also resolves the json, text, formData, or blob accordingly based on the content-type, returning the raw response if no match.

Usage
const url = "https://yourapiurl.com";
const res = await fetchr<ExpectedType, CustomErrorType>(url); // Optional generics for expected types, defaults to <unknown, Error>

// Fallback data with `unwrap_or`
const data = res.unwrap_or(FALLBACK_DATA);

// Match to handle each case
res.match({
    ok: data => /* do something with data */,
    err: err => /* handle errors */,
});

Contributions

Are welcome!

Refer to the docs for more details and examples.