-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathsql-formatter.ts
More file actions
41 lines (34 loc) · 1005 Bytes
/
sql-formatter.ts
File metadata and controls
41 lines (34 loc) · 1005 Bytes
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
export class SqlFormatter {
private newlineChar: string;
private tabChar: string;
private prettyMode: boolean;
constructor(newlineChar: string = '\n', tabChar: string = ' ', prettyMode: boolean = true) {
this.newlineChar = newlineChar;
this.tabChar = tabChar;
this.prettyMode = prettyMode;
}
format(parts: string[], separator: string = ' '): string {
return parts.filter(part => part !== null && part !== undefined && part !== '').join(separator);
}
indent(text: string, count: number = 1): string {
if (!this.prettyMode) {
return text;
}
const indentation = this.tabChar.repeat(count);
return text.split(this.newlineChar).map(line =>
line.trim() ? indentation + line : line
).join(this.newlineChar);
}
parens(content: string): string {
return `(${content})`;
}
newline(): string {
return this.newlineChar;
}
tab(): string {
return this.tabChar;
}
isPretty(): boolean {
return this.prettyMode;
}
}