chain-commander
Advanced tools
Comparing version 0.1.0 to 0.1.2
{ | ||
"name": "chain-commander", | ||
"version": "0.1.0", | ||
"version": "0.1.2", | ||
"repository": { | ||
@@ -5,0 +5,0 @@ "type":"git", |
{ | ||
"name": "chain-commander", | ||
"repo": "pocesar/js-chain-commander.js", | ||
"version": "0.1.0", | ||
"version": "0.1.2", | ||
"description": "Chain commander is a library based on Q library, to encapsulate business rules logic in form of javascript objects or JSON.", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
(function (root, definition){ | ||
/* istanbul ignore else: untestable */ | ||
if (typeof exports === 'object') { | ||
/* CommonJS/Node.js */ | ||
module.exports = definition; | ||
} else if (typeof root.define === 'function' && root.define.amd) { | ||
@@ -35,2 +35,3 @@ /* RequireJS */ | ||
function log() { | ||
/* istanbul ignore next: untestable */ | ||
if (self.debug === true) { | ||
@@ -116,4 +117,7 @@ console.log.apply(console, Array.prototype.slice.call(arguments)); | ||
log('conditional fcall', condition[0]); | ||
if (self.throws === true && typeof context[condition[0]] !== 'function') { | ||
throw new Error('"if" function "' + condition[0] + '" doesnt exists'); | ||
if (self.throws === true) { | ||
/* istanbul ignore else */ | ||
if (typeof context[condition[0]] !== 'function') { | ||
throw new Error('"if" function "' + condition[0] + '" doesnt exists'); | ||
} | ||
} | ||
@@ -152,16 +156,9 @@ return context[condition[0]].apply(context, condition.slice(1).concat(currentValue)); | ||
parser.call(self, _else, currentValue, context) | ||
.fail(function(){ | ||
/* the last parser threw an error, keep going with the current unchanged value */ | ||
log('conditionalelse fcall'); | ||
d.resolve(currentValue); | ||
}) | ||
/* parser never fails */ | ||
.done(function(val){ | ||
/* only resolve with the new value if it isn't undefined (that means the value had no "return" value */ | ||
/* parser always return the currentValue if there are none */ | ||
log('conditionalelse done', val); | ||
if (val === void 0) { | ||
d.resolve(currentValue); | ||
} else { | ||
d.resolve(val); | ||
} | ||
d.resolve(val); | ||
}); | ||
@@ -183,10 +180,8 @@ | ||
/* we should either check if the "if" was successiful, if so, execute */ | ||
if (val === true) { | ||
if (val === true && typeof parsing['exec'] !== 'undefined') { | ||
for (i = 0, len = parsing['exec'].length; i < len; i++) { | ||
inpromise = inpromise.then(exec(parsing['exec'][i])); | ||
} | ||
} else { | ||
if (typeof parsing['else'] !== 'undefined') { | ||
inpromise = inpromise.then(conditionalelse(parsing['else'])); | ||
} | ||
} else if (val === false && typeof parsing['else'] !== 'undefined') { | ||
inpromise = inpromise.then(conditionalelse(parsing['else'])); | ||
} | ||
@@ -197,3 +192,7 @@ | ||
} else { | ||
d.resolve(currentValue); | ||
if (self.throws === true) { | ||
d.reject(new Error('Missing "checks" in "if"')); | ||
} else { | ||
d.resolve(currentValue); | ||
} | ||
} | ||
@@ -229,4 +228,5 @@ | ||
this.debug = opts.debug !== undefined ? opts.debug : false; | ||
this.throws = opts.throws !== undefined ? opts.throws : false; | ||
/* istanbul ignore next */ | ||
this.debug = opts.debug !== void 0 ? opts.debug : false; | ||
this.throws = opts.throws !== void 0 ? opts.throws : false; | ||
@@ -236,2 +236,25 @@ this.obj = convert(obj); | ||
/** | ||
* Execute all items in chain, with the same context | ||
* | ||
* @param {*} value | ||
* @param {Array} arr | ||
* @param {Object} context | ||
*/ | ||
ChainCommander.all = function(value, arr, context){ | ||
var | ||
result = Q(value), | ||
curry = function(item){ | ||
return function(val) { | ||
return item.execute(val, context); | ||
}; | ||
}; | ||
for(var i = 0; i < arr.length; i++) { | ||
result = result.then(curry(arr[i])); | ||
} | ||
return result; | ||
}; | ||
ChainCommander.prototype.execute = function(initial, context){ | ||
@@ -238,0 +261,0 @@ return parser.call(this, this.obj, Q(initial), context); |
{ | ||
"name" : "chain-commander", | ||
"version": "0.1.0", | ||
"version": "0.1.2", | ||
"main": "./lib/chain-commander.js", | ||
@@ -34,4 +34,7 @@ "description":"Chain commander is a library based on Q library, to encapsulate business rules logic in form of javascript objects or JSON.", | ||
"mocha": "*", | ||
"expect.js": "*" | ||
"expect.js": "*", | ||
"grunt": "0.4.x", | ||
"grunt-mocha-istanbul":"*", | ||
"coveralls":"*" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
[![Build Status](https://travis-ci.org/pocesar/js-chain-commander.png?branch=master)](https://travis-ci.org/pocesar/js-chain-commander) | ||
[![Build Status](https://travis-ci.org/pocesar/js-chain-commander.png?branch=master)](https://travis-ci.org/pocesar/js-chain-commander) [![Coverage Status](https://coveralls.io/repos/pocesar/js-chain-commander/badge.png)](https://coveralls.io/r/pocesar/js-chain-commander) | ||
@@ -255,3 +255,3 @@ Chain Commander | ||
{"if": { | ||
"conditions":[ | ||
"check":[ | ||
["isSelected"] | ||
@@ -264,3 +264,3 @@ ], | ||
{"if": { | ||
"conditions":[ | ||
"check":[ | ||
["isCombo"] | ||
@@ -292,3 +292,3 @@ ], | ||
{"if": { | ||
"conditions":[ | ||
"check":[ | ||
["isSelected"] | ||
@@ -305,2 +305,12 @@ ], | ||
## ChainCommander.all | ||
Taking that you have an array with many chaincommanders, that you want to execute in order, with the same context: | ||
```js | ||
ChainCommander.all('initial value', arrayOfCommanders, context).done(function(value){ | ||
// initial value transformed | ||
}); | ||
``` | ||
## Debug | ||
@@ -307,0 +317,0 @@ |
139
test/test.js
@@ -18,2 +18,12 @@ var | ||
}, | ||
'if without member check is forgiving':function(done){ | ||
CC([ | ||
{if:{ | ||
exec:[['done']] | ||
}} | ||
]).execute('ok', {}).then(function(value){ | ||
expect(value).to.equal('ok'); | ||
done(); | ||
}).done(); | ||
}, | ||
'exec call functions in the context object': function(done){ | ||
@@ -294,3 +304,4 @@ var cc, obj; | ||
exec:[['done']] | ||
} | ||
}, | ||
exec:[['returnUndefined']] | ||
} | ||
@@ -300,2 +311,10 @@ } | ||
} | ||
}, | ||
{ | ||
if: { | ||
check:[['no']], | ||
else: { | ||
exec:[['throws']] | ||
} | ||
} | ||
} | ||
@@ -311,4 +330,9 @@ ]; | ||
}, | ||
done: function(){ | ||
done: function(value){ | ||
return 'done'; | ||
}, | ||
returnUndefined: function(){ | ||
}, | ||
throws: function(){ | ||
throw new Error('else throw'); | ||
} | ||
@@ -396,2 +420,66 @@ }; | ||
}, | ||
'two if checks': function(done){ | ||
var context, cmds = [ | ||
{ | ||
if:{ | ||
check: [ | ||
['returnFalse'], | ||
['returnTrue'] | ||
], | ||
exec:[ | ||
['callIt'] | ||
] | ||
} | ||
} | ||
], cc; | ||
context = { | ||
count: 0, | ||
called: false, | ||
returnTrue: function(){ | ||
this.count++; | ||
return true; | ||
}, | ||
returnFalse: function(){ | ||
this.count++; | ||
return false; | ||
}, | ||
callIt: function(){ | ||
this.called = true; | ||
} | ||
}; | ||
cc = new CC(cmds); | ||
cc.execute('', context).then(function(){ | ||
expect(context.called).to.be(false); | ||
expect(context.count).to.be(1); | ||
done(); | ||
}).done(); | ||
}, | ||
'execute array of chain commanders': function(done){ | ||
var context, cmds = [ | ||
{ | ||
exec: [['append']] | ||
} | ||
], ccs = [ | ||
new CC(cmds), | ||
new CC(cmds), | ||
new CC(cmds), | ||
new CC(cmds), | ||
new CC(cmds) | ||
]; | ||
context = { | ||
count: 0, | ||
letters: 'abcd', | ||
append: function(value){ | ||
return value + this.letters[this.count++ % this.letters.length]; | ||
} | ||
}; | ||
CC.all('wow-', ccs, context).then(function(value){ | ||
expect(value).to.equal('wow-abcda'); | ||
done(); | ||
}).done(); | ||
}, | ||
'forgiving on non existant checks and functions': function(done){ | ||
@@ -420,4 +508,4 @@ var cc, obj = {thisOneDoes: function(plus, value){ return value + plus; }}, cmds = [ | ||
'unforgiving definition': function(done) { | ||
var ifDomain = domain.create(), execDomain = domain.create(); | ||
var cc, obj = {}, cmds = [ | ||
var execDomain = domain.create(), | ||
cmds = [ | ||
{ | ||
@@ -429,2 +517,7 @@ if: { | ||
{ | ||
if: { | ||
exec:[['doh']] | ||
} | ||
}, | ||
{ | ||
exec:[['dontExists']] | ||
@@ -434,23 +527,35 @@ } | ||
cc = CC(cmds, {throws: true}); | ||
var thrown = 0; | ||
ifDomain.on('error', function(err){ | ||
expect(err.message).to.be('"if" function "dontExist" doesnt exists'); | ||
cc = CC(cmds.pop(), {throws: true}); | ||
var run = function(){ | ||
var cc; | ||
execDomain.on('error', function(err){ | ||
expect(err.message).to.be('"exec" function "dontExists" doesnt exists'); | ||
done(); | ||
cc = new CC(cmds.slice(thrown), {throws: true}); | ||
execDomain.run(function(){ | ||
cc.execute(0, {}).done(function(){}); | ||
}); | ||
}; | ||
execDomain.run(function(){ | ||
cc.execute(0, obj).done(function(){}); | ||
}); | ||
execDomain.on('error', function(err){ | ||
switch (thrown) { | ||
case 0: | ||
expect(err.message).to.be('"if" function "dontExist" doesnt exists'); | ||
thrown++; | ||
run(); | ||
break; | ||
case 1: | ||
expect(err.message).to.be('Missing "checks" in "if"'); | ||
thrown++; | ||
run(); | ||
break; | ||
case 2: | ||
expect(err.message).to.be('"exec" function "dontExists" doesnt exists'); | ||
done(); | ||
break; | ||
} | ||
}); | ||
ifDomain.run(function(){ | ||
cc.execute(0, obj).done(function(){}); | ||
}); | ||
run(); | ||
} | ||
} | ||
}; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
134726
22
884
325
6