happy-rusty
TypeScript icon, indicating that this package has built-in type declarations

1.0.9 • Public • Published

Use Rust features in JavaScript happily

NPM version JSR Version JSR Score Build Status


[中文]


Partial supported

More is coming

Installation

via pnpm

pnpm add happy-rusty

or via yarn

yarn add happy-rusty

or just from npm

npm install --save happy-rusty

via JSR

jsr add @happy-js/happy-rusty

for deno

deno add @happy-js/happy-rusty

for bun

bunx jsr add @happy-js/happy-rusty

then import to your code.

import { Some, None, Ok, Err } from 'happy-rusty';

Enjoy the happiness.

Examples

import { Some, None, Ok, Err } from 'happy-rusty';

function judge(n: number): Option<Promise<Result<number, Error>>> {
    if (n < 0 || n >= 1) {
        return None;
    }

    return Some(new Promise(resolve => {
        const r = Math.random();
        resolve(r > n ? Ok(r) : Err(new Error('lose')));
    }));
}

const res = judge(0.8);
if (res.isNone()) {
    console.error('invalid number');
} else {
    const result = await res.unwrap();
    if (result.isErr()) {
        console.assert(result.err().message === 'lose');
    } else {
        console.log(result.unwrap()); // must greater than 0.8
    }
}

Package Sidebar

Install

npm i happy-rusty

Weekly Downloads

30

Version

1.0.9

License

GPL-3.0

Unpacked Size

59.5 kB

Total Files

13

Last publish

Collaborators

  • jarvisjiang