73 lines
3.1 KiB
JavaScript
73 lines
3.1 KiB
JavaScript
/*jslint node:true */
|
|
|
|
module.exports = {
|
|
sanitize: function (text) {
|
|
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
},
|
|
copyOptions: function (options) {
|
|
var key, copy = {};
|
|
for (key in options) {
|
|
if (options.hasOwnProperty(key)) {
|
|
copy[key] = options[key];
|
|
}
|
|
}
|
|
return copy;
|
|
},
|
|
ensureFlagExists: function (item, options) {
|
|
if (!(item in options) || typeof options[item] !== 'boolean') {
|
|
options[item] = false;
|
|
}
|
|
},
|
|
ensureSpacesExists: function (options) {
|
|
if (!('spaces' in options) || (typeof options.spaces !== 'number' && typeof options.spaces !== 'string')) {
|
|
options.spaces = 0;
|
|
}
|
|
},
|
|
ensureKeyExists: function (key, options) {
|
|
if (!(key + 'Key' in options) || typeof options[key + 'Key'] !== 'string') {
|
|
options[key + 'Key'] = options.compact ? '_' + key : key;
|
|
}
|
|
},
|
|
getCommandLineHelp: function (command, requiredArgs, optionalArgs) {
|
|
var reqArgs = requiredArgs.reduce(function (res, arg) {return res + ' <' + arg.arg + '>';}, '');
|
|
var output = 'Usage: ' + command + reqArgs + ' [options]' + '\n';
|
|
requiredArgs.forEach(function (argument) {
|
|
output += ' <' + argument.arg + '>' + Array(20 - argument.arg.length).join(' ') + argument.desc + '\n';
|
|
});
|
|
output += '\nOptions:' + '\n';
|
|
optionalArgs.forEach(function (argument) {
|
|
output += ' --' + argument.arg + Array(20 - argument.arg.length).join(' ') + argument.desc + '\n';
|
|
});
|
|
return output;
|
|
},
|
|
mapCommandLineArgs: function (requiredArgs, optionalArgs) {
|
|
var options = {}, r, o, a = 2;
|
|
for (r = 0; r < requiredArgs.length; r += 1) {
|
|
if (a < process.argv.length && process.argv[a].substr(0, 1) !== '-' && process.argv[a] !== 'JASMINE_CONFIG_PATH=./jasmine.json') {
|
|
options[requiredArgs[r].option] = process.argv[a++];
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
for (; a < process.argv.length; a += 1) {
|
|
for (o = 0; o < optionalArgs.length; o += 1) {
|
|
if (optionalArgs[o].alias === process.argv[a].slice(1) || optionalArgs[o].arg === process.argv[a].slice(2)) {
|
|
break;
|
|
}
|
|
}
|
|
if (o < optionalArgs.length) {
|
|
switch (optionalArgs[o].type) {
|
|
case 'file': case 'string': case 'number':
|
|
if (a + 1 < process.argv.length) {
|
|
a += 1;
|
|
options[optionalArgs[o].option] = (optionalArgs[o].type === 'number' ? Number(process.argv[a]) : process.argv[a]);
|
|
}
|
|
break;
|
|
case 'flag':
|
|
options[optionalArgs[o].option] = true; break;
|
|
}
|
|
}
|
|
}
|
|
return options;
|
|
}
|
|
}; |