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

juan-ignacio-acosta-rios-maybe-utility-library

v1.0.2

Published

A bunch of tools to deal with uncertainty

Downloads

7

Readme

Maybe Library

A TypeScript library for working with Maybe types, providing utilities for handling optional values.

Installation

To install the library, use npm or yarn:

npm install juan-ignacio-acosta-rios-maybe-utility-library

or

yarn add juan-ignacio-acosta-rios-maybe-utility-library

Importing

To use the library, import the necessary functions and types:


import { buildMaybe } from '@/maybe/factory';
import { type Maybe, type MaybeFunction, type Nothing } from '@/maybe/typing';

Functions

isNothingType

Checks if a value is Nothing (null or undefined).

export function isNothingType<T> (value: T | Nothing): value is Nothing {
    return value === null || value === undefined;
}

isValueType

Checks if a value is not Nothing.

export function isValueType<T> (value: T | Nothing): value is T {
    return !isNothingType(value);
}

isValue

Checks if a Maybe contains a value.

export function isValue<T> (maybe: Maybe<T>): boolean {
    return isValueType(maybe.value);
}

isNothing

Checks if a Maybe is Nothing.

export function isNothing<T> (maybe: Maybe<T>): boolean {
    return isNothingType(maybe.value);
}

evaluate

Returns the value of a Maybe if it exists, otherwise returns a default value.

export function evaluate<T> (maybe: Maybe<T>, defaultValue: T): T {
    if (isValueType(maybe.value)) {
        return maybe.value;
    }
    return defaultValue;
}

map

Maps a Maybe value using a mapper function, returning a new Maybe.

export function map<T, U> (maybe: Maybe<T>, mapper: (value: T) => U): Maybe<U> {
    if (isValueType(maybe.value)) {
        return buildMaybe(mapper(maybe.value));
    }
    return buildMaybe();
}

reduce

Reduces a series of Maybe functions starting with an initial Maybe value.

export function reduce<T> (initialValue: Maybe<T>, fns: MaybeFunction<T>[]): Maybe<T> {
    return fns.reduce((acc, fn) => {
        if (isNothingType(acc.value)) {
            return buildMaybe();
        }
        return fn(acc.value);
    }, initialValue);
}

filter

Filters a Maybe value based on a predicate function.

export function filter<T> (maybe: Maybe<T>, predicate: (value: T) => boolean): Maybe<T> {
    if (isValueType(maybe.value) && predicate(maybe.value)) {
        return maybe;
    }
    return buildMaybe();
}

toMaybeFunction

Converts a regular function to a MaybeFunction. The returned function, when called, will execute the original function and wrap the result in a Maybe. If the original function throws an error, the returned function will return Nothing.

export function toMaybeFunction<T> (fn: TypeFunction<T>): MaybeFunction<T> {
    return (value: T): Maybe<T> => {
        try {
            return buildMaybe(fn(value));
        } catch (error) {
            return buildMaybe();
        }
    };
}

Example Usage


import { buildMaybe, isNothing, isValue, evaluate, map, reduce, filter } from '@/maybe';

// Example usage of the Maybe library
const maybeValue = buildMaybe(5);

if (isValue(maybeValue)) {
    console.log('Value exists:', maybeValue.value);
}


const defaultValue = 10;
const evaluatedValue = evaluate(maybeValue, defaultValue);
console.log('Evaluated value:', evaluatedValue);


const mappedValue = map(maybeValue, x => x * 2);
console.log('Mapped value:', mappedValue);


const reducedValue = reduce(maybeValue, [x => buildMaybe(x + 1), x => buildMaybe(x * 3)]);
console.log('Reduced value:', reducedValue);


const filteredValue = filter(maybeValue, x => x > 3);
console.log('Filtered value:', filteredValue);


// A regular function that might throw an error
const riskyFunction = (value: number): number => {
    if (value < 0) {
        throw new Error('Negative value!');
    }
    return value * 2;
};

const safeFunction = toMaybeFunction(riskyFunction);

const maybeResult1 = safeFunction(5);
console.log('Result for 5:', maybeResult1);  // Outputs the Maybe with value 10

const maybeResult2 = safeFunction(-1);
console.log('Result for -1:', maybeResult2); // Outputs Nothing due to the error