| Conditions | 6 |
| Paths | 33 |
| Total Lines | 64 |
| Lines | 64 |
| Ratio | 100 % |
| Changes | 1 | ||
| 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:
| 1 | /* TODO |
||
| 149 | View Code Duplication | app.get('/exams', function (req, res, next) {
|
|
| 150 | if (!req.query.id || !req.query.pwd || (req.query.sem && !(/^20\d{2}-20\d{2}-[1-2]$/).test(req.query.sem))) {
|
||
| 151 | res.send({ error: "参数不正确" });
|
||
| 152 | return; |
||
| 153 | } |
||
| 154 | if (fullLog) {
|
||
| 155 | var start = new Date(); |
||
| 156 | console.log((timeStamp() + 'Started to query the exams: ').cyan + req.query.id.yellow); |
||
|
1 ignored issue
–
show
|
|||
| 157 | } |
||
| 158 | access.login(req.query.id, req.query.pwd, res, function (headers, ires) {
|
||
| 159 | fullLog && console.log((timeStamp() + 'Successfully logged in.').green); |
||
|
1 ignored issue
–
show
|
|||
| 160 | |||
| 161 | var ret = {};
|
||
| 162 | var $ = cheerio.load(ires.text); |
||
| 163 | |||
| 164 | ret.name = escaper.unescape($('.block1text').html()).match(/姓名:.+</)[0].replace('<', '').substring(3);
|
||
| 165 | ret.id = req.query.id; |
||
| 166 | ret.sem = req.query.sem || getSem(); |
||
| 167 | |||
| 168 | superagent |
||
| 169 | .post('http://csujwc.its.csu.edu.cn/jsxsd/xsks/xsksap_list')
|
||
| 170 | .set(headers) |
||
| 171 | .type('form')
|
||
| 172 | .send({
|
||
| 173 | xqlbmc: '', |
||
| 174 | xnxqid: ret.sem, |
||
| 175 | xqlb: '' |
||
| 176 | }) |
||
| 177 | .end(function (err, iires) {
|
||
| 178 | if (err) {
|
||
| 179 | console.log((timeStamp() + 'Failed to reach exams page\n' + err.stack).red); |
||
|
1 ignored issue
–
show
|
|||
| 180 | res.send({ error: '获取成绩失败' });
|
||
| 181 | return next(err); |
||
| 182 | } |
||
| 183 | fullLog && console.log((timeStamp() + 'Successfully entered exams page.').green); |
||
| 184 | |||
| 185 | $ = cheerio.load(iires.text); |
||
| 186 | |||
| 187 | ret.exams = {};
|
||
| 188 | ret['exams-count'] = 0; |
||
| 189 | |||
| 190 | $('#dataList tr').each(function (index) {
|
||
| 191 | if (index === 0) {
|
||
| 192 | return; |
||
| 193 | } |
||
| 194 | let element = $(this).find('td');
|
||
| 195 | let title = escaper.unescape(element.eq(3).text()); |
||
| 196 | |||
| 197 | let item = {};
|
||
| 198 | item.time = escaper.unescape(element.eq(4).text()); |
||
| 199 | item.location = escaper.unescape(element.eq(5).text()); |
||
| 200 | item.seat = escaper.unescape(element.eq(6).text()); |
||
| 201 | |||
| 202 | ret.exams[title] = item; |
||
| 203 | ret['exams-count']++; |
||
| 204 | }); |
||
| 205 | |||
| 206 | access.logout(headers, res, function() {
|
||
| 207 | res.send(JSON.stringify(ret)); |
||
| 208 | fullLog && console.log((timeStamp() + 'Successfully logged out: ').green + req.query.id.yellow + (' (processed in ' + (new Date() - start) + 'ms)').green);
|
||
|
1 ignored issue
–
show
|
|||
| 209 | }); |
||
| 210 | }); |
||
| 211 | }); |
||
| 212 | }); |
||
| 213 | |||
| 216 |