Skip to content

Commit 9d337bf

Browse files
authored
refactor: move message merging utilities to shared module (#1635)
1 parent be32a37 commit 9d337bf

File tree

5 files changed

+28
-27
lines changed

5 files changed

+28
-27
lines changed

packages/shared/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './warn'
33
export * from './error'
44
export * from './emittable'
55
export * from './emitter'
6+
export * from './messages'

packages/shared/src/messages.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { hasOwn, isArray, isObject } from './utils'
2+
3+
const isNotObjectOrIsArray = (val: unknown) => !isObject(val) || isArray(val)
4+
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
5+
export function deepCopy(src: any, des: any): void {
6+
// src and des should both be objects, and none of them can be a array
7+
if (isNotObjectOrIsArray(src) || isNotObjectOrIsArray(des)) {
8+
throw new Error('Invalid value')
9+
}
10+
11+
for (const key in src) {
12+
if (hasOwn(src, key)) {
13+
if (isNotObjectOrIsArray(src[key]) || isNotObjectOrIsArray(des[key])) {
14+
// replace with src[key] when:
15+
// src[key] or des[key] is not an object, or
16+
// src[key] or des[key] is an array
17+
des[key] = src[key]
18+
} else {
19+
// src[key] and des[key] are both objects, merge them
20+
deepCopy(src[key], des[key])
21+
}
22+
}
23+
}
24+
}

packages/vue-i18n-core/src/composer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
isObject,
1313
assign,
1414
inBrowser,
15+
deepCopy,
1516
hasOwn
1617
} from '@intlify/shared'
1718
import {
@@ -53,7 +54,6 @@ import {
5354
__VUE_I18N_BRIDGE__
5455
} from './symbols'
5556
import {
56-
deepCopy,
5757
getLocaleMessages,
5858
getComponentOptions,
5959
createTextNode,

packages/vue-i18n-core/src/mixins/bridge.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { isPlainObject, warn } from '@intlify/shared'
2-
import { deepCopy } from '../utils'
1+
import { deepCopy, isPlainObject, warn } from '@intlify/shared'
32

43
import type { ComponentOptions } from 'vue'
54
import type { Locale } from '@intlify/core-base'

packages/vue-i18n-core/src/utils.ts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import {
55
hasOwn,
66
isPlainObject,
77
isString,
8+
deepCopy,
89
warn
910
} from '@intlify/shared'
1011
import { Text, createVNode } from 'vue'
11-
import { I18nErrorCodes, createI18nError } from './errors'
1212
import { I18nWarnCodes, getWarnMessage } from './warnings'
1313

1414
import type { Locale, MessageResolver } from '@intlify/core-base'
@@ -148,29 +148,6 @@ export function getLocaleMessages<Messages = {}>(
148148
return ret as { [K in keyof Messages]: Messages[K] }
149149
}
150150

151-
const isNotObjectOrIsArray = (val: unknown) => !isObject(val) || isArray(val)
152-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types
153-
export function deepCopy(src: any, des: any): void {
154-
// src and des should both be objects, and non of then can be a array
155-
if (isNotObjectOrIsArray(src) || isNotObjectOrIsArray(des)) {
156-
throw createI18nError(I18nErrorCodes.INVALID_VALUE)
157-
}
158-
159-
for (const key in src) {
160-
if (hasOwn(src, key)) {
161-
if (isNotObjectOrIsArray(src[key]) || isNotObjectOrIsArray(des[key])) {
162-
// replace with src[key] when:
163-
// src[key] or des[key] is not a object, or
164-
// src[key] or des[key] is a array
165-
des[key] = src[key]
166-
} else {
167-
// src[key] and des[key] are both object, merge them
168-
deepCopy(src[key], des[key])
169-
}
170-
}
171-
}
172-
}
173-
174151
// eslint-disable-next-line @typescript-eslint/no-explicit-any
175152
export function getComponentOptions(instance: ComponentInternalInstance): any {
176153
return !__BRIDGE__ ? instance.type : instance.proxy!.$options

0 commit comments

Comments
 (0)