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

monadify

v0.5.0

Published

Make your javascript code cleaner and more expressive with the power of monads and functional programming.

Downloads

16

Readme

Monadify

Make your javascript code cleaner and more expressive with the power of monads and functional programming.

Features

  • Makes your code more expressive by using a form of function chaining, instead of wrapping the input inside multiple function calls.
  • Fail the way you want, by passing custom error handling functions so that you don't have to abuse try and catch statements.
  • Does not mutate objects, creates a new cloned object on each instantiation, to prevent bug spewing mutations on the original object.
  • Now with support for lodash/underscore - Simply use the popular utility library you want, and gain all the functionality it provides.

Install

$ npm install --save monadify

Usage

var Monadify = require('monadify'),
  n = 1;

function add(n){
  return n + 1;
}

//The old way
console.log(add(add(add(n))));
//=> 4

//With Monadify
var nMonad = Monadify(n)
  .bind(add)
  .bind(add)
  .bind(add)
  .send(console.log);
//=> 4

//To return the result
var result = nMonad.value();
console.log(result);
//=> 4

Documentation

Monadify(input, [errorHandler])

Returns a mutation (monad) of the input argument. Optional errorHandler function, which gets called if an error is thrown somewhere down the function chain.

Arguments

  • input - The input for the monad. Objects given as input are cloned, to prevent mutation.
  • errorHandler(error) - Callback function to handle error thrown anywhere in the function chain.

Examples

var nMonad = Monadify(1);

var obj = {},
 objMonad = Monadify(obj);

//Objects passed to the constructor are cloned, which means any changes applied to 'obj' are not passed on to objMonad.
obj.a = 'a';
objMonad.send(console.log);
//=> {}

Error handling

Defaults to throw new Error(e);. Calls the errorHandler function, if present.

var nMonad = Monadify(1, console.log);
// Logs any errors to the console, instead of throwing an error.

var notAFunction = null,

  identity = function(n){
    return n;
  };

nMonad.bind(identity)
  .bind(notAFunction)
  .bind(identity);

//=> [TypeError: object is not a function]

bind(bindingFunction)

binds the given function to the current state of the input. Mutates the input into the returned value of bindingFunction.

Arguments

  • bindingFunction(input) - Function to be bound to the current state of the input.

Examples

var nMonad = Monadify(2);

nMonad.bind( n => n*3);
//current state of nMonad is 6
nMonad.send(console.log);
//=> 6

//Monads bound to console.log will subsequently return undefined, since the console.log function has no return value
nMonad.bind(console.log);
//=> 6
// current state of nMonad is undefined
nMonad.send(console.log);
//=> undefined

send(sentToFunction)

sends the current state of input to the given function. Does not affect or mutate the input, and retains input state before sending to sentToFunction.

Arguments

  • sentToFunction(input) - Function to which the current state of input is passed as an argument. Return value is disregarded.

Examples

var nMonad = Monadify(2);

nMonad.send( n => n*3);
//current state of nMonad is still 2
nMonad.send(console.log);
//=> 2

//Monads bound to console.log will subsequently return undefined, since the console.log function has no return value
nMonad.send(console.log);
//=> 2
// current state of nMonad is 2
nMonad.send(console.log);
//=> undefined

value()

Returns the current state of input.

Examples

var nMonad = Monadify(2);
nMonad.bind( n => n*3);

var n = nMonad.value();
//value of n is 6
console.log(n);
//=> 6

Lodash/underscore support*

Monadify supports the use of external utility libraries like lodash. It extends their functionality to provide for a great and expressive new way to write code. Lodash functions of the form _.someFunction(operand, argument) can be used with Monadify as someMonad.someFunction(argument).value() (see example).

* This feature is still in development. Contributions are welcome :)

Examples

var Monadify = require('monadify'),
  _ = require('lodash');

Monadify.use(_);

var nMonad = Monadify([1,2,3]);

nMonad.map(addOne)  //Native lodash function => [2,3,4]
  .max()  //Native lodash function => 4
  .bind(addOne) // Monadify bind function => 5
  .value(); // => 5


function addOne(n){
  return n + 1;
};

License

MIT © Soham Kamani