npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

maybe-monade

v1.3.6

Published

Maybe monad implementation in Typescript

Downloads

15

Readme

maybe-monade

Maybe monad implementation in Typescript. Inspired from Haskell Maybe and Java Optional< T >

Maybe monad is an abstraction for values that may or may not exist

Maybe does not replace the exception mechanism but in most cases the use of Maybe to represent the non-existence of a value is more appropriate.

I recommend this excellent article to understand Functors, Applicatives, And Monads In Pictures or his javascript translation

Installation

npm i --save maybe-monade

Import

import {Maybe} from "maybe-monade"

Chaining functions (flatmap, map, do and getOrElse)

const isUserAuthenticated: boolean = getUserById(2)
    .flatMap(getUserToken)
    .map<boolean>({expire} => expire > new Date())
    .do(x => console.log)
    .getOrElse(false);

expect(isUserAuthenticated).toBeTruthy();

Maybe.some() and Maybe.none() as a function result

export const getUserById = (id: number): Maybe<IUser> => {
  const user: IUser = { id, email: "[email protected]" };
  return id < 1 ? Maybe.none() : Maybe.some(user);
};

export const getUserToken = (user: IUser): Maybe<IAppUser> => {
  const { id, email } = user;
  const appuser: IAppUser = {
    id,
    email,
    token: "HAAZNEBD12",
    expire: new Date(2020, 1, 1)
  };
  return !email ? Maybe.none() : Maybe.some(appuser);
};

Functions

fromValue< T >(value: T): Maybe< T >

const zero = Maybe.fromValue<number>(0);
expect(zero).toEqual({value: 0}); // some maybe

const scoped = () => {
    const undefined = 2;// undefined as variable name
    expect(Maybe.fromValue(undefined))
    .toEqual({value: 2}); // maybe some
};

expect(Maybe.fromValue(undefined))
.toEqual({value: null}); // none maybe

expect(Maybe.fromValue(null))
.toEqual({value: null}); // none maybe

getOrElse(defaultValue: T): T

const getNothing = (): Maybe<number> => Maybe.none();
const value: number = getNothing().getOrElse(0);
expect(value).toEqual(0);

orElse(alternative: () => Maybe< T >): Maybe< T >

const getNothing = (): Maybe<number> => Maybe.none();
const value: Maybe<number> = getNothing().orElse(() => Maybe.some(0));
expect(value).toEqual(Maybe.some(0)); //unsafe get

map< R >(fmap: (value: T) => R): Maybe< R >

const value = Maybe.some(2).map(x => x + 1);
expect(value).toEqual(Maybe.some(3));

flatMap< R >(f: (value: T) => Maybe< R >): Maybe< R >

const value = Maybe.some(2).flatMap(x => Maybe.some(x).map(y => y + 1));
expect(value).toEqual(Maybe.some(3));

get(): T

const value = Maybe.some(2).get();
expect(value).toEqual(2);
expect(() => Maybe.none().get()).toThrow();

do(f: (value: T) => void): Maybe< T >

Maybe.some(2).do(console.log); // print 2

filter(predicate: (x: T) => boolean): Maybe< T >

const value = Maybe.some(2).filter(x => x % 3 === 0);
expect(value).toEqual(Maybe.none());

isEmpty()

const value = Maybe.none();
expect(value.isEmpty()).toBeTruthy();

exists()

const value = Maybe.some(2);
expect(value.exists()).toBeTruthy();

Maybe callbacks

from throwable function

const throws = (): number => {
  throw new Error("error");
};
const wrapped = Maybe.fromFunction<number>(throws);
const wrappedResult = wrapped.applySafe();
expect(wrappedResult).toEqual({ value: null });

from undefined function

// callback which could be empty
const callback: any = undefined;
const wrappedCallback = Maybe.fromFunction<number>(callback);
// executing undefined function returns None
// instead of throwing "undefined is not a function" Error
const result = wrappedCallback.apply();
expect(result).toEqual({ value: null });

from some function

const div = (a: any, b: any) => a / b;
const safeDiv = Maybe.fromFunction<number>(div);
const just3 = safeDiv.apply(1, 2);
expect(just3).toEqual({ value: 0.5 });

from some function returning null

const square = (a: number | null): number | null => (a ? a * a : null);
const maybe_square = Maybe.fromFunction<number>(square);
const maybe_result = maybe_square.apply(null);
const mapped_result = maybe_result.map(x => x + 1).map(x => x + 2); // expected => {value: null}
expect(mapped_result).toEqual(Maybe.none());

To clone and run the project

git clone https://github.com/bouraine/maybe-monade.git

npm install install npm packages

npm run test to run jest tests

npm run test:watch tests with watch option

tsc or npm run build to build project

Contributing to maybe-monade

Feel free to submit issues, request features or contribute by sending a pull request.

Issues

https://github.com/bouraine/maybe-monade/issues

Pull Requests

https://github.com/bouraine/maybe-monade/pulls