Conditions | 22 |
Paths | 749 |
Total Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 _.extend._convert 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 | import _ from 'underscore'; |
||
55 | _convert: function (data, validate) { |
||
56 | if ((data + '').trim() === '') return null; |
||
57 | |||
58 | var date, time = null; |
||
59 | if (_.isNumber(data)) { |
||
60 | var jsDate = new Date(data); |
||
61 | date = lpad(jsDate.getUTCFullYear(), 4, 0) + '-' + lpad(jsDate.getUTCMonth() + 1, 2, 0) + '-' + lpad(jsDate.getUTCDate(), 2, 0); |
||
62 | time = lpad(jsDate.getUTCHours(), 2, 0) + ':' + lpad(jsDate.getUTCMinutes(), 2, 0) + ':' + lpad(jsDate.getUTCSeconds(), 2, 0); |
||
63 | } else { |
||
64 | data = data.trim(); |
||
65 | var parts = data.split(this.ISO_SPLITTER_RE) || []; |
||
66 | date = this.DATE_RE.test(parts[0]) ? parts[0] : ''; |
||
67 | time = date && parts[1] ? parts[1] : this.TIME_RE.test(parts[0]) ? parts[0] : ''; |
||
68 | } |
||
69 | |||
70 | var YYYYMMDD = this.DATE_RE.exec(date) || []; |
||
71 | var HHmmssSSS = this.TIME_RE.exec(time) || []; |
||
72 | |||
73 | if (validate) { |
||
74 | if (this.includeDate && _.isUndefined(YYYYMMDD[0])) return; |
||
75 | if (this.includeTime && _.isUndefined(HHmmssSSS[0])) return; |
||
76 | if (!this.includeDate && date) return; |
||
77 | if (!this.includeTime && time) return; |
||
78 | } |
||
79 | |||
80 | var jsDate = new Date(Date.UTC(YYYYMMDD[1] * 1 || 0, |
||
81 | YYYYMMDD[2] * 1 - 1 || 0, |
||
82 | YYYYMMDD[3] * 1 || 0, |
||
83 | HHmmssSSS[1] * 1 || null, |
||
84 | HHmmssSSS[2] * 1 || null, |
||
85 | HHmmssSSS[3] * 1 || null, |
||
86 | HHmmssSSS[5] * 1 || null)); |
||
87 | |||
88 | var result = ''; |
||
89 | |||
90 | if (this.includeDate) { |
||
91 | result = lpad(jsDate.getUTCFullYear(), 4, 0) + '-' + lpad(jsDate.getUTCMonth() + 1, 2, 0) + '-' + lpad(jsDate.getUTCDate(), 2, 0); |
||
92 | } |
||
93 | |||
94 | if (this.includeTime) { |
||
95 | result = result + (this.includeDate ? 'T' : '') + lpad(jsDate.getUTCHours(), 2, 0) + ':' + lpad(jsDate.getUTCMinutes(), 2, 0) + ':' + lpad(jsDate.getUTCSeconds(), 2, 0); |
||
96 | |||
97 | if (this.includeMilli) { |
||
98 | result = result + '.' + lpad(jsDate.getUTCMilliseconds(), 3, 0); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | if (this.includeDate && this.includeTime) { |
||
103 | result += "Z"; |
||
104 | } |
||
105 | |||
106 | return result; |
||
107 | }, |
||
108 | |||
148 |