diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts index 87c19dfd7..4b4946525 100644 --- a/packages/shared/src/index.ts +++ b/packages/shared/src/index.ts @@ -3,3 +3,4 @@ export * from './warn' export * from './error' export * from './emittable' export * from './emitter' +export * from './messages' diff --git a/packages/shared/src/messages.ts b/packages/shared/src/messages.ts new file mode 100644 index 000000000..69172dd93 --- /dev/null +++ b/packages/shared/src/messages.ts @@ -0,0 +1,24 @@ +import { hasOwn, isArray, isObject } from './utils' + +const isNotObjectOrIsArray = (val: unknown) => !isObject(val) || isArray(val) +// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types +export function deepCopy(src: any, des: any): void { + // src and des should both be objects, and none of them can be a array + if (isNotObjectOrIsArray(src) || isNotObjectOrIsArray(des)) { + throw new Error('Invalid value') + } + + for (const key in src) { + if (hasOwn(src, key)) { + if (isNotObjectOrIsArray(src[key]) || isNotObjectOrIsArray(des[key])) { + // replace with src[key] when: + // src[key] or des[key] is not an object, or + // src[key] or des[key] is an array + des[key] = src[key] + } else { + // src[key] and des[key] are both objects, merge them + deepCopy(src[key], des[key]) + } + } + } +} diff --git a/packages/vue-i18n-core/src/composer.ts b/packages/vue-i18n-core/src/composer.ts index c730179cf..7c8a3e67c 100644 --- a/packages/vue-i18n-core/src/composer.ts +++ b/packages/vue-i18n-core/src/composer.ts @@ -12,6 +12,7 @@ import { isObject, assign, inBrowser, + deepCopy, hasOwn } from '@intlify/shared' import { @@ -53,7 +54,6 @@ import { __VUE_I18N_BRIDGE__ } from './symbols' import { - deepCopy, getLocaleMessages, getComponentOptions, createTextNode, diff --git a/packages/vue-i18n-core/src/mixins/bridge.ts b/packages/vue-i18n-core/src/mixins/bridge.ts index c49eeaa13..564085435 100644 --- a/packages/vue-i18n-core/src/mixins/bridge.ts +++ b/packages/vue-i18n-core/src/mixins/bridge.ts @@ -1,5 +1,4 @@ -import { isPlainObject, warn } from '@intlify/shared' -import { deepCopy } from '../utils' +import { deepCopy, isPlainObject, warn } from '@intlify/shared' import type { ComponentOptions } from 'vue' import type { Locale } from '@intlify/core-base' diff --git a/packages/vue-i18n-core/src/utils.ts b/packages/vue-i18n-core/src/utils.ts index 6af257bf6..a7291268c 100644 --- a/packages/vue-i18n-core/src/utils.ts +++ b/packages/vue-i18n-core/src/utils.ts @@ -5,10 +5,10 @@ import { hasOwn, isPlainObject, isString, + deepCopy, warn } from '@intlify/shared' import { Text, createVNode } from 'vue' -import { I18nErrorCodes, createI18nError } from './errors' import { I18nWarnCodes, getWarnMessage } from './warnings' import type { Locale, MessageResolver } from '@intlify/core-base' @@ -148,29 +148,6 @@ export function getLocaleMessages( return ret as { [K in keyof Messages]: Messages[K] } } -const isNotObjectOrIsArray = (val: unknown) => !isObject(val) || isArray(val) -// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types -export function deepCopy(src: any, des: any): void { - // src and des should both be objects, and non of then can be a array - if (isNotObjectOrIsArray(src) || isNotObjectOrIsArray(des)) { - throw createI18nError(I18nErrorCodes.INVALID_VALUE) - } - - for (const key in src) { - if (hasOwn(src, key)) { - if (isNotObjectOrIsArray(src[key]) || isNotObjectOrIsArray(des[key])) { - // replace with src[key] when: - // src[key] or des[key] is not a object, or - // src[key] or des[key] is a array - des[key] = src[key] - } else { - // src[key] and des[key] are both object, merge them - deepCopy(src[key], des[key]) - } - } - } -} - // eslint-disable-next-line @typescript-eslint/no-explicit-any export function getComponentOptions(instance: ComponentInternalInstance): any { return !__BRIDGE__ ? instance.type : instance.proxy!.$options