From 9393e8d4d961f890c7c0612683ba7c8f37e408ad Mon Sep 17 00:00:00 2001 From: Brent Salisbury Date: Thu, 11 Jul 2024 23:01:49 -0400 Subject: [PATCH 1/2] Remove trailing spaces from yaml - Custom yaml for multiline white spaces. - Single line will single quote by default. Signed-off-by: Brent Salisbury --- src/utils/yamlConfig.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/utils/yamlConfig.ts b/src/utils/yamlConfig.ts index baaf9cb..31bb8b8 100644 --- a/src/utils/yamlConfig.ts +++ b/src/utils/yamlConfig.ts @@ -37,8 +37,31 @@ function removeNullValues(obj: unknown): unknown { return obj === null ? '' : obj; } +function trimTrailingSpaces(yamlString: string): string { + // Split the string, but keep the final newline if it exists + const hasTrailingNewline = yamlString.endsWith('\n'); + const lines = yamlString.split('\n'); + const trimmedLines = lines.map((line, index) => { + if (index === lines.length - 1 && line === '' && hasTrailingNewline) { + 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; + }); + + return trimmedLines.join('\n'); +} + export function dumpYaml(data: unknown, options: Partial = {}): string { const mergedOptions: YamlDumpOptions = { ...defaultYamlDumpOptions, ...options }; const processedData = removeNullValues(data); - return yaml.dump(processedData, mergedOptions); + const yamlString = yaml.dump(processedData, mergedOptions); + return trimTrailingSpaces(yamlString); } From 0c3fdc1b13c8ae422c92625e43201b1ede101703 Mon Sep 17 00:00:00 2001 From: Brent Salisbury Date: Thu, 11 Jul 2024 23:52:20 -0400 Subject: [PATCH 2/2] Only add a context if the value is null Signed-off-by: Brent Salisbury --- src/utils/yamlConfig.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/utils/yamlConfig.ts b/src/utils/yamlConfig.ts index 31bb8b8..193ff21 100644 --- a/src/utils/yamlConfig.ts +++ b/src/utils/yamlConfig.ts @@ -37,8 +37,25 @@ function removeNullValues(obj: unknown): unknown { return obj === null ? '' : obj; } +// If the Context field is empty, omit the key from the output +function filterEmptyContext(data: unknown): unknown { + if (Array.isArray(data)) { + return data.map((item) => filterEmptyContext(item)); + } else if (data !== null && typeof data === 'object') { + const filteredEntries = Object.entries(data) + .map(([key, value]) => { + if (key === 'context' && value === '') { + return null; + } + return [key, filterEmptyContext(value)]; + }) + .filter((entry): entry is [string, unknown] => entry !== null); + return Object.fromEntries(filteredEntries); + } + return data; +} + function trimTrailingSpaces(yamlString: string): string { - // Split the string, but keep the final newline if it exists const hasTrailingNewline = yamlString.endsWith('\n'); const lines = yamlString.split('\n'); const trimmedLines = lines.map((line, index) => { @@ -61,7 +78,8 @@ function trimTrailingSpaces(yamlString: string): string { export function dumpYaml(data: unknown, options: Partial = {}): string { const mergedOptions: YamlDumpOptions = { ...defaultYamlDumpOptions, ...options }; - const processedData = removeNullValues(data); + const filteredData = filterEmptyContext(data); + const processedData = removeNullValues(filteredData); const yamlString = yaml.dump(processedData, mergedOptions); return trimTrailingSpaces(yamlString); }