From 42225171e57ef61363c5ba01fb5d59104234eb13 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Sun, 12 Mar 2023 20:03:38 +0100 Subject: [PATCH] Fix undefined structureClone (#466) --- components/use-locales-map.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/components/use-locales-map.js b/components/use-locales-map.js index f658f551..cfc01ef5 100644 --- a/components/use-locales-map.js +++ b/components/use-locales-map.js @@ -39,7 +39,8 @@ export default function useLocalesMap(localesMap) { return localesMap[locale] || localesMap[defaultLocale]; } - return mergeDeep(localesMap[defaultLocale], localesMap[locale]); + const target = JSON.parse(JSON.stringify(localesMap[defaultLocale])); + return mergeDeep(target, localesMap[locale]); } /** @@ -47,7 +48,7 @@ export default function useLocalesMap(localesMap) { * @param {any} item * @returns {boolean} */ -export function isObject(item) { +function isObject(item) { return item && typeof item === "object" && !Array.isArray(item); } @@ -58,21 +59,20 @@ export function isObject(item) { * @param {Record} sources * @returns {Record} */ -export function mergeDeep(target, ...sources) { - const targetClone = structuredClone(target) - if (!sources.length) return targetClone; +function mergeDeep(target, ...sources) { + if (!sources.length) return target; const source = sources.shift(); - if (isObject(targetClone) && isObject(source)) { + if (isObject(target) && isObject(source)) { for (const key in source) { if (isObject(source[key])) { - if (!targetClone[key]) Object.assign(targetClone, { [key]: {} }); - mergeDeep(targetClone[key], source[key]); + if (!target[key]) Object.assign(target, { [key]: {} }); + mergeDeep(target[key], source[key]); } else { - Object.assign(targetClone, { [key]: source[key] }); + Object.assign(target, { [key]: source[key] }); } } } - return mergeDeep(targetClone, ...sources); + return mergeDeep(target, ...sources); }