Rusty-Result.js
Zero dependency Typescript implementation of Rust's Result avaible via NPM or Deno Land!
Rusty-Result aims to be a Typescript/Javascript interpretation of Result from Rust.
View the generated docs for the library here.
npm install --save '@urbdyn/rusty-result'
echo 'export { Result } from "https://deno.land/x/rusty_result/mod.ts";' >> ./deps.ts
Example Usage
import { Result } from '@urbdyn/rusty-result';
import { Result } from 'https://deno.land/x/rusty_result/mod.ts';
const myOkResult = Result.ok(1);
const myErrorResult = Result.error(2);
function myResultFunction(r: Result<number, number>): void {
const r2 = r.map((x) => x + 10);
const r3 = r.map((x) => x + 10).mapError((x) => x - 10);
if (r3.isOk()) {
console.log(`r3 is ok with value of: ${r3.unwrap()}`);
}
if (r3.isError()) {
console.log(`r3 is error with value of: ${r3.unwrapError()}`);
}
}
myResultFunction(myOkResult);
myResultFunction(myErrorResult);
Supported Environments
Environment | Versions |
---|
Deno | v1.17.0 and v1.21.1 |
Node.js | 12.22.12 , 14.19.2 , and 16.15.0 |