Commit a5563899 authored by Ivan Poddubny's avatar Ivan Poddubny

Simplify prepareArgs

parent b6561cde
......@@ -138,7 +138,7 @@ Context.prototype.onEvent = function (event) {
//additional agi commands
commands.map(function (command) {
commands.forEach(function (command) {
var str = '';
Context.prototype[command.name] = function () {
if (command.params > 0) {
......@@ -152,28 +152,16 @@ commands.map(function (command) {
});
var prepareArgs = function (args, argsRules, count) {
var q, argsP = [];
if (argsRules && count) {
args = args.map(function (arg){
return arg.toString();
});
for (var i = 0; i < count; i++) {
argsP[i] = (args[i]) ?
args[i] :
((argsRules[i] && argsRules[i].default) ?
argsRules[i].default :
'');
}
if (!argsRules || !count) {
return args;
}
q = argsP.map(function (arg, i) {
return (argsRules[i] && argsRules[i].prepare) ? argsRules[i].prepare(arg) : arg;
return Array.apply(null, new Array(count)) // old node.js versions don't support Array.fill()
.map(function (arg, i) {
arg = args[i] !== undefined && args[i] !== null ? args[i] : argsRules[i] && argsRules[i].default || '';
var prepare = argsRules[i] && argsRules[i].prepare || function (x) { return x; };
return prepare(String(arg));
});
} else {
q = args;
}
return q;
};
//sugar commands
......@@ -182,4 +170,4 @@ Context.prototype.dial = function (target, timeout, params) {
return this.exec('Dial', target + ',' + timeout + ',' + params);
};
module.exports = Context;
\ No newline at end of file
module.exports = Context;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment