Giter Site home page Giter Site logo

rusty-ts's Introduction

rusty-ts

Build Status Coverage Status npm version Downloads

Rust-inspired Option and Result types for TypeScript.

Usage

npm install --save rusty-ts

Option

import { Option, option } from "rusty-ts";

const a = option.some("foo");
const b = a.map(x => x.toUpperCase());

const c = option.some(3);
const d = a.andThen(a => c.map(c => a.repeat(c)));

function f(opt: Option<string>) {
  console.log(opt.unwrap());
}

// Logs "FOOFOOFOO"
f(d);

Result

import { Result, result } from "rusty-ts";

const a = result.ok("foo");
const b = a.map(x => x.toUpperCase());

const c = result.err(3);
const d = c.orElse(cError => b.map(bVal => bVal.repeat(cError)));

function f(opt: Result<string>) {
  console.log(opt.unwrap());
}

// Logs "FOOFOOFOO"
f(d);

Why Option and option (or Result and result)?

Option is just an interface—any Option-compatible code you write will be compatible with any implementation of Option. This gives you the flexibility to implement Option however you like.

However, you probably don't want to write your own implementation, so we provide you with one out-of-the-box. The option object provides a namespace that groups the factories of the default implementation. To instantiate an Some or None variant, simply call option.some() or option.none(), respectively.

The same goes for Result (the interface), and result (the namespace containing the factory functions).

API Docs

Docs can be found here.

rusty-ts's People

Contributors

kylejlin avatar

Watchers

 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.