Giter Site home page Giter Site logo

option-ts's Introduction

Option monad for Typescript

Yet another port of the Scala Option type to TypeScript.

npm (tag)

npm i --save option-monad-ts

Usage

Basic Example

import { Option, Some, None } from 'option-monad-ts';

// Create Some 
let n = new Some(3);
let m = Some.of('four');

// Create Option
let c = Option.of(3);
let d = Option.of(null);

// Validate and use
n.get();                    // 3
m.get();                    // 'four'

c.isDefined();              // true
d.isDefined();              // false

Some.of(4)
    .map(_ => _.toString())
    .forEach(_ => console.log(_))

Documentation

Option<T>

static of<T>(x: T): Option<T>

An Option factory which creates Some.of(x) if the argument is not null, and None if it is null.

filter(p: (i: T) => boolean): Option<T>

Returns this Option if it is nonempty and applying the predicate p to this Option's value returns true. Otherwise, return None.

flatMap<R>(f: (i: T) => Option<R>): Option<R>

Returns the result of applying f to this Option's value if this Option is nonempty. Returns None if this Option is empty. Slightly different from map in that f is expected to return an Option (which could be None).

forEach<U>(f: (i: T) => U): void

Apply the given procedure f to the option's value, if it is nonempty. Otherwise, do nothing.

get(): T

Returns the option's value. The option must be nonEmpty.

getOrElse(defaultValue: T): T

Returns the option's value if the option is nonempty, otherwise return defaultValue.

isDefined(): boolean

Returns true if the option is an instance of Some, false otherwise.

isEmpty(): boolean

Returns true if the option is None, false otherwise.

map<R>(f: (i: T) => R): Option<R>

Returns a Some containing the result of applying f to this Option's value if this Option is nonempty. Otherwise, return None.

Why another library?

  • I thought it would be a good exercise
  • I didn't like the API/usage patterns of some existing libraries
  • I wanted something as close to the Scala lib as possible, so I ported it trying to keep to its spirit

option-ts's People

Contributors

actions-user avatar sdedovic avatar

Stargazers

 avatar

Watchers

 avatar  avatar  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.