Skip to content

Commit 85afa50

Browse files
committed
Use builtin functions.
* Array.prototype.forEach * Array.prototype.filter * Array.prototype.find * Array.prototype.map * Array.prototype.some * Array.isArray * Number.isNaN * String.prototype.includes Signed-off-by: Eric Wang <[email protected]>
1 parent 37c26bb commit 85afa50

File tree

16 files changed

+29
-30
lines changed

16 files changed

+29
-30
lines changed

lib/chalk.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ chalk.wrap = function(pre, post) {
5353
function bgName(s) { return 'bg' + s[0].toUpperCase() + s.substr(1); }
5454

5555
chalk.init = function() {
56-
_.each(require('./helper').getCodeDirData('colors'), function(f) {
56+
require('./helper').getCodeDirData('colors').forEach(function(f) {
5757
var o = {};
5858
_.pairs(f.data).forEach(function(x) {
5959
var k = x[0];

lib/commands/cache.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ cmd.handler = function(argv) {
4242
} else {
4343
_.sortBy(caches, function(f) {
4444
var x = parseInt(f.name.split('.')[0], 10);
45-
if (_.isNaN(x)) x = 0;
45+
if (Number.isNaN(x)) x = 0;
4646
return x;
4747
})
4848
.forEach(function(f) {

lib/commands/list.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ cmd.handler = function(argv) {
5151
if (word.endsWith(word.substr(-1).repeat(6))) {
5252
log.warn('Hmmm...you might need a new keyboard?');
5353
}
54-
problems = _.filter(problems, function(x) {
55-
return x.name.toLowerCase().indexOf(word) >= 0;
54+
problems = problems.filter(function(x) {
55+
return x.name.toLowerCase().includes(word);
5656
});
5757
}
5858

lib/commands/show.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function showProblem(problem, argv) {
8888
var code;
8989
var needcode = argv.gen || argv.codeonly;
9090
if (needcode) {
91-
var template = _.find(problem.templates, function(x) {
91+
var template = problem.templates.find(function(x) {
9292
return x.value === argv.lang;
9393
});
9494
if (!template) {
@@ -166,14 +166,14 @@ cmd.handler = function(argv) {
166166

167167
// random select one that not AC-ed yet
168168
var user = session.getUser();
169-
problems = _.filter(problems, function(x) {
169+
problems = problems.filter(function(x) {
170170
if (x.state === 'ac') return false;
171171
if (!user.paid && x.locked) return false;
172172
return true;
173173
});
174174
if (problems.length === 0) return cb('Problem not found!');
175175

176-
var problem = problems[_.random(problems.length - 1)];
176+
var problem = _.sample(problems);
177177
core.getProblem(problem, function(e, problem) {
178178
if (e) return log.fail(e);
179179
showProblem(problem, argv);

lib/commands/stat.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function showGraph(problems) {
109109
log.info(' ' + header);
110110

111111
var graph = [];
112-
_.each(problems, function(problem) {
112+
problems.forEach(function(problem) {
113113
graph[problem.id] = ICONS[problem.state] || ICONS.none;
114114
});
115115

@@ -214,7 +214,7 @@ cmd.handler = function(argv) {
214214
if (e) return log.fail(e);
215215

216216
if (!argv.lock)
217-
problems = _.reject(problems, function(x) { return x.locked; });
217+
problems = problems.filter(function(x) { return !x.locked; });
218218

219219
log.info();
220220
if (argv.graph) showGraph(problems);

lib/commands/submission.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var fs = require('fs');
22

3-
var _ = require('underscore');
43
var sprintf = require('sprintf-js').sprintf;
54

65
var h = require('../helper');
@@ -81,14 +80,14 @@ function exportSubmission(problem, argv, cb) {
8180
return cb('No submissions?');
8281

8382
// get obj list contain required filetype
84-
submissions = _.filter(submissions, function(x) {
83+
submissions = submissions.filter(function(x) {
8584
return argv.lang === 'all' || argv.lang === x.lang;
8685
});
8786
if (submissions.length === 0)
8887
return cb('No submissions in required language.');
8988

9089
// if no accepted, use the latest non-accepted one
91-
var submission = _.find(submissions, function(x) {
90+
var submission = submissions.find(function(x) {
9291
return x.status_display === 'Accepted';
9392
}) || submissions[0];
9493
submission.ac = (submission.status_display === 'Accepted');

lib/commands/submit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function printResult(actual, k) {
2828
if (!actual.hasOwnProperty(k)) return;
2929

3030
var v = actual[k] || '';
31-
var lines = _.isArray(v) ? v : [v];
31+
var lines = Array.isArray(v) ? v : [v];
3232
lines.forEach(function(line) {
3333
if (k !== 'state') line = k + ': ' + line;
3434
log.info(' ' + h.prettyText(' ' + line, actual.ok));

lib/commands/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function printResult(actual, expect, k) {
4444
var ok = actual.ok;
4545
if (expect && !_.isEqual(actual[k], expect[k])) ok = false;
4646

47-
var lines = _.isArray(v) ? v : [v];
47+
var lines = Array.isArray(v) ? v : [v];
4848
lines.forEach(function(line) {
4949
if (k !== 'state') line = k + ': ' + line;
5050
log.info(' ' + h.prettyText(' ' + line, ok));

lib/commands/version.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ cmd.handler = function(argv) {
7070
printLine('Icons', _.keys(icon.themes));
7171

7272
log.info('\n[Plugins]');
73-
_.each(Plugin.plugins, function(p, k) {
73+
Plugin.plugins.forEach(function(p, k) {
7474
printLine(p.name, p.ver);
7575
});
7676
};

lib/core.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ core.filters = {
3434
};
3535

3636
function hasTag(o, tag) {
37-
return _.isArray(o) &&
38-
_.some(o, function(x) { return x.indexOf(tag.toLowerCase()) >= 0; });
37+
return Array.isArray(o) &&
38+
o.some(function(x) { return x.indexOf(tag.toLowerCase()) >= 0; });
3939
}
4040

4141
function isLevel(x, q) { return x.level[0].toLowerCase() === q.toLowerCase(); }
@@ -65,11 +65,11 @@ core.filterProblems = function(opts, cb) {
6565
(opts.query || '').split('').forEach(function(q) {
6666
var f = QUERY_HANDLERS[q];
6767
if (!f) return;
68-
problems = _.filter(problems, function(x) { return f(x, q); });
68+
problems = problems.filter(function(x) { return f(x, q); });
6969
});
7070

7171
(opts.tag || []).forEach(function(t) {
72-
problems = _.filter(problems, function(x) {
72+
problems = problems.filter(function(x) {
7373
return x.category === t ||
7474
hasTag(x.companies, t) ||
7575
hasTag(x.tags, t);
@@ -88,7 +88,7 @@ core.getProblem = function(keyword, cb) {
8888
if (e) return cb(e);
8989

9090
keyword = Number(keyword) || keyword;
91-
var problem = _.find(problems, function(x) {
91+
var problem = problems.find(function(x) {
9292
return x.id === keyword || x.name === keyword || x.slug === keyword;
9393
});
9494
if (!problem) return cb('Problem not found!');

0 commit comments

Comments
 (0)