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

rustty

v2.1.3

Published

A package that brings the iconic Rust types such as Result and Option

Downloads

19

Readme

Rustty

Rustty is a library that brings various data structures and algorithms such as iconic Result and Option from the Rust standard library to the JavaScript world.


If you like this project, please consider leaving a star on the GitHub repository

Installation

npm install rustty

Usage

You can either import the whole library or just the Result and Option types.

import "rustty";

Will import only the Result and Option types. On the other hand, if you want to import the whole library, you can do by using the following statement:

import "rustty/full";

Note that using the full version will add a lot of methods to the prototypes of the built-in types such as Array.

Examples

Using Result and Option

import "rustty";

function divide(a: number, b: number): Result<number, string> {
  if (b === 0) {
    return Err("Division by zero");
  }

  return Ok(a / b);
}

const result = divide(10, 5); // Ok(2)

console.assert(result.isOk() === true); 
console.assert(result.unwrap() === 2);

const failed = divide(10, 0); // Err("Division by zero")

console.assert(failed.isErr() === true);
console.assert(failed.unwrapErr() === "Division by zero");

// Alternatively, you can use Option type to indicate that the result may be null or undefined

function divide(a: number, b: number): Option<number> {
  if (b === 0) {
    return None;
  }

  return Some(a / b);
}

const result = divide(10, 5).unwrap(); // 2
const failed = divide(10, 0).unwrap(); // Throws an error

Using additional methods

import "rustty/full";

const arr = [1, 2, 3, 4, 5];

// Swap elements at indices 0 and 4
arr.swap(0, 4);

arr.windows(2).forEach(window => {
  console.log(window); // [5, 2], [2, 3], [3, 4], [4, 1]
});

console.assert(arr.sum() === 15);
console.assert(arr.product() === 120);

arr.swap(0, 4);

arr
  .groupBy((a, b) => Math.round(a / 2) === Math.round(b / 2))
  .forEach(group => console.log(group)); // [1, 2], [3, 4], [5]

console.assert(arr.max() === 5);
console.assert(arr.min() === 1);

Features

Note that every name has been changed to camelCase to be consistent with the JavaScript naming convention.

  • [✅] Result with most of its methods (Global scope)
  • [✅] Option with most of its methods (Global scope)
  • [✅] Most of the methods from Vec (Array) (drain_filter -> spliceFilter)
  • [✅] Most of the methods from Slice (Array)
  • [✅] Most of the methods from Iterator (Array)
  • [❌] Most of the methods from String
  • [❌] Most of the methods from HashMap
  • [❌] Most of the methods from HashSet
  • [❌] Most of the methods from Numbers

If you can't find a method that you need, feel free to open an issue or a pull request.

License

This project is licensed under the MIT License - see the LICENSE file for details