Skip to content

Commit

Permalink
refactor: Simplify deepMerge function and improve type checking (#2151)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnsdls authored Jan 11, 2024
1 parent 6dac95b commit 415f2aa
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-pants-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@thirdweb-dev/react-native": patch
---

improve typechecking for deepMerge function
1 change: 0 additions & 1 deletion packages/react-native/src/evm/i18n/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export const getLocale = (
return deepMerge(defaultLocale, locale);
};


export const en = (locale?: DeepPartial<LocaleType>) => getLocale("en", locale);
export const es = (locale?: DeepPartial<LocaleType>) => getLocale("es", locale);
export const ja = (locale?: DeepPartial<LocaleType>) => getLocale("ja", locale);
Expand Down
21 changes: 12 additions & 9 deletions packages/react-native/src/evm/types/deepPartial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ export type DeepPartial<T> = {
[P in keyof T]?: DeepPartial<T[P]>;
};

export function deepMerge<T>(target: T, source: DeepPartial<T>): T {
export function deepMerge<T>(target: T, source: DeepPartial<T> = {}): T {
for (const key in source) {
if (!source.hasOwnProperty(key)) {
continue;
}
if (
source[key] instanceof Object &&
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
key in target &&
target[key] instanceof Object
isObject(target) &&
target.hasOwnProperty(key) &&
isObject(target[key])
) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
deepMerge(target[key], source[key]);
} else {
target[key] = source[key] as any;
target[key] = source[key] as (typeof target)[typeof key];
}
}
return target;
}

function isObject(obj: any): obj is object {
return obj !== null && typeof obj === "object";
}

0 comments on commit 415f2aa

Please sign in to comment.