YAML converter indentation fix

This commit is contained in:
mProjectsCode 2022-05-31 23:17:37 +02:00
parent 0298270b03
commit 8b4a50647d

View file

@ -3,13 +3,13 @@ export class YAMLConverter {
let output = ''; let output = '';
for (const [key, value] of Object.entries(obj)) { for (const [key, value] of Object.entries(obj)) {
output += `${key}: ${YAMLConverter.toYamlString(value)}\n`; output += `${key}: ${YAMLConverter.toYamlString(value, 0)}\n`;
} }
return output; return output;
} }
private static toYamlString(value: any): string { private static toYamlString(value: any, indentation: number): string {
if (typeof value === 'boolean') { if (typeof value === 'boolean') {
return value ? 'true' : 'false'; return value ? 'true' : 'false';
} else if (typeof value === 'number') { } else if (typeof value === 'number') {
@ -21,15 +21,19 @@ export class YAMLConverter {
if (Array.isArray(value)) { if (Array.isArray(value)) {
for (const valueElement of value) { for (const valueElement of value) {
output += `\n - ${YAMLConverter.toYamlString(valueElement)}`; output += `\n${YAMLConverter.calculateSpacing(indentation)} - ${YAMLConverter.toYamlString(valueElement, indentation + 1)}`;
} }
} else { } else {
for (const [objKey, objValue] of Object.entries(value)) { for (const [objKey, objValue] of Object.entries(value)) {
output += `\n ${objKey}: ${YAMLConverter.toYamlString(objValue)}`; output += `\n${YAMLConverter.calculateSpacing(indentation)} ${objKey}: ${YAMLConverter.toYamlString(objValue, indentation + 1)}`;
} }
} }
return output; return output;
} }
} }
private static calculateSpacing(indentation: number): string {
return ' '.repeat(indentation * 4);
}
} }