forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathhelper.js
89 lines (78 loc) · 2.57 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var fs = require('fs'),
path = require('path');
var h = {};
h.prettyState = function(state) {
switch(state) {
case 'ac': return '✔'; break;
case 'notac': return '✘'; break;
default: return ' '; break;
};
};
h.prettyYesNo = function(x) {
return x ? '✔' : '✘';
};
h.statusToName = function(sc) {
switch(sc) {
case 10: return 'Accepted'; break;
case 11: return 'Wrong Answer'; break;
case 12: return 'Memory Limit Exceeded'; break;
case 13: return 'Output Limit Exceeded'; break;
case 14: return 'Time Limit Exceeded'; break;
case 15: return 'Runtime Error'; break;
case 16: return 'Internal Error'; break;
case 20: return 'Compile Error'; break;
case 21: return 'Unknown Error'; break;
default: return 'Unknown'; break;
}
};
h.langToExt = function(lang) {
switch(lang) {
case 'c': return '.c'; break;
case 'cpp': return '.cpp'; break;
case 'csharp': return '.cs'; break;
case 'golang': return '.go'; break;
case 'java': return '.java'; break;
case 'javascript': return '.js'; break;
case 'python': return '.py'; break;
case 'ruby': return '.rb'; break;
case 'swift': return '.swift'; break;
default: return '.raw'; break;
}
};
h.extToLang = function(fullpath) {
var ext = path.extname(fullpath);
switch(ext) {
case '.c': return 'c'; break;
case '.cpp': return 'cpp'; break;
case '.cs': return 'csharp'; break;
case '.go': return 'golang'; break;
case '.java': return 'java'; break;
case '.js': return 'javascript'; break;
case '.py': return 'python'; break;
case '.rb': return 'ruby'; break;
case '.swift': return 'swift'; break;
default: return 'unknown'; break;
}
};
h.fileData = function(fullpath) {
return fs.readFileSync(fullpath).toString();
};
h.getFilename = function(fullpath) {
return path.basename(fullpath, path.extname(fullpath));
};
h.readStdin = function() {
// FIXME: not work for win32
return fs.readFileSync('/dev/stdin').toString();
};
h.getSetCookieValue = function(resp, key) {
var cookies = resp.headers['set-cookie'];
if (!cookies) return null;
for (var i=0; i<cookies.length; ++i) {
var sections = cookies[i].split(';');
for (var j=0; j<sections.length; ++j) {
var kv = sections[j].trim().split('=');
if (kv[0] == key) return kv[1];
}
}
};
module.exports = h;