Skip to content

Commit cdf819e

Browse files
committed
Added support for opting out endpoint translation
Fixed some translation issue Continue to fix some translation problem fix: fixed test cases Fixed all test cases relating to getProblem(s) to adapt new parameter fix: fixed test_leetcode testcase
1 parent bca58ab commit cdf819e

15 files changed

+94
-65
lines changed

lib/commands/list.js

+6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ const cmd = {
2828
default: false,
2929
describe: 'Show extra details: category, companies, tags.'
3030
})
31+
.option('T', {
32+
alias: 'dontTranslate',
33+
type: 'boolean',
34+
default: false,
35+
describe: 'Set to true to disable endpoint\'s translation',
36+
})
3137
.positional('keyword', {
3238
type: 'string',
3339
default: '',

lib/commands/show.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ const cmd = {
5757
default: false,
5858
describe: 'Show extra question details in source code'
5959
})
60+
.option('T', {
61+
alias: 'dontTranslate',
62+
type: 'boolean',
63+
default: false,
64+
describe: 'Set to true to disable endpoint\'s translation',
65+
})
6066
.positional('keyword', {
6167
type: 'string',
6268
default: '',
@@ -175,7 +181,7 @@ cmd.handler = function(argv) {
175181
session.argv = argv;
176182
if (argv.keyword.length > 0) {
177183
// show specific one
178-
core.getProblem(argv.keyword, function(e, problem) {
184+
core.getProblem(argv.keyword, !argv.dontTranslate, function(e, problem) {
179185
if (e) return log.fail(e);
180186
showProblem(problem, argv);
181187
});
@@ -194,7 +200,7 @@ cmd.handler = function(argv) {
194200
if (problems.length === 0) return log.fail('Problem not found!');
195201

196202
const problem = _.sample(problems);
197-
core.getProblem(problem, function(e, problem) {
203+
core.getProblem(problem, !argv.dontTranslate, function(e, problem) {
198204
if (e) return log.fail(e);
199205
showProblem(problem, argv);
200206
});

lib/commands/star.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ const cmd = {
2929

3030
cmd.handler = function(argv) {
3131
session.argv = argv;
32-
core.getProblem(argv.keyword, function(e, problem) {
32+
// translation doesn't affect question lookup
33+
core.getProblem(argv.keyword, true, function(e, problem) {
3334
if (e) return log.fail(e);
3435

3536
core.starProblem(problem, !argv.delete, function(e, starred) {

lib/commands/submission.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ const cmd = {
4242
default: false,
4343
describe: 'Show extra question details in submission code'
4444
})
45+
.option('T', {
46+
alias: 'dontTranslate',
47+
type: 'boolean',
48+
default: false,
49+
describe: 'Set to true to disable endpoint\'s translation',
50+
})
4551
.positional('keyword', {
4652
type: 'string',
4753
default: '',
@@ -69,7 +75,7 @@ function doTask(problem, queue, cb) {
6975

7076
if (argv.extra) {
7177
// have to get problem details, e.g. problem description.
72-
core.getProblem(problem.fid, function(e, problem) {
78+
core.getProblem(problem.fid, !argv.dontTranslate, function(e, problem) {
7379
if (e) return cb(e);
7480
exportSubmission(problem, argv, onTaskDone);
7581
});
@@ -135,7 +141,7 @@ cmd.handler = function(argv) {
135141
if (!argv.keyword)
136142
return log.fail('missing keyword?');
137143

138-
core.getProblem(argv.keyword, function(e, problem) {
144+
core.getProblem(argv.keyword, !argv.dontTranslate, function(e, problem) {
139145
if (e) return log.fail(e);
140146
q.addTask(problem).run();
141147
});

lib/commands/submit.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ cmd.handler = function(argv) {
4949

5050
const meta = file.meta(argv.filename);
5151

52-
core.getProblem(meta.id, function(e, problem) {
52+
// translation doesn't affect problem lookup
53+
core.getProblem(meta.id, true, function(e, problem) {
5354
if (e) return log.fail(e);
5455

5556
problem.file = argv.filename;

lib/commands/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function runTest(argv) {
6060

6161
const meta = file.meta(argv.filename);
6262

63-
core.getProblem(meta.id, function(e, problem) {
63+
core.getProblem(meta.id, true, function(e, problem) {
6464
if (e) return log.fail(e);
6565

6666
if (!problem.testable)

lib/core.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const QUERY_HANDLERS = {
6161
};
6262

6363
core.filterProblems = function(opts, cb) {
64-
this.getProblems(function(e, problems) {
64+
this.getProblems(!opts.dontTranslate, function(e, problems) {
6565
if (e) return cb(e);
6666

6767
for (let q of (opts.query || '').split('')) {
@@ -82,11 +82,11 @@ core.filterProblems = function(opts, cb) {
8282
});
8383
};
8484

85-
core.getProblem = function(keyword, cb) {
85+
core.getProblem = function(keyword, needTranslation, cb) {
8686
if (keyword.id)
87-
return core.next.getProblem(keyword, cb);
87+
return core.next.getProblem(keyword, needTranslation, cb);
8888

89-
this.getProblems(function(e, problems) {
89+
this.getProblems(needTranslation, function(e, problems) {
9090
if (e) return cb(e);
9191

9292
keyword = Number(keyword) || keyword;
@@ -95,7 +95,7 @@ core.getProblem = function(keyword, cb) {
9595
return x.fid + '' === keyword + '' || x.fid + '' === metaFid + '' || x.name === keyword || x.slug === keyword;
9696
});
9797
if (!problem) return cb('Problem not found!');
98-
core.next.getProblem(problem, cb);
98+
core.next.getProblem(problem, needTranslation, cb);
9999
});
100100
};
101101

lib/plugins/cache.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ var session = require('../session');
99

1010
const plugin = new Plugin(50, 'cache', '', 'Plugin to provide local cache.');
1111

12-
plugin.getProblems = function(cb) {
12+
plugin.getProblems = function(needTranslation, cb) {
1313
const problems = cache.get(h.KEYS.problems);
1414
if (problems) {
1515
log.debug('cache hit: problems.json');
1616
return cb(null, problems);
1717
}
1818

19-
plugin.next.getProblems(function(e, problems) {
19+
plugin.next.getProblems(needTranslation, function(e, problems) {
2020
if (e) return cb(e);
2121

2222
cache.set(h.KEYS.problems, problems);
2323
return cb(null, problems);
2424
});
2525
};
2626

27-
plugin.getProblem = function(problem, cb) {
27+
plugin.getProblem = function(problem, needTranslation, cb) {
2828
const k = h.KEYS.problem(problem);
2929
const _problem = cache.get(k);
3030
if (_problem) {
@@ -42,7 +42,7 @@ plugin.getProblem = function(problem, cb) {
4242
}
4343
}
4444

45-
plugin.next.getProblem(problem, function(e, _problem) {
45+
plugin.next.getProblem(problem, needTranslation, function(e, _problem) {
4646
if (e) return cb(e);
4747

4848
plugin.saveProblem(_problem);

lib/plugins/company.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1511,8 +1511,8 @@ var TAGS = {
15111511
'1148': ['math']
15121512
};
15131513

1514-
plugin.getProblems = function(cb) {
1515-
plugin.next.getProblems(function(e, problems) {
1514+
plugin.getProblems = function(needTranslation, cb) {
1515+
plugin.next.getProblems(needTranslation, function(e, problems) {
15161516
if (e) return cb(e);
15171517

15181518
problems.forEach(function(problem) {

lib/plugins/leetcode.cn.js

+18-10
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,29 @@ function checkError(e, resp, expectedStatus) {
7070
return e;
7171
}
7272

73-
plugin.getProblems = function(cb) {
74-
plugin.next.getProblems(function(e, problems) {
73+
// overloading getProblems here to make sure everything related
74+
// to listing out problems can have a chance to be translated.
75+
// NOTE: Details of the problem is translated inside leetcode.js
76+
plugin.getProblems = function (needTranslation, cb) {
77+
plugin.next.getProblems(needTranslation, function(e, problems) {
7578
if (e) return cb(e);
7679

77-
plugin.getProblemsTitle(function(e, titles) {
78-
if (e) return cb(e);
80+
if (needTranslation) {
81+
// only translate titles of the list if user requested
82+
plugin.getProblemsTitle(function (e, titles) {
83+
if (e) return cb(e);
7984

80-
problems.forEach(function(problem) {
81-
const title = titles[problem.id];
82-
if (title)
83-
problem.name = title;
84-
});
85+
problems.forEach(function (problem) {
86+
const title = titles[problem.id];
87+
if (title)
88+
problem.name = title;
89+
});
8590

91+
return cb(null, problems);
92+
});
93+
} else {
8694
return cb(null, problems);
87-
});
95+
}
8896
});
8997
};
9098

lib/plugins/leetcode.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ plugin.init = function() {
5454
config.app = 'leetcode';
5555
};
5656

57-
plugin.getProblems = function(cb) {
57+
plugin.getProblems = function (needTranslation, cb) {
5858
log.debug('running leetcode.getProblems');
5959
let problems = [];
6060
const getCategory = function(category, queue, cb) {
@@ -117,7 +117,7 @@ plugin.getCategoryProblems = function(category, cb) {
117117
});
118118
};
119119

120-
plugin.getProblem = function(problem, cb) {
120+
plugin.getProblem = function(problem, needTranslation, cb) {
121121
log.debug('running leetcode.getProblem');
122122
const user = session.getUser();
123123
if (problem.locked && !user.paid) return cb('failed to load locked problem!');
@@ -161,7 +161,7 @@ plugin.getProblem = function(problem, cb) {
161161
problem.likes = q.likes;
162162
problem.dislikes = q.dislikes;
163163

164-
problem.desc = q.translatedContent ? q.translatedContent : q.content;
164+
problem.desc = (q.translatedContent && needTranslation) ? q.translatedContent : q.content;
165165

166166
problem.templates = JSON.parse(q.codeDefinition);
167167
problem.testcase = q.sampleTestCase;

lib/plugins/solution.discuss.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ function getSolution(problem, lang, cb) {
7171
});
7272
}
7373

74-
plugin.getProblem = function(problem, cb) {
75-
plugin.next.getProblem(problem, function(e, problem) {
74+
plugin.getProblem = function(problem, needTranslation, cb) {
75+
plugin.next.getProblem(problem, needTranslation, function(e, problem) {
7676
if (e || !session.argv.solution) return cb(e, problem);
7777

7878
var lang = session.argv.lang;

test/plugins/test_cache.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('plugin:cache', function() {
5252
it('should getProblems w/ cache ok', function(done) {
5353
cache.set('problems', PROBLEMS);
5454

55-
plugin.getProblems(function(e, problems) {
55+
plugin.getProblems(false, function(e, problems) {
5656
assert.equal(e, null);
5757
assert.deepEqual(problems, PROBLEMS);
5858
done();
@@ -61,9 +61,9 @@ describe('plugin:cache', function() {
6161

6262
it('should getProblems w/o cache ok', function(done) {
6363
cache.del('problems');
64-
next.getProblems = cb => cb(null, PROBLEMS);
64+
next.getProblems = (needT, cb) => cb(null, PROBLEMS);
6565

66-
plugin.getProblems(function(e, problems) {
66+
plugin.getProblems(false, function(e, problems) {
6767
assert.equal(e, null);
6868
assert.deepEqual(problems, PROBLEMS);
6969
done();
@@ -72,9 +72,9 @@ describe('plugin:cache', function() {
7272

7373
it('should getProblems w/o cache fail if client error', function(done) {
7474
cache.del('problems');
75-
next.getProblems = cb => cb('client getProblems error');
75+
next.getProblems = (needT, cb) => cb('client getProblems error');
7676

77-
plugin.getProblems(function(e, problems) {
77+
plugin.getProblems(false, function(e, problems) {
7878
assert.equal(e, 'client getProblems error');
7979
done();
8080
});
@@ -86,7 +86,7 @@ describe('plugin:cache', function() {
8686
cache.set('problems', PROBLEMS);
8787
cache.set('0.slug0.algorithms', PROBLEMS[0]);
8888

89-
plugin.getProblem(_.clone(PROBLEM), function(e, problem) {
89+
plugin.getProblem(_.clone(PROBLEM), false, function(e, problem) {
9090
assert.equal(e, null);
9191
assert.deepEqual(problem, PROBLEMS[0]);
9292
done();
@@ -96,9 +96,9 @@ describe('plugin:cache', function() {
9696
it('should getProblem w/o cache ok', function(done) {
9797
cache.set('problems', PROBLEMS);
9898
cache.del('0.slug0.algorithms');
99-
next.getProblem = (problem, cb) => cb(null, PROBLEMS[0]);
99+
next.getProblem = (problem, needT, cb) => cb(null, PROBLEMS[0]);
100100

101-
plugin.getProblem(_.clone(PROBLEM), function(e, problem) {
101+
plugin.getProblem(_.clone(PROBLEM), false, function(e, problem) {
102102
assert.equal(e, null);
103103
assert.deepEqual(problem, PROBLEMS[0]);
104104
done();
@@ -108,9 +108,9 @@ describe('plugin:cache', function() {
108108
it('should getProblem fail if client error', function(done) {
109109
cache.set('problems', PROBLEMS);
110110
cache.del('0.slug0.algorithms');
111-
next.getProblem = (problem, cb) => cb('client getProblem error');
111+
next.getProblem = (problem, needT, cb) => cb('client getProblem error');
112112

113-
plugin.getProblem(_.clone(PROBLEM), function(e, problem) {
113+
plugin.getProblem(_.clone(PROBLEM), false, function(e, problem) {
114114
assert.equal(e, 'client getProblem error');
115115
done();
116116
});
@@ -140,7 +140,7 @@ describe('plugin:cache', function() {
140140
const ret = plugin.updateProblem(PROBLEMS[0], kv);
141141
assert.equal(ret, true);
142142

143-
plugin.getProblems(function(e, problems) {
143+
plugin.getProblems(false, function(e, problems) {
144144
assert.equal(e, null);
145145
assert.deepEqual(problems, [
146146
{id: 0, fid: 0, name: 'name0', slug: 'slug0', value: 'value00', starred: false, desc: '<pre></pre>', likes: '1', dislikes: '1', category: 'algorithms'},

test/plugins/test_leetcode.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ describe('plugin:leetcode', function() {
125125
.get('/api/problems/concurrency/')
126126
.replyWithFile(200, './test/mock/problems.json.20160911');
127127

128-
plugin.getProblems(function(e, problems) {
128+
plugin.getProblems(false, function(e, problems) {
129129
assert.equal(e, null);
130130
assert.equal(problems.length, 377 * 4);
131131
done();
@@ -149,7 +149,7 @@ describe('plugin:leetcode', function() {
149149
.get('/api/problems/concurrency/')
150150
.replyWithFile(200, './test/mock/problems.json.20160911');
151151

152-
plugin.getProblems(function(e, problems) {
152+
plugin.getProblems(false, function(e, problems) {
153153
assert.equal(e.message, 'unknown error');
154154
done();
155155
});
@@ -192,7 +192,7 @@ describe('plugin:leetcode', function() {
192192
.post('/graphql')
193193
.replyWithFile(200, './test/mock/find-the-difference.json.20171216');
194194

195-
plugin.getProblem(PROBLEM, function(e, problem) {
195+
plugin.getProblem(PROBLEM, false, function(e, problem) {
196196
assert.equal(e, null);
197197
assert.equal(problem.totalAC, '89.7K');
198198
assert.equal(problem.totalSubmit, '175.7K');
@@ -367,7 +367,7 @@ describe('plugin:leetcode', function() {
367367
it('should fail if no permission for locked', function(done) {
368368
PROBLEM.locked = true;
369369

370-
plugin.getProblem(PROBLEM, function(e, problem) {
370+
plugin.getProblem(PROBLEM, false, function(e, problem) {
371371
assert.equal(e, 'failed to load locked problem!');
372372
done();
373373
});
@@ -376,7 +376,7 @@ describe('plugin:leetcode', function() {
376376
it('should fail if session expired', function(done) {
377377
nock('https://leetcode.com').post('/graphql').reply(403);
378378

379-
plugin.getProblem(PROBLEM, function(e, problem) {
379+
plugin.getProblem(PROBLEM, false, function(e, problem) {
380380
assert.equal(e, session.errors.EXPIRED);
381381
done();
382382
});
@@ -385,7 +385,7 @@ describe('plugin:leetcode', function() {
385385
it('should fail if http error', function(done) {
386386
nock('https://leetcode.com').post('/graphql').reply(500);
387387

388-
plugin.getProblem(PROBLEM, function(e, problem) {
388+
plugin.getProblem(PROBLEM, false, function(e, problem) {
389389
assert.deepEqual(e, {msg: 'http error', statusCode: 500});
390390
done();
391391
});
@@ -394,7 +394,7 @@ describe('plugin:leetcode', function() {
394394
it('should fail if unknown error', function(done) {
395395
nock('https://leetcode.com').post('/graphql').replyWithError('unknown error!');
396396

397-
plugin.getProblem(PROBLEM, function(e, problem) {
397+
plugin.getProblem(PROBLEM, false, function(e, problem) {
398398
assert.equal(e.message, 'unknown error!');
399399
done();
400400
});

0 commit comments

Comments
 (0)