Conditions | 34 |
Paths | 832 |
Total Lines | 148 |
Code Lines | 90 |
Lines | 1 |
Ratio | 0.68 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like module.exports often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | View Code Duplication | module.exports = function (args, opts) { |
|
|
|||
2 | if (!opts) opts = {}; |
||
3 | |||
4 | var flags = { bools : {}, strings : {} }; |
||
5 | |||
6 | [].concat(opts['boolean']).filter(Boolean).forEach(function (key) { |
||
7 | flags.bools[key] = true; |
||
8 | }); |
||
9 | |||
10 | [].concat(opts.string).filter(Boolean).forEach(function (key) { |
||
11 | flags.strings[key] = true; |
||
12 | }); |
||
13 | |||
14 | var aliases = {}; |
||
15 | Object.keys(opts.alias || {}).forEach(function (key) { |
||
16 | aliases[key] = [].concat(opts.alias[key]); |
||
17 | aliases[key].forEach(function (x) { |
||
18 | aliases[x] = [key].concat(aliases[key].filter(function (y) { |
||
19 | return x !== y; |
||
20 | })); |
||
21 | }); |
||
22 | }); |
||
23 | |||
24 | var defaults = opts['default'] || {}; |
||
25 | |||
26 | var argv = { _ : [] }; |
||
27 | Object.keys(flags.bools).forEach(function (key) { |
||
28 | setArg(key, defaults[key] === undefined ? false : defaults[key]); |
||
29 | }); |
||
30 | |||
31 | var notFlags = []; |
||
32 | |||
33 | if (args.indexOf('--') !== -1) { |
||
34 | notFlags = args.slice(args.indexOf('--')+1); |
||
35 | args = args.slice(0, args.indexOf('--')); |
||
36 | } |
||
37 | |||
38 | function setArg (key, val) { |
||
39 | var value = !flags.strings[key] && isNumber(val) |
||
40 | ? Number(val) : val |
||
41 | ; |
||
42 | setKey(argv, key.split('.'), value); |
||
43 | |||
44 | (aliases[key] || []).forEach(function (x) { |
||
45 | setKey(argv, x.split('.'), value); |
||
46 | }); |
||
47 | } |
||
48 | |||
49 | for (var i = 0; i < args.length; i++) { |
||
50 | var arg = args[i]; |
||
51 | |||
52 | if (/^--.+=/.test(arg)) { |
||
53 | // Using [\s\S] instead of . because js doesn't support the |
||
54 | // 'dotall' regex modifier. See: |
||
55 | // http://stackoverflow.com/a/1068308/13216 |
||
56 | var m = arg.match(/^--([^=]+)=([\s\S]*)$/); |
||
57 | setArg(m[1], m[2]); |
||
58 | } |
||
59 | else if (/^--no-.+/.test(arg)) { |
||
60 | var key = arg.match(/^--no-(.+)/)[1]; |
||
61 | setArg(key, false); |
||
62 | } |
||
63 | else if (/^--.+/.test(arg)) { |
||
64 | var key = arg.match(/^--(.+)/)[1]; |
||
65 | var next = args[i + 1]; |
||
66 | if (next !== undefined && !/^-/.test(next) |
||
67 | && !flags.bools[key] |
||
68 | && (aliases[key] ? !flags.bools[aliases[key]] : true)) { |
||
69 | setArg(key, next); |
||
70 | i++; |
||
71 | } |
||
72 | else if (/^(true|false)$/.test(next)) { |
||
73 | setArg(key, next === 'true'); |
||
74 | i++; |
||
75 | } |
||
76 | else { |
||
77 | setArg(key, flags.strings[key] ? '' : true); |
||
78 | } |
||
79 | } |
||
80 | else if (/^-[^-]+/.test(arg)) { |
||
81 | var letters = arg.slice(1,-1).split(''); |
||
82 | |||
83 | var broken = false; |
||
84 | for (var j = 0; j < letters.length; j++) { |
||
85 | var next = arg.slice(j+2); |
||
86 | |||
87 | if (next === '-') { |
||
88 | setArg(letters[j], next) |
||
89 | continue; |
||
90 | } |
||
91 | |||
92 | if (/[A-Za-z]/.test(letters[j]) |
||
93 | && /-?\d+(\.\d*)?(e-?\d+)?$/.test(next)) { |
||
94 | setArg(letters[j], next); |
||
95 | broken = true; |
||
96 | break; |
||
97 | } |
||
98 | |||
99 | if (letters[j+1] && letters[j+1].match(/\W/)) { |
||
100 | setArg(letters[j], arg.slice(j+2)); |
||
101 | broken = true; |
||
102 | break; |
||
103 | } |
||
104 | else { |
||
105 | setArg(letters[j], flags.strings[letters[j]] ? '' : true); |
||
106 | } |
||
107 | } |
||
108 | |||
109 | var key = arg.slice(-1)[0]; |
||
110 | if (!broken && key !== '-') { |
||
111 | if (args[i+1] && !/^(-|--)[^-]/.test(args[i+1]) |
||
112 | && !flags.bools[key] |
||
113 | && (aliases[key] ? !flags.bools[aliases[key]] : true)) { |
||
114 | setArg(key, args[i+1]); |
||
115 | i++; |
||
116 | } |
||
117 | else if (args[i+1] && /true|false/.test(args[i+1])) { |
||
118 | setArg(key, args[i+1] === 'true'); |
||
119 | i++; |
||
120 | } |
||
121 | else { |
||
122 | setArg(key, flags.strings[key] ? '' : true); |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | else { |
||
127 | argv._.push( |
||
128 | flags.strings['_'] || !isNumber(arg) ? arg : Number(arg) |
||
129 | ); |
||
130 | } |
||
131 | } |
||
132 | |||
133 | Object.keys(defaults).forEach(function (key) { |
||
134 | if (!hasKey(argv, key.split('.'))) { |
||
135 | setKey(argv, key.split('.'), defaults[key]); |
||
136 | |||
137 | (aliases[key] || []).forEach(function (x) { |
||
138 | setKey(argv, x.split('.'), defaults[key]); |
||
139 | }); |
||
140 | } |
||
141 | }); |
||
142 | |||
143 | notFlags.forEach(function(key) { |
||
144 | argv._.push(key); |
||
145 | }); |
||
146 | |||
147 | return argv; |
||
148 | }; |
||
149 | |||
188 |