-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtidy_code.js
More file actions
55 lines (48 loc) · 1.15 KB
/
tidy_code.js
File metadata and controls
55 lines (48 loc) · 1.15 KB
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
const beautify = require('js-beautify').js;
function tidy_js_tab(code) {
return beautify(code, {indent_size: 4, space_in_empty_paren: true});
}
function tidy_c_tab(code,) {
const arr = code.split('\n');
let tab_cnt = 0;
return arr.map((_line) => {
const line = _line.trim();
if (line.indexOf('}') >= 0) {
tab_cnt --;
}
let ret = '';
let tab = ' ';
for (let i = 0; i < tab_cnt; ++ i) {
ret += tab;
}
ret += line;
if (line.indexOf('{') >= 0) {
tab_cnt ++;
}
return ret;
}).join('\n');
}
function tidy_blank(code) {
const arr = code.split('\n');
const ret = [];
for (let i = 0, len = arr.length; i < len; ++ i) {
if (!(0 === arr[i].trim().length)) {
ret.push(arr[i].replace(/\n/g, ''));
}
}
return ret.join('\n');
}
function tidy_js(code) {
return tidy_js_tab(tidy_blank(code));
}
function tidy_c(code) {
return tidy_c_tab(tidy_blank(code));
}
function tidy_python(code) {
return tidy_blank(code);
}
module.exports = {
tidy_js,
tidy_c,
tidy_python,
}