Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7236c60

Browse files
Refactor filtering in list
Signed-off-by: Eric Wang <skygragon@gmail.com>
1 parent ee0ded4 commit 7236c60

File tree

3 files changed

+125
-64
lines changed

3 files changed

+125
-64
lines changed

‎lib/commands/list.js‎

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -54,68 +54,12 @@ var cmd = {
5454
}
5555
};
5656

57-
function byLevel(x, q) {
58-
return x.level[0].toLowerCase() === q.toLowerCase();
59-
}
60-
61-
function byStateAC(x, q) {
62-
return x.state === 'ac';
63-
}
64-
65-
function byLocked(x, q) {
66-
return x.locked;
67-
}
68-
69-
function byStarred(x, q) {
70-
return x.starred;
71-
}
72-
73-
var QUERY_HANDLERS = {
74-
e: byLevel,
75-
E: _.negate(byLevel),
76-
m: byLevel,
77-
M: _.negate(byLevel),
78-
h: byLevel,
79-
H: _.negate(byLevel),
80-
l: byLocked,
81-
L: _.negate(byLocked),
82-
d: byStateAC,
83-
D: _.negate(byStateAC),
84-
s: byStarred,
85-
S: _.negate(byStarred)
86-
};
87-
88-
function hasTag(o, tag) {
89-
return _.isArray(o) &&
90-
_.some(o, function(x) { return x.indexOf(tag.toLowerCase()) >= 0; });
91-
}
92-
9357
cmd.handler = function(argv) {
9458
session.argv = argv;
95-
core.getProblems(function(e, problems) {
59+
core.filterProblems(argv,function(e, problems) {
9660
if (e) return log.fail(e);
9761

98-
var all = problems.length;
99-
100-
if (argv.query) {
101-
argv.query.split('').forEach(function(q) {
102-
var f = QUERY_HANDLERS[q];
103-
if (!f) return;
104-
105-
problems = _.filter(problems, _.partial(f, _, q));
106-
});
107-
}
108-
109-
argv.tag.forEach(function(tag) {
110-
// TODO: fill company/tags in problems
111-
problems = _.filter(problems, function(p) {
112-
return p.category === tag ||
113-
hasTag(p.companies, tag) ||
114-
hasTag(p.tags, tag);
115-
});
116-
});
117-
118-
var word = String(argv.keyword).toLowerCase();
62+
var word = argv.keyword.toLowerCase();
11963
if (word) {
12064
if (word.endsWith(word.substr(-1).repeat(6))) {
12165
log.warn('Hmmm...you might need a new keyboard?');
@@ -148,10 +92,9 @@ cmd.handler = function(argv) {
14892

14993
if (argv.stat) {
15094
log.info();
151-
log.printf(' All: %-9d Listed: %-9d', all, problems.length);
152-
log.printf(' Locked: %-9d Starred: %-9d', stat.locked, stat.starred);
153-
log.printf(' Accept: %-9d Not-AC: %-9d New: %-9d', stat.ac, stat.notac, stat.None);
154-
log.printf(' Easy: %-9d Medium: %-9d Hard: %-9d', stat.Easy, stat.Medium, stat.Hard);
95+
log.printf(' Listed: %-9d Locked: %-9d Starred: %-9d', problems.length, stat.locked, stat.starred);
96+
log.printf(' Accept: %-9d Not-AC: %-9d Remain: %-9d', stat.ac, stat.notac, stat.None);
97+
log.printf(' Easy: %-9d Medium: %-9d Hard: %-9d', stat.Easy, stat.Medium, stat.Hard);
15598
}
15699
});
157100
};

‎lib/core.js‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,53 @@ var session = require('./session');
1010

1111
var core = new Plugin(99999999, 'core', '20170722', 'Plugins manager');
1212

13+
function hasTag(o, tag) {
14+
return _.isArray(o) &&
15+
_.some(o, function(x) { return x.indexOf(tag.toLowerCase()) >= 0; });
16+
}
17+
18+
function isLevel(x, q) { return x.level[0].toLowerCase() === q.toLowerCase(); }
19+
function isACed(x) { return x.state === 'ac'; }
20+
function isLocked(x) { return x.locked; }
21+
function isStarred(x) { return x.starred; }
22+
23+
var QUERY_HANDLERS = {
24+
e: isLevel,
25+
E: _.negate(isLevel),
26+
m: isLevel,
27+
M: _.negate(isLevel),
28+
h: isLevel,
29+
H: _.negate(isLevel),
30+
l: isLocked,
31+
L: _.negate(isLocked),
32+
d: isACed,
33+
D: _.negate(isACed),
34+
s: isStarred,
35+
S: _.negate(isStarred)
36+
};
37+
38+
core.filterProblems = function(opts, cb) {
39+
this.getProblems(function(e, problems) {
40+
if (e) return cb(e);
41+
42+
(opts.query || '').split('').forEach(function(q) {
43+
var f = QUERY_HANDLERS[q];
44+
if (!f) return;
45+
problems = _.filter(problems, function(x) { return f(x, q); });
46+
});
47+
48+
(opts.tag || []).forEach(function(t) {
49+
problems = _.filter(problems, function(x) {
50+
return x.category === t ||
51+
hasTag(x.companies, t) ||
52+
hasTag(x.tags, t);
53+
});
54+
});
55+
56+
return cb(null, problems);
57+
});
58+
};
59+
1360
core.getProblem = function(keyword, cb) {
1461
this.getProblems(function(e, problems) {
1562
if (e) return cb(e);

‎test/test_core.js‎

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,28 @@ var plugin = rewire('../lib/core');
88

99
describe('core', function() {
1010
var PROBLEMS = [
11-
{id: 0, name: 'name0', slug: 'slug0', starred: false, category: 'algorithms'},
12-
{id: 1, name: 'name1', slug: 'slug1', starred: true, category: 'algorithms'}
11+
{
12+
category: 'algorithms',
13+
id: 0,
14+
name: 'name0',
15+
slug: 'slug0',
16+
level: 'Hard',
17+
locked: true,
18+
starred: false,
19+
state: 'ac',
20+
tags: ['google', 'facebook']
21+
},
22+
{
23+
category: 'algorithms',
24+
companies: ['amazon', 'facebook'],
25+
id: 1,
26+
name: 'name1',
27+
slug: 'slug1',
28+
level: 'Easy',
29+
locked: false,
30+
starred: true,
31+
state: 'none'
32+
}
1333
];
1434
var USER = {};
1535
var NEXT = {};
@@ -34,6 +54,57 @@ describe('core', function() {
3454
};
3555
});
3656

57+
describe('#filterProblems', function() {
58+
it('should filter by query ok', function(done) {
59+
var cases = [
60+
['', [0, 1]],
61+
['x', [0, 1]],
62+
['h', [0]],
63+
['H', [1]],
64+
['m', []],
65+
['M', [0, 1]],
66+
['l', [0]],
67+
['L', [1]],
68+
['s', [1]],
69+
['S', [0]],
70+
['d', [0]],
71+
['D', [1]],
72+
['eLsD', [1]],
73+
['Dh', []]
74+
];
75+
var n = cases.length;
76+
cases.forEach(function(x) {
77+
plugin.filterProblems({query: x[0]}, function(e, problems) {
78+
assert.equal(e, null);
79+
assert.equal(problems.length, x[1].length);
80+
for (var i = 0; i < problems.length; ++i)
81+
assert.equal(problems[i], PROBLEMS[x[1][i]]);
82+
if (--n === 0) done();
83+
});
84+
});
85+
});
86+
87+
it('should filter by tag ok', function(done) {
88+
var cases = [
89+
[[], [0, 1]],
90+
[['facebook'], [0, 1]],
91+
[['google'], [0]],
92+
[['amazon'], [1]],
93+
[['apple'], []],
94+
];
95+
var n = cases.length;
96+
cases.forEach(function(x) {
97+
plugin.filterProblems({tag: x[0]}, function(e, problems) {
98+
assert.equal(e, null);
99+
assert.equal(problems.length, x[1].length);
100+
for (var i = 0; i < problems.length; ++i)
101+
assert.equal(problems[i], PROBLEMS[x[1][i]]);
102+
if (--n === 0) done();
103+
});
104+
});
105+
});
106+
});
107+
37108
describe('#starProblem', function() {
38109
it('should starProblem ok', function(done) {
39110
NEXT.starProblem = function(problem, starred, cb) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /