From 5aee3a15407aa4f92a3d1329b3a29216232d915f Mon Sep 17 00:00:00 2001 From: Loris Sigrist Date: Tue, 3 Oct 2023 12:34:36 +0200 Subject: [PATCH] Handle null case --- src/file-handling/utils.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/file-handling/utils.js b/src/file-handling/utils.js index 7478b94..b67d193 100644 --- a/src/file-handling/utils.js +++ b/src/file-handling/utils.js @@ -14,17 +14,22 @@ export function generateDictionaryFromTree(tree, locale) { const keyVal = new Map(); /** - * @param {unknown} obj + * @param {unknown} thing * @param {string[]} path */ - function flatten(obj, path = []) { - if (typeof obj === "string" || typeof obj === "number") { - keyVal.set(path.join("."), String(obj)); + function flatten(thing, path = []) { + if (thing === null) { + keyVal.set(path.join("."), ""); return; } - if (typeof obj === "object" && obj !== null) { - for (const [key, value] of Object.entries(obj)) { + if (typeof thing === "string" || typeof thing === "number") { + keyVal.set(path.join("."), String(thing)); + return; + } + + if (typeof thing === "object") { + for (const [key, value] of Object.entries(thing)) { flatten(value, [...path, key]); } return;