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

16 files changed

+29
-30
lines changed

Diff for: lib/chalk.js

+1-1
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];

Diff for: lib/commands/cache.js

+1-1
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) {

Diff for: lib/commands/list.js

+2-2
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

Diff for: lib/commands/show.js

+3-3
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);

Diff for: lib/commands/stat.js

+2-2
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);

Diff for: lib/commands/submission.js

+2-3
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');

Diff for: lib/commands/submit.js

+1-1
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));

Diff for: lib/commands/test.js

+1-1
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));

Diff for: lib/commands/version.js

+1-1
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
};

Diff for: lib/core.js

+5-5
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!');

Diff for: lib/helper.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ h.statusToName = function(sc) {
123123
};
124124

125125
h.langToExt = function(lang) {
126-
var res = _.find(LANGS, function(x) { return x.lang === lang; });
126+
var res = LANGS.find(function(x) { return x.lang === lang; });
127127
return res ? res.ext : '.raw';
128128
};
129129

@@ -139,7 +139,7 @@ h.extToLang = function(fullpath) {
139139
};
140140

141141
h.langToCommentStyle = function(lang) {
142-
var res = _.find(LANGS, function(x) { return x.lang === lang; });
142+
var res = LANGS.find(function(x) { return x.lang === lang; });
143143

144144
return (res && res.style === '#') ?
145145
{start: '#', line: '#', end: '#'} :
@@ -153,7 +153,7 @@ h.mkdir = function(fullpath) {
153153

154154
h.getCodeDirData = function(dir) {
155155
dir = h.getCodeDir(dir);
156-
return _.map(fs.readdirSync(dir), function(file) {
156+
return fs.readdirSync(dir).map(function(file) {
157157
var fullpath = path.join(dir, file);
158158
var ext = path.extname(file);
159159

Diff for: lib/icon.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ icons.setTheme = function(name) {
2020
};
2121

2222
icons.init = function() {
23-
_.each(h.getCodeDirData('icons'), function(f) {
23+
h.getCodeDirData('icons').forEach(function(f) {
2424
icons.themes[f.name] = f.data;
2525
});
2626
};

Diff for: lib/plugin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function Plugin(id, name, ver, desc, deps) {
1818

1919
// only need deps for current platform
2020
this.deps = _.chain(deps || [])
21-
.filter(function(x) { return x.indexOf(':') < 0 || x.indexOf(':' + process.platform) > 0; })
21+
.filter(function(x) { return ! x.includes(':') || x.includes(':' + process.platform); })
2222
.map(function(x) { return x.split(':')[0]; })
2323
.value();
2424
}

Diff for: lib/plugins/cache.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ plugin.updateProblem = function(problem, kv) {
5757
var problems = cache.get(h.KEYS.problems);
5858
if (!problems) return false;
5959

60-
var _problem = _.find(problems, function(x) { return x.id === problem.id; });
60+
var _problem = problems.find(function(x) { return x.id === problem.id; });
6161
if (!_problem) return false;
6262

6363
_.extend(_problem, kv);

Diff for: lib/plugins/leetcode.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ function runCode(opts, problem, cb) {
189189
if (e) return cb(e);
190190

191191
if (body.error) {
192-
if (body.error.indexOf('too soon') < 0)
192+
if (!body.error.includes('too soon'))
193193
return cb(body.error);
194194

195195
// hit 'run code too soon' error, have to wait a bit
@@ -312,7 +312,7 @@ plugin.getSubmissions = function(problem, cb) {
312312

313313
// FIXME: this only return the 1st 20 submissions, we should get next if necessary.
314314
var submissions = JSON.parse(body).submissions_dump;
315-
_.each(submissions, function(submission) {
315+
submissions.forEach(function(submission) {
316316
submission.id = _.last(_.compact(submission.url.split('/')));
317317
});
318318

@@ -419,7 +419,7 @@ plugin.getUser = function(user, cb) {
419419
plugin.getFavorites(function(e, favorites) {
420420
if (e) return cb(e);
421421

422-
var favorite = _.find(favorites.favorites.private_favorites, function(f) {
422+
var favorite = favorites.favorites.private_favorites.find(function(f) {
423423
return f.name === 'Favorite';
424424
});
425425
user.hash = favorite.id_hash;

Diff for: templates/detailed.tpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
<%= comment.line %> Total Submissions: <%= totalSubmit %>
1010
<%= comment.line %> Testcase Example: <%= testcase %>
1111
<%= comment.line %>
12-
<% _.each(desc, function(x) { %><%= comment.line %> <%= x %>
12+
<% desc.forEach(function(x) { %><%= comment.line %> <%= x %>
1313
<% }) %><%= comment.end %>
1414
<%= code %>

0 commit comments

Comments
 (0)