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
andcatch
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 = n = 1; { return n + 1;} //The old wayconsole;//=> 4 //With Monadifyvar nMonad = ;//=> 4 //To return the resultvar result = nMonadvalue;console;//=> 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 = ; var obj = {} objMonad = ; //Objects passed to the constructor are cloned, which means any changes applied to 'obj' are not passed on to objMonad.obja = 'a';objMonad;//=> {}
Error handling
Defaults to throw new Error(e);
. Calls the errorHandler
function, if present.
var nMonad = ;// Logs any errors to the console, instead of throwing an error. var notAFunction = null { return n; }; nMonad ; //=> [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 = ; nMonad;//current state of nMonad is 6nMonad;//=> 6 //Monads bound to console.log will subsequently return undefined, since the console.log function has no return valuenMonad;//=> 6// current state of nMonad is undefinednMonad;//=> 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 = ; nMonad;//current state of nMonad is still 2nMonad;//=> 2 //Monads bound to console.log will subsequently return undefined, since the console.log function has no return valuenMonad;//=> 2// current state of nMonad is 2nMonad;//=> undefined
value()
Returns the current state of input.
Examples
var nMonad = ;nMonad; var n = nMonadvalue;//value of n is 6console;//=> 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 = _ = ; Monadify; var nMonad = ; nMonad //Native lodash function => [2,3,4] //Native lodash function => 4 // Monadify bind function => 5 value; // => 5 { return n + 1;};
License
MIT © Soham Kamani