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

utall

v1.1.2

Published

⚙️ Utility library ⚙️

Downloads

8

Readme

Utall

Build Status Code Coverage Issues Open Licenses Bundle Size NPM Release

General purpose utility library


Table of contents

  1. Installation
  2. Library

Installation

(npm|yarn) install utall

Library

| name | import || |-|-|- |Monad| utall.Monad |Monad for pipeline functionality.

Monad

Constructor

const simple = new Monad(value)
const { value, logs } = new Monad<T>(value, []) // Value of type T

Usage

Monad is class based. It can be initiated with a contructor

For debugging and error handling the log array can be added to the constructor. This will impact the performance of the Monad.

Apply

Applies a function to the current value and returns a new Monad.

Promise / resolve

Promise adds a promise to the stack and returns a PromisedMonad. Resolve resolves all tasks in the PromisedMonad and returns back the Promise<Monad>.

Functions apply and promise accept extra arguments. This allows for the combination of different monads (see example)

All (static)

A static function to resolve multiple PromisedMonads in parallel (Promise.all)

Maybe (static)

An extension of Monad where pipeline halts when null or undefined is returned. Can be augmented with custom logic by giving a before function to the constructor. This can be used like Monad but is generally safer

Either (static)

Either replaces the apply function by accepting two functions. Herein the 'left' function is executed as a fallback to the 'right' function.

The instance of Either has a fold function to return back into a Maybe or Monad

Examples

  1. Simple pipeline

    const addOne = (e: number) => e + 1
    
    const monad = new Monad(1)
    monad.apply(addOne)
    
    console.log(monad.value) // 2
  2. With deconstructing

    const addOne = (e: number) => e + 1
    
    const { value, log } = new Monad(1, [])
      .apply(addOne)
      .apply(addOne)
    
    console.log(value) // 3
    console.log(log)   // [2,3]
  3. With new arguments

    const addOne = (e: number) => e + 1
    const multiply = (e: number, c: number) => e * c
    
    const { value, log } = new Monad(2, [])
      .apply(addOne)
      .apply(multiply, 2)
      .apply(multiply, 2)
    
    console.log(value) // 12
    console.log(log)   // [3,6,12]
  4. With Promises

    const addOne = (e: number) => e + 1
    const promisedAddOne = (e) => Promise.resolve(e + 1)
    
    const { value, log } = new Monad(1, [])
      .apply(addOne)
      .promise(promisedAddOne)
      .promise(promisedAddOne)
      .resolve()
    
    console.log(value) // 4
    console.log(log)   // [2,3,4]
  5. Combined monads

    const multiply = (e: number, c: number) => e * c
    
    const monadOne = new Monad(1, []).apply(multiply, 2)
    const monadTwo = new Monad(1).apply(multiply, 2)
    
    const { value, log } = monadOne.apply(multiply, monadTwo.value)
    
    console.log(value)          // 4
    console.log(log)            // [2,4]
    console.log(monadTwo.value) // 2
    console.log(monadTwo.log)   // undefined
  6. Combined promised monads (top level await for simplicity)

    const promisedAddOne = (e) => Promise.resolve(e + 1)
    
    const monadOne = new Monad(1, [])
      .promise(promisedAddOne)
      .promise(promisedAddOne)
    
    const monadTwo = new Monad(10, [])
      .promise(promisedAddOne)
      .promise(promisedAddOne)
    
    const [
      { value: value1, log: log1 },
      { value: value2, log: log2 },
    ] = await Monad.all<number>([monadOne, monadTwo])
    
    console.log(value1) // 3
    console.log(value2) // 12
    console.log(log1)   // [2,3]
    console.log(log2)   // [11,12]
  7. Use of a Maybe

    const addOne = (e: number) => e + 1
    const isNumber = (e) => typeof e === 'number'
    
    const maybe = new Monad.Maybe('1', null, isNumber)
    
    const { value } = maybe.apply(addOne)
    
    console.log(value) // '1'
  8. Use of an Either

    const addOne = (e: number) => e + 1
    const multiply = (e: number, c: number) => e * c
    
    const either = new Monad.Either(2)
      .apply(addOne, multiply, 2)
      .apply(addOne, addOne)
    
    console.log(either.value)      // 5
    console.log(either.fold(true)) // instanceof Maybe
    console.log(either.fold()) .   // instanceof Monad

License

MIT licensed