Skip to content

Commit

Permalink
Fix undefined structureClone (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi authored Mar 12, 2023
1 parent 1032d5e commit 4222517
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions components/use-locales-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ 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]);
}

/**
* Simple object check.
* @param {any} item
* @returns {boolean}
*/
export function isObject(item) {
function isObject(item) {
return item && typeof item === "object" && !Array.isArray(item);
}

Expand All @@ -58,21 +59,20 @@ export function isObject(item) {
* @param {Record<string, T>} sources
* @returns {Record<string, T>}
*/
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);
}

1 comment on commit 4222517

@vercel
Copy link

@vercel vercel bot commented on 4222517 Mar 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

swr-site – ./

swr.vercel.app
swr-site.vercel.sh
swr-site-git-main.vercel.sh

Please sign in to comment.