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

@avalander/fun

v1.0.0-beta.4

Published

This is a collection of functional primitives that I reuse across projects.

Downloads

9

Readme

This is a collection of functional primitives that I reuse across projects.

Either

import { Left, Right, tryCatch, conditional } from '@avalander/func/src/either'

Either constructors

Left :: a -> Left a

It returns an instance of Left wrapping the given value.

Right :: a -> Right a

It returns an instance of Right wrapping the given value.

tryCatch :: (...args -> a) throws fn => (fn, ...args) -> Either a

tryCatch receives a function that may throw an exception and a list of arguments. It will apply the arguments to the function and return a Left if the function throws or a Right wrapping the result otherwise.

const noOddsPlz = x => {
    if (x % 2 === 0) return x
    throw new Error('It was odd')
}

tryCatch(noOddsPlz, 4) // -> Right(4)
tryCatch(noOddsPlz, 3) // -> Left(Error(It was odd))

conditional :: ((a -> bool), string?) -> a -> Either a

conditional recieves a predicate function that takes a value and returns a boolean, and an optional string as a reason and returns a function that will take a value, apply the predicate function and return an instance of Right if the predicate returns true or an instance of Left with the reason if the predicate returns false.

const isOdd = conditional(x => x % 2 != 0, 'It is even')
isOdd(3) // -> Right(3)
isOdd(4) // -> Left(It is even)

Either methods

map :: (a -> b) -> Either b

It receives a function that will be applied to the wrapped value of a Right instance and returned wrapped in a new instance of Right or ignored in an instance of Left.

Right(3).map(x => x * 2) // -> Right(6)
Left(3).map(x => x * 2) // -> Left(3)

chain :: (a -> Either b) -> Either b

It receives a function that takes the wrapped value and returns a new instance of Either.

An instance of Right will apply the function and return the result. An instance of Left will ignore the function and return the same instance.

Right(3).map(x => Right(x * 2)) // -> Right(6)
Right(3).map(x => Left(x - 1)) // -> Left(2)
Left(3).map(x => Right(x * 2)) // -> Left(3)

fold :: ((a -> c), (b -> c)) -> c

It receives a left function that will be applied to the wrapped value of a Left instance and a right function that will be applied to the wrapped value of a Right instance. In both cases, the result of the function will be returned.

Right(3).fold(
    l => l
    r => r * 2
) // -> 6
Left(3).fold(
    l => l
    r => r * 2
) // -> 3

Maybe

import { Just, Nothing, fromNullable, fromList } from '@avalander/func/src/maybe'

Maybe constructors

Just :: a -> Just a

Returns an instance of Just wrapping a value.

Nothing :: () -> Nothing

Returns an instance of Nothing.

fromNullable :: a -> Maybe a

Returns an instance of Nothing if the given value is null or undefined and an instance of Just wrapping the value otherwise.

fromNullable(null) // -> Nothing
fromNullable('potato') // -> Just(potato)

fromList :: [a] -> Maybe a

Receives an array-like object. Returns an instance of Nothing if the object is empty and an instance of Just wrapping the object otherwise.

fromList([]) // -> Nothing
fromList([1, 2, 3]) // -> Just([1, 2, 3])

Maybe methods

map :: (a -> b) -> Maybe b

Applies the function to the wrapped value of a Just and returns the result wrapped in a new Just, or returns Nothing.

Just(3).map(x => x * 2) // -> Just(6)
Nothing().map(x => x * 2) // -> Nothing

chain :: (a -> Maybe b) -> Maybe b

Applies the function to the wrapped value of a Just and returns the result or returns Nothing.

It expects the result to be an instance of Maybe.

Just(3).chain(x => Just(x * 2)) // -> Just(6)
Just(3).chain(x => Nothing()) // -> Nothing
Just(3).chain(x => x * 2) // -> 'Error: fn in Maybe.chain(fn) must return an instance of either Just or Nothing.'
Nothing().chain(x => Just(x * 2)) // -> Nothing

fold :: ((() -> b), (a -> b)) -> b

Applies the left function if it is an instance of Nothing or the right function if it is an instance of Just to the wrapped value and returns the result.

Just(3).fold(
	() => -1,
	x => x * 2
) // -> 6
Nothing.fold(
	() => -1,
	x => x * 2
) // -> -1