0
0
mirror of https://github.com/renovatebot/renovate.git synced 2024-12-22 13:38:32 +00:00
renovatebot_renovate/lib/util/json-writer/json-writer.ts
RahulGautamSingh c93154bd24
refactor: enums to unions (#18747)
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
2022-11-05 09:12:03 +00:00

32 lines
790 B
TypeScript

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;
}
}