Skip to content

Commit

Permalink
use monad
Browse files Browse the repository at this point in the history
  • Loading branch information
Dakuan committed Jan 25, 2015
1 parent 5faf1c0 commit e255b34
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "connect-chaos",
"version": "0.0.7",
"version": "0.0.8",
"description": "connect / express middleware that causes chaos",
"main": "index.js",
"scripts": {
Expand Down
15 changes: 9 additions & 6 deletions src/util/run-on-prop.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
var R = require('ramda');
var R = require('ramda'),
Maybe = require('ramda/ext/types/Maybe');

// returns a function that expects an object with a property containing a config variable
// if the variable is not a number it is ignored
function _runOnProp(fn, prop) {
// if the args aren't a number use null instead
var parseArgs = R.ifElse(R.is(Number), R.I, R.always());
// if the args are not an object, use empty hash instead
var coalesceToObject = R.ifElse(R.is(Object), R.I, R.always({}))
return R.compose(fn, parseArgs, R.prop(prop), coalesceToObject);
return R.compose(
fn, // apply the function
R.prop('value'), // unwrap the monad
R.map(R.ifElse(R.is(Number), R.I, R.always())), // if the prop isn't a number use undefined
R.map(R.prop(prop)), // get the prop if the argument is truthy
Maybe // Maybe null
);
}

module.exports = _runOnProp;

0 comments on commit e255b34

Please sign in to comment.