blob: fb1437db26abe703bb0f7da074024d4da323897d (
plain)
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
|
import type { CodeFormat } from './code-format';
import type { IndentationType } from './indentation-type';
export class JSONWriter {
private readonly indentationType: IndentationType;
private readonly indentationSize: number;
constructor(codeFormat: CodeFormat = {}) {
this.indentationSize = codeFormat.indentationSize ?? 2;
this.indentationType = codeFormat.indentationType ?? 'space';
}
public write(json: unknown, newLineAtTheEnd = true): string {
let content = JSON.stringify(json, null, this.indentation);
if (newLineAtTheEnd) {
content = content.concat('\n');
}
return content;
}
private get indentation(): string | number {
if (this.indentationType === 'tab') {
return '\t';
}
return this.indentationSize;
}
}
|