From e255b34e1a3e9b30b3c9ecffbf4cc7b1d7225972 Mon Sep 17 00:00:00 2001 From: Dakuan Date: Sun, 25 Jan 2015 10:34:30 +0000 Subject: [PATCH] use monad --- package.json | 2 +- src/util/run-on-prop.js | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 3ea5b70..87e196b 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/util/run-on-prop.js b/src/util/run-on-prop.js index 38aba6a..0fc9ac0 100644 --- a/src/util/run-on-prop.js +++ b/src/util/run-on-prop.js @@ -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; \ No newline at end of file