Conditions | 4 |
Paths | 17 |
Total Lines | 62 |
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 |
||
52 | app.get('/grades/', function (req, res, next) { |
||
53 | if (!req.query.id || !req.query.pwd) { |
||
54 | res.send({ error: "参数不正确" }); |
||
55 | return; |
||
56 | } |
||
57 | if (program.fullLog) { |
||
58 | var start = new Date(); |
||
59 | console.log((timeStamp() + 'Started to query the grades: ').cyan + req.query.id.yellow); |
||
1 ignored issue
–
show
|
|||
60 | } |
||
61 | access.login(req.query.id, req.query.pwd, res, function (headers, ires) { |
||
62 | program.fullLog && console.log((timeStamp() + 'Successfully logged in.').green); |
||
1 ignored issue
–
show
|
|||
63 | var ret = {}; |
||
64 | var $ = cheerio.load(ires.text); |
||
65 | ret.name = escaper.unescape($('.block1text').html()).match(/姓名:.+</)[0].replace('<', '').substring(3); |
||
66 | ret.id = req.query.id; |
||
67 | |||
68 | // 进入成绩页面 |
||
69 | superagent.get(base + $('li[title="我的成绩"] a').attr('href')) |
||
70 | .set(headers) |
||
71 | .end(function (err, iires) { |
||
72 | if (err) { |
||
73 | console.log((timeStamp() + 'Failed to get grades page\n' + err.stack).red); |
||
1 ignored issue
–
show
|
|||
74 | res.send({ error: '无法进入成绩页面' }); |
||
75 | return next(err); |
||
76 | } |
||
77 | program.fullLog && console.log((timeStamp() + 'Successfully entered grades page.').green); |
||
78 | |||
79 | $ = cheerio.load(iires.text); |
||
80 | |||
81 | // 获取成绩列表 |
||
82 | let grades = {}; |
||
83 | let failed = {}; |
||
84 | $('#dataList').each(function (index) { |
||
85 | // cheerio没有实现jQuery的lt |
||
86 | if (index >= 2) |
||
87 | return; |
||
88 | $(this).find('tr[class!="theadCss"]').each(function() { |
||
89 | // 这段写得真是要吐血了 |
||
90 | let subject = escaper.unescape($(this).find('td[align="left"]').eq(1).text()); |
||
91 | if (subject) { |
||
92 | let score = $(this).find('font'); |
||
93 | if (score.text()) |
||
94 | grades[subject] = score.text(); |
||
95 | if (score.css('color')) |
||
96 | failed[subject] = score.text(); |
||
97 | } |
||
98 | }); |
||
99 | }); |
||
100 | ret.grades = grades; |
||
101 | ret['subject-count'] = Object.getOwnPropertyNames(grades).length; |
||
102 | ret.failed = failed; |
||
103 | ret['failed-count'] = Object.getOwnPropertyNames(failed).length; |
||
104 | |||
105 | // 完成所有工作后,登出 |
||
106 | access.logout(headers, res, function() { |
||
107 | // 第五步:返回JSON |
||
108 | res.send(JSON.stringify(ret)); |
||
109 | program.fullLog && console.log((timeStamp() + 'Successfully logged out: ').green + req.query.id.yellow + (' (processed in ' + (new Date() - start) + 'ms)').green); |
||
1 ignored issue
–
show
|
|||
110 | }); |
||
111 | }); |
||
112 | }); |
||
113 | }); |
||
114 | |||
191 | console.log((timeStamp() + 'The server is now running on port ' + port + '.').green); |