Coder Social home page Coder Social logo

monapt's Introduction

Monapt npm version Build Status Join the chat at https://gitter.im/jiaweihli/monapt

The most current version of Monapt uses TypeScript 2.x.

TypeScript 1.x support is in maintenance mode on version 0.5.x.
It lives on the typescript-1.x branch.

What the heck are options anyway?

They're the cure for all your null/undefined problems. Read this article to learn more!

And tries?

You can also read up on tries, which provide a flexible way to deal with exceptions.

Setup

With npm:

$ npm install monapt

With Bower:

$ bower install git://github.com/jiaweihli/monapt.git --save

APIs

Monapt

Properties / Methods

  • static flatten<T>(options: Array<Option<T>>): Array<T>

Monapt.Option

var valueOption = Option(null);
var finalValue = valueOption.getOrElse(() => 'defaultValue'); // 'defaultValue'
valueOption
  .map((v) => v * 2)
  .filter((v) => v > 10)
  .match({
    Some: (v)  => 'Value: ' + v,
    None: () => 'Invalid value'
  })

Monapt.Some / Monapt.None

Monapt.Option('value') // Some('value')
Monapt.Option(null) // None
Monapt.Option(undefined) // None
Monapt.None // None
Monapt.None.get() // Monapt.NoSuchElementError
Monapt.flatten([Monapt.None, Monapt.Option(1)]) // [1]

Properties / Methods

  • isDefined: boolean
  • isEmpty: boolean
  • get(): T
  • getOrElse(defaultValue: () => T): T
  • orElse(alternative: () => Option<A>): Option<A>
  • match<B>(matcher: { Some: (T) => U, None: () => U }): U
  • map<B>(f: (value: T) => U): Option<U>
  • flatMap<B>(f: (value: T) => Option<U>): Option<B>
  • filter(predicate: (value: T) => boolean): Option<U>
  • reject(predicate: (value: T) => boolean): Option<U>
  • foreach(f: (value: T) => void): void
  • equals(option: Option<T>): boolean

Monapt.Try

var attempt = Monapt.Try(() => {
    return parse(aValue);
});
attempt.getOrElse(() => 'defaultValue');
attempt.match({
    Success: (v) => 'Awesome! ' + v,
    Failure: (e) => 'Whoops!' + e.message
});

Properties / Methods

  • exception: Error
  • isSuccess: boolean
  • isFailure: boolean
  • get(): T
  • getOrElse(defaultValue: () => T): T
  • orElse(alternative: () => Try<T>): Try<T>
  • match<U>(matcher: { Success: (T) => U, Failure: () => U }): U
  • map<U>(f: (value: T) => U): Try<U>
  • flatMap<U>(f: (value: T) => Try<U>): Try<U>
  • filter(predicate: (value: T) => boolean): Try<T>
  • reject(predicate: (value: T) => boolean): Try<T>
  • foreach(f: (value: T) => void): void
  • recover(fn: (error: Error) => T): Try<T>
  • recoverWith(fn: (error: Error) => Try<T>): Try<T>
  • toOption(): Option<T>

Monapt.Future

Monapt.future((promise) => {
  api.get((error, value) => {
    if (error) {
      promise.failure(error);
    }
    else {
      promise.success(value);
    }
  });
})
  .onComplete({
    Success: (v) => 'Awesome! ' + v,
    Failure: (e) => 'Whoops! ' + e.toString()
  })

Mix futures:

var macbook = Monapt.future((promise) => {
  setTimeout(() => {
    promise.success('MacBook');
  }, 100);
});

var pro = Monapt.future((promise) => {
  setTimeout(() => {
    promise.success('Pro');
  }, 100);
});

var macbookPro = macbook.flatMap((mb) => {
  return pro.map((pro) => {
    return mb + pro;
  });
});

macbookPro.onSuccess((v) => {
  console.log(v); // MacBookPro
});

Properties / Methods

  • onComplete(callback: { Success: (T) => void, Failure: () => void } => void): void
  • onSuccess(callback: (value: T) => void): void
  • onFailure(callback: (error: Error) => void): void
  • map<U>(f: (value: T) => U): Future<U>
  • flatMap<U>(f: (value: T) => Future<U>): Future<U>
  • filter(predicate: (value: T) => boolean): Future<T>
  • reject(predicate: (value: T) => boolean): Future<T>
  • recover(fn: (e: Error) => T): Future<T>
  • recoverWith(fn: (e: Error) => Future<T>): Future<T>

Credits

This repo couldn't have been possible without https://github.com/yaakaito/monapt. In his absence, I'll continue improving upon his hard work.

monapt's People

Contributors

afrieder avatar gitter-badger avatar jklmli avatar ksikka avatar kt3k avatar opensrcken avatar theefer avatar yaakaito avatar

Watchers

 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.