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

rustic

v1.2.2

Published

A package providing Rust types and helpers for Typescript

Downloads

3,236

Readme

rustic

rustic is a TypeScript library providing emulation of Rust's Option and Result types (and some useful wrappers for common js functions as well!).

The repo has 100% test code coverage, so you can be sure it will never fail! Even more in your production code™. (The not fail part is obviously a lie. Test your code.)



Installation

Just install as any other package:

$ npm i rustic

Usage

Result

  1. Let's suppose we you have a fallible function that'll return an error for random number lower than 5:
import { Result, Err, Ok, isOk } from 'rustic';

function fallible(): Result<number, string> {
	const zeroToTen: number = Math.random() * 10;

	// Using Err and Ok helper as a shorthand to produce Result
	if (zeroToTen < 5) {
		return Err("Lower than 5");
	} else {
		return Ok(zeroToTen);
	}
}

const res = fallible();

// Using isOk helper will do this for You, but You can also
// access the `__kind` field and compare it with `ResultKind` enum directly
if (isOk(res)) {
	// Typescript infers res.data's type as `number`
	console.log('Successful num sq:', res.data * res.data);	// Successful num sq: <number>
} else {
	console.log('Error:', res.data);	// 'Error: Lower than 5'
}
  1. Suppose you want to map the Result of a fallible function:
import { Result, equip, ResultEquipped } from 'rustic';

function fallible(): Result<number, string> { ... }

const res: Result<number, string> = fallible();

// Call `equip` with the Result of fallible function
const equipped: ResultEquipped<number, string> = equip(res);

// Use as You would Rust's Result
const squared: number = equipped.map(n => n * n).expect('Squared n');

// Using unwrap can cause a panic: `panicked at 'Squared n: "<err message>"'`

// You can access the underlying Result<number, string> using the `.inner` getter:
// `equipped.inner`;

console.log('Squared', squared);

Option

  1. Option follows the same methods as Result does:
import { Option } from 'rustic';

function returnsOption(): Option<number> { ... }

const res: Option<number> = returnsOption();

if (res == null) {
	console.log('Returned null');
} else {
	// Typescript can infer for sure, that the res is of type `number`.
	console.log('Returned num:', res * 2);
}
  1. Call equip with the optional variable to gain access to the full functionality:
import { Option, equip, OptionEquipped } from 'rustic';

function returnsOption(): Option<number> { ... }

const res: OptionEquipped<number> = equip(returnsOption());

const squared = res.map(n => n * n);

// Unwrap can lead to panics. You can still access the underlying Option<number>
// by using `.inner`: `squared.inner`
console.log('Sqared num:', squared.unwrap());

Wrappers

catchResult

import { catchResult } from 'rustic';

function throwsError(): void { throw new Error('1234') }

function doesNotThrowError(): number { return 5 }

const res1 = catchResult(throwsError);	// Err('Error: 1234')
const res2 = catchResult(doesNotThrowError);	// Ok(5)

parseJson

import { parseJson, Result } from 'rustic';

const parsed1: Result<number, string> = parseJson('5');	// Ok(5)

const parsed2: Result<number, string> = parseJson('{');	// Err('...')