forked from ferdikoomen/openapi-typescript-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatCode.ts
More file actions
23 lines (22 loc) · 680 Bytes
/
Copy pathformatCode.ts
File metadata and controls
23 lines (22 loc) · 680 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { EOL } from 'os';
export const formatCode = (s: string): string => {
let indent: number = 0;
let lines = s.split(/[\r\n]+/);
lines = lines.map(line => {
line = line.trim().replace(/^\*/g, ' *');
let i = indent;
if (line.endsWith('(') || line.endsWith('{') || line.endsWith('[')) {
indent++;
}
if ((line.startsWith(')') || line.startsWith('}') || line.startsWith(']')) && i) {
indent--;
i--;
}
const result = `${'\t'.repeat(i)}${line}`;
if (result.trim() === '') {
return '';
}
return result;
});
return lines.join(EOL);
};