Giter Site home page Giter Site logo

just-maybe's Introduction

Just-Maybe

It's a Maybe monad for JavaScript.

Installation

> npm install --save-dev just-maybe

Usage

import { Maybe } from 'just-maybe';


// Create a Maybe from a possible null value.
const maybe: Maybe<User> =
  Maybe.fromNullable(getUser());


// Create a Just type
const maybe: Maybe<number> =
  Maybe.just(5);


// Create a Nothing type
const maybe: Maybe<number> =
  Maybe.nothing<number>();


// Map a maybe, transform a Just value
const mappedMaybe: Maybe<number> =
  maybe.map(val: number => val + 5);


// Filter a maybe, return a Nothing if predicate fails
const filteredMaybe: Maybe<number> =
  maybe.filter((val: number): boolean => val > 5);


// Chain maybes, use value of a maybe to create a new Maybe
const chainedMaybe: Maybe<string> =
  maybe.chain((val: number): Maybe<string> => {
    if (val > 5) {
      return Maybe.just('is greater than 5');
    } else {
      return Maybe.nothing<string>();
    }
  });


// Join maybes, flatten nested Maybes
const nested: Maybe<Maybe<number>> =
  Maybe.just(Maybe.just(5));

const flattened: Maybe<number> =
  nested.join();


// Fork a maybe, return value of first function for Just, value of second for Nothing
const value: number =
  maybe.fork(
    (val: number): number => val * 10,
    (): number => 100
  );


// Get the value of a Just, throws if a Nothing
const value: number =
  maybe.get();


// Get with default, return default if Nothing
const value: number =
  maybe.getOrElse(0);

just-maybe's People

Contributors

kevinbgreene avatar

Stargazers

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