From 51e160560c0ee2fa37de78402baf302a0e77c2bb Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Tue, 2 Feb 2021 16:50:13 -0800 Subject: [PATCH] Fix handling of 'undefined' in YAML --- lib/common/yaml.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/common/yaml.ts b/lib/common/yaml.ts index 549812c..77a3ce9 100644 --- a/lib/common/yaml.ts +++ b/lib/common/yaml.ts @@ -116,8 +116,12 @@ function stringifyAny( prefix1 = "", prefix2 = "" ): void { - if (obj == null || typeof obj === "number" || typeof obj === "boolean") { - res.push(`${prefix1}${obj}`); + if (obj == null) { + res.push(`${prefix1}null`); + return; + } + if (typeof obj === "number" || typeof obj === "boolean") { + res.push(`${prefix1}${JSON.stringify(obj)}`); return; } if (obj instanceof Date) { @@ -152,7 +156,7 @@ function stringifyAny( return; } - const entries = Object.entries(obj); + const entries = Object.entries(obj).filter((e) => e[1] !== undefined); if (!entries.length) { res.push(prefix1 + "{}"); @@ -192,6 +196,7 @@ function stringifyAny( } export function stringify(obj: unknown): string { + if (obj === undefined) return undefined; const lines: string[] = []; stringifyAny(obj, lines); return lines.join("\n") + "\n";