1 |
|
2 |
|
3 | module.exports = {
|
4 | sanitizeElement: function (text) {
|
5 | return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
6 | },
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | copyOptions: function (options) {
|
12 | var key, copy = {};
|
13 | for (key in options) {
|
14 | if (options.hasOwnProperty(key)) {
|
15 | copy[key] = options[key];
|
16 | }
|
17 | }
|
18 | return copy;
|
19 | },
|
20 | ensureFlagExists: function (item, options) {
|
21 | if (!(item in options) || typeof options[item] !== 'boolean') {
|
22 | options[item] = false;
|
23 | }
|
24 | },
|
25 | ensureSpacesExists: function (options) {
|
26 | if (!('spaces' in options) || (typeof options.spaces !== 'number' && typeof options.spaces !== 'string')) {
|
27 | options.spaces = 0;
|
28 | }
|
29 | },
|
30 | ensureKeyExists: function (key, options) {
|
31 | if (!(key + 'Key' in options) || typeof options[key + 'Key'] !== 'string') {
|
32 | options[key + 'Key'] = options.compact ? '_' + key : key;
|
33 | }
|
34 | },
|
35 | getCommandLineHelp: function (command, requiredArgs, optionalArgs) {
|
36 | var reqArgs = requiredArgs.reduce(function (res, arg) {return res + ' <' + arg.arg + '>';}, '');
|
37 | var output = 'Usage: ' + command + reqArgs + ' [options]' + '\n';
|
38 | requiredArgs.forEach(function (argument) {
|
39 | output += ' <' + argument.arg + '>' + Array(20 - argument.arg.length).join(' ') + argument.desc + '\n';
|
40 | });
|
41 | output += '\nOptions:' + '\n';
|
42 | optionalArgs.forEach(function (argument) {
|
43 | output += ' --' + argument.arg + Array(20 - argument.arg.length).join(' ') + argument.desc + '\n';
|
44 | });
|
45 | return output;
|
46 | },
|
47 | mapCommandLineArgs: function (requiredArgs, optionalArgs) {
|
48 | var options = {}, r, o, a = 2;
|
49 | for (r = 0; r < requiredArgs.length; r += 1) {
|
50 | if (a < process.argv.length && process.argv[a].substr(0, 1) !== '-' && process.argv[a] !== 'JASMINE_CONFIG_PATH=./jasmine.json') {
|
51 | options[requiredArgs[r].option] = process.argv[a++];
|
52 | } else {
|
53 | break;
|
54 | }
|
55 | }
|
56 | for (; a < process.argv.length; a += 1) {
|
57 | for (o = 0; o < optionalArgs.length; o += 1) {
|
58 | if (optionalArgs[o].alias === process.argv[a].slice(1) || optionalArgs[o].arg === process.argv[a].slice(2)) {
|
59 | break;
|
60 | }
|
61 | }
|
62 | if (o < optionalArgs.length) {
|
63 | switch (optionalArgs[o].type) {
|
64 | case 'file': case 'string': case 'number':
|
65 | if (a + 1 < process.argv.length) {
|
66 | a += 1;
|
67 | options[optionalArgs[o].option] = (optionalArgs[o].type === 'number' ? Number(process.argv[a]) : process.argv[a]);
|
68 | }
|
69 | break;
|
70 | case 'flag':
|
71 | options[optionalArgs[o].option] = true; break;
|
72 | }
|
73 | }
|
74 | }
|
75 | return options;
|
76 | }
|
77 | };
|