Conditions | 1 |
Paths | 8 |
Total Lines | 82 |
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 |
||
75 | access.login(req.query.id, req.query.pwd, res, function (headers, ires) { |
||
76 | fullLog && console.log((timeStamp() + 'Successfully logged in.').green); |
||
1 ignored issue
–
show
|
|||
77 | |||
78 | var ret = {}; |
||
79 | var $ = cheerio.load(ires.text); |
||
80 | |||
81 | ret.name = escaper.unescape($('.block1text').html()).match(/姓名:.+</)[0].substring(3).replace(/</, ''); |
||
82 | ret.id = req.query.id; |
||
83 | |||
84 | // 实际上xnxq01id为空的时候和GET这个URL的效果是一样的,都是查询所有学期 |
||
85 | superagent |
||
86 | .post('http://csujwc.its.csu.edu.cn/jsxsd/kscj/yscjcx_list') |
||
87 | .set(headers) |
||
88 | .type('form') |
||
89 | .send({ |
||
90 | xnxq01id: req.query.sem |
||
91 | }) |
||
92 | .end(function (err, iires) { |
||
93 | if (err) { |
||
94 | console.log((timeStamp() + 'Failed to get grades page\n' + err.stack).red); |
||
1 ignored issue
–
show
|
|||
95 | res.send({ error: '无法进入成绩页面' }); |
||
96 | return next(err); |
||
97 | } |
||
98 | fullLog && console.log((timeStamp() + 'Successfully entered grades page.').green); |
||
99 | |||
100 | $ = cheerio.load(iires.text); |
||
101 | |||
102 | ret.grades = {}; |
||
103 | ret.failed = {}; |
||
104 | |||
105 | // 获取成绩列表 |
||
106 | $('#dataList tr').each(function (index) { |
||
107 | if (index === 0) { |
||
108 | return; |
||
109 | } |
||
110 | let element = $(this).find('td'); |
||
111 | let title = escaper.unescape(element.eq(3).text().match(/].+$/)[0].substring(1)); |
||
112 | |||
113 | let item = {}; |
||
114 | item.sem = escaper.unescape(element.eq(2).text()); |
||
115 | item.regular = escaper.unescape(element.eq(4).text()); |
||
116 | item.exam = escaper.unescape(element.eq(5).text()); |
||
117 | item.overall = escaper.unescape(element.eq(6).text()); |
||
118 | if (req.query.details) { |
||
119 | item.id = escaper.unescape(element.eq(3).text().match(/\[.+\]/)[0].replace(/\[|\]/g, '')); |
||
120 | item.attr = escaper.unescape(element.eq(8).text()); |
||
121 | item.genre = escaper.unescape(element.eq(9).text()); |
||
122 | item.credit = escaper.unescape(element.eq(7).text()); |
||
123 | } |
||
124 | |||
125 | // 如果有补考记录,则以最高分的为准 |
||
126 | if (title in ret.grades) { |
||
127 | if (item.overall < ret.grades[title].overall) { |
||
128 | return; |
||
129 | } |
||
130 | if (!element.eq(6).css('color')) { |
||
131 | delete ret.failed[title]; |
||
132 | } |
||
133 | } else { |
||
134 | if (element.eq(6).css('color')) { |
||
135 | ret.failed[title] = item; |
||
136 | } |
||
137 | } |
||
138 | |||
139 | ret.grades[title] = item; |
||
140 | }); |
||
141 | |||
142 | ret['subject-count'] = Object.keys(ret.grades).length; |
||
143 | ret['failed-count'] = Object.keys(ret.failed).length; |
||
144 | |||
145 | access.logout(headers, res, function() { |
||
146 | // 返回JSON |
||
147 | res.send(JSON.stringify(ret)); |
||
148 | fullLog && console.log((timeStamp() + |
||
1 ignored issue
–
show
|
|||
149 | 'Successfully logged out: ').green + |
||
150 | req.query.id.yellow + |
||
151 | (' (processed in ' + |
||
152 | (new Date() - start) + 'ms)' |
||
153 | ).green); |
||
154 | }); |
||
155 | }); |
||
156 | }); |
||
157 | }); |
||
231 |