Conditions | 4 |
Paths | 5 |
Total Lines | 73 |
Lines | 0 |
Ratio | 0 % |
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:
1 | /* TODO |
||
98 | .end(function (err, iires) { |
||
99 | if (err) { |
||
100 | console.log(`${timeStamp()} Failed to get grades page\n${err.stack}`.red); |
||
101 | res.status(404).send({ error: '无法进入成绩页面' }); |
||
102 | return next(err); |
||
103 | } |
||
104 | if (fullLog) { |
||
105 | console.log(`${timeStamp()} Successfully entered grades page.`.green); |
||
106 | } |
||
107 | |||
108 | let $ = cheerio.load(iires.text); |
||
109 | let result = { |
||
110 | name: escaper.unescape($('#Top1_divLoginName').text().match(/\s.+\(/)[0].replace(/\s|\(/g, '')), |
||
111 | id: escaper.unescape($('#Top1_divLoginName').text().match(/\(.+\)/)[0].replace(/\(|\)/g, '')), |
||
112 | grades: {}, |
||
113 | 'subject-count': 0, |
||
114 | failed: {}, |
||
115 | 'failed-count': 0, |
||
116 | }; |
||
117 | |||
118 | // 获取成绩列表 |
||
119 | $('#dataList tr').each(function (index) { |
||
120 | if (index === 0) { |
||
121 | return; |
||
122 | } |
||
123 | let element = $(this).find('td'); |
||
124 | |||
125 | let title = escaper.unescape(element.eq(3).text().match(/].+$/)[0].substring(1)); |
||
126 | let item = { |
||
127 | sem: escaper.unescape(element.eq(2).text()), |
||
128 | reg: escaper.unescape(element.eq(4).text()), |
||
129 | exam: escaper.unescape(element.eq(5).text()), |
||
130 | overall: escaper.unescape(element.eq(6).text()) |
||
131 | }; |
||
132 | if (req.query.details) { |
||
133 | item.id = escaper.unescape(element.eq(3).text().match(/\[.+\]/)[0].replace(/\[|\]/g, '')); |
||
134 | item.attr = escaper.unescape(element.eq(8).text()); |
||
135 | item.genre = escaper.unescape(element.eq(9).text()); |
||
136 | item.credit = escaper.unescape(element.eq(7).text()); |
||
137 | } |
||
138 | |||
139 | // 如果有补考记录,则以最高分的为准 |
||
140 | // 这段代码是拿可读性换了时间复杂度…… |
||
141 | if (title in result.grades) { |
||
142 | // 暂不考虑NaN |
||
143 | if (item.overall < result.grades[title].overall) { |
||
144 | return; |
||
145 | } |
||
146 | if (!element.eq(6).css('color')) { |
||
147 | delete result.failed[title]; |
||
148 | } |
||
149 | } else if (element.eq(6).css('color')) { |
||
150 | result.failed[title] = item; |
||
151 | } |
||
152 | |||
153 | result.grades[title] = item; |
||
154 | }); |
||
155 | |||
156 | result['subject-count'] = Object.keys(result.grades).length; |
||
157 | result['failed-count'] = Object.keys(result.failed).length; |
||
158 | |||
159 | // 返回JSON |
||
160 | res.send(JSON.stringify(result)); |
||
161 | if (fullLog) { |
||
162 | console.log(`${timeStamp()} Successfully responded. (processed in ${new Date() - start} ms)`.green); |
||
163 | } |
||
164 | |||
165 | access.logout(headers, res, function() { |
||
166 | if (fullLog) { |
||
167 | console.log(`${timeStamp()} Successfully logged out: `.green + req.query.id.yellow); |
||
168 | } |
||
169 | }); |
||
170 | }); |
||
171 | }); |
||
249 | }); |