Skip to content

Commit

Permalink
refactor: move message merging utilities to shared module (#1635)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede authored Nov 16, 2023
1 parent be32a37 commit 9d337bf
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 27 deletions.
1 change: 1 addition & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './warn'
export * from './error'
export * from './emittable'
export * from './emitter'
export * from './messages'
24 changes: 24 additions & 0 deletions packages/shared/src/messages.ts
Original file line number Diff line number Diff line change
@@ -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])
}
}
}
}
2 changes: 1 addition & 1 deletion packages/vue-i18n-core/src/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
isObject,
assign,
inBrowser,
deepCopy,
hasOwn
} from '@intlify/shared'
import {
Expand Down Expand Up @@ -53,7 +54,6 @@ import {
__VUE_I18N_BRIDGE__
} from './symbols'
import {
deepCopy,
getLocaleMessages,
getComponentOptions,
createTextNode,
Expand Down
3 changes: 1 addition & 2 deletions packages/vue-i18n-core/src/mixins/bridge.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
25 changes: 1 addition & 24 deletions packages/vue-i18n-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -148,29 +148,6 @@ export function getLocaleMessages<Messages = {}>(
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
Expand Down

0 comments on commit 9d337bf

Please sign in to comment.