Git Product home page Git Product logo

rusty-result.js's Introduction

Rusty-Result.js

npm version npm bundle size

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
npm install --save '@urbdyn/rusty-result'
# Deno
echo 'export { Result } from "https://deno.land/x/rusty_result/mod.ts";' >> ./deps.ts

Example Usage

// Node.js
import { Result } from '@urbdyn/rusty-result';
// Deno
import { Result } from 'https://deno.land/x/rusty_result/mod.ts';

// Create an ok Result
const myOkResult = Result.ok(1);
// Create an error Result
const myErrorResult = Result.error(2);

// Create a function that takes Results
function myResultFunction(r: Result<number, number>): void {
  // Perform operation on ok value and get new Result
  const r2 = r.map((x) => x + 10);
  // Perform operation on ok value and on error value and get new Result
  const r3 = r.map((x) => x + 10).mapError((x) => x - 10);

  // Do action if Result is ok
  if (r3.isOk()) {
    console.log(`r3 is ok with value of: ${r3.unwrap()}`);
  }
  // Do action if Result is error
  if (r3.isError()) {
    console.log(`r3 is error with value of: ${r3.unwrapError()}`);
  }
}

// Use Result with a function
myResultFunction(myOkResult); // prints: r3 is ok with value of: 11
myResultFunction(myErrorResult); // prints: r3 is error with value of: -8

// Check docs for all supported functions for working with Result values!

Supported Environments

Environment Versions
Deno v1.17.0 and v1.21.1
Node.js 12.22.12, 14.19.2, and 16.15.0

rusty-result.js's People

Contributors

code-ape avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.