From 5a27c93b861c50cbace21a1d5a7761064e3a381c Mon Sep 17 00:00:00 2001 From: Anil Vishnoi Date: Thu, 5 Sep 2024 15:44:42 -0700 Subject: [PATCH] Remove trailing new line from the downloaded yaml file Signed-off-by: Anil Vishnoi --- src/utils/yamlConfig.ts | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/utils/yamlConfig.ts b/src/utils/yamlConfig.ts index 193ff21d..8761cb06 100644 --- a/src/utils/yamlConfig.ts +++ b/src/utils/yamlConfig.ts @@ -58,20 +58,22 @@ function filterEmptyContext(data: unknown): unknown { function trimTrailingSpaces(yamlString: string): string { const hasTrailingNewline = yamlString.endsWith('\n'); const lines = yamlString.split('\n'); - const trimmedLines = lines.map((line, index) => { - if (index === lines.length - 1 && line === '' && hasTrailingNewline) { + const trimmedLines = lines + .map((line, index) => { + if (index === lines.length - 1 && line === '' && hasTrailingNewline) { + return undefined; + } + // Preserve empty lines + if (line.trim() === '') return ''; + // Trim trailing spaces, preserving indentation + const match = line.match(/^(\s*)(.*)$/); + if (match) { + const [, indent, content] = match; + return indent + content.trimEnd(); + } return line; - } - // Preserve empty lines - if (line.trim() === '') return ''; - // Trim trailing spaces, preserving indentation - const match = line.match(/^(\s*)(.*)$/); - if (match) { - const [, indent, content] = match; - return indent + content.trimEnd(); - } - return line; - }); + }) + .filter((line) => line !== undefined); return trimmedLines.join('\n'); }