Skip to content

Commit 1d7843a

Browse files
committed
refactor: move handleFlatJSON to vue-i18n-core/composer from core-base/resolver
1 parent 9e0923c commit 1d7843a

File tree

4 files changed

+77
-77
lines changed

4 files changed

+77
-77
lines changed

packages/core-base/src/resolver.ts

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isObject, hasOwn } from '@intlify/shared'
1+
import { isObject } from '@intlify/shared'
22

33
/** @VueI18nGeneral */
44
export type Path = string
@@ -354,50 +354,3 @@ export function resolveValue(obj: unknown, path: Path): PathValue {
354354

355355
return last
356356
}
357-
358-
/**
359-
* Transform flat json in obj to normal json in obj
360-
*/
361-
export function handleFlatJson(obj: unknown): unknown {
362-
// check obj
363-
if (!isObject(obj)) {
364-
return obj
365-
}
366-
367-
for (const key in obj as object) {
368-
// check key
369-
if (!hasOwn(obj, key)) {
370-
continue
371-
}
372-
373-
// handle for normal json
374-
if (!key.includes(PathCharTypes.DOT)) {
375-
// recursive process value if value is also a object
376-
if (isObject(obj[key])) {
377-
handleFlatJson(obj[key])
378-
}
379-
}
380-
// handle for flat json, transform to normal json
381-
else {
382-
// go to the last object
383-
const subKeys = key.split(PathCharTypes.DOT)
384-
const lastIndex = subKeys.length - 1
385-
let currentObj = obj
386-
for (let i = 0; i < lastIndex; i++) {
387-
if (!(subKeys[i] in currentObj)) {
388-
currentObj[subKeys[i]] = {}
389-
}
390-
currentObj = currentObj[subKeys[i]]
391-
}
392-
// update last object value, delete old property
393-
currentObj[subKeys[lastIndex]] = obj[key]
394-
delete obj[key]
395-
// recursive process value if value is also a object
396-
if (isObject(currentObj[subKeys[lastIndex]])) {
397-
handleFlatJson(currentObj[subKeys[lastIndex]])
398-
}
399-
}
400-
}
401-
402-
return obj
403-
}

packages/core-base/test/resolver.test.ts

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parse, resolveValue, handleFlatJson } from '../src/resolver'
1+
import { parse, resolveValue } from '../src/resolver'
22

33
test('parse', () => {
44
expect(parse('a')).toEqual(['a'])
@@ -121,29 +121,3 @@ test('resolveValue', () => {
121121
// blanket middle
122122
expect(resolveValue({}, 'a.b.c[]d')).toEqual(null)
123123
})
124-
125-
test('handleFlatJson', () => {
126-
const obj = {
127-
a: { a1: 'a1.value' },
128-
'a.a2': 'a.a2.value',
129-
'b.x': {
130-
'b1.x': 'b1.x.value',
131-
'b2.x': ['b2.x.value0', 'b2.x.value1'],
132-
'b3.x': { 'b3.x': 'b3.x.value' }
133-
}
134-
}
135-
const expectObj = {
136-
a: {
137-
a1: 'a1.value',
138-
a2: 'a.a2.value'
139-
},
140-
b: {
141-
x: {
142-
b1: { x: 'b1.x.value' },
143-
b2: { x: ['b2.x.value0', 'b2.x.value1'] },
144-
b3: { x: { b3: { x: 'b3.x.value' } } }
145-
}
146-
}
147-
}
148-
expect(handleFlatJson(obj)).toEqual(expectObj)
149-
})

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

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import {
3636
clearNumberFormat,
3737
fallbackWithLocaleChain,
3838
NOT_REOSLVED,
39-
handleFlatJson,
4039
MessageFunction,
4140
setAdditionalMeta,
4241
DEFAULT_LOCALE
@@ -1716,6 +1715,53 @@ type GetLocaleMessagesOptions<Messages = {}> = {
17161715
flatJson?: boolean
17171716
}
17181717

1718+
/**
1719+
* Transform flat json in obj to normal json in obj
1720+
*/
1721+
export function handleFlatJson(obj: unknown): unknown {
1722+
// check obj
1723+
if (!isObject(obj)) {
1724+
return obj
1725+
}
1726+
1727+
for (const key in obj as object) {
1728+
// check key
1729+
if (!hasOwn(obj, key)) {
1730+
continue
1731+
}
1732+
1733+
// handle for normal json
1734+
if (!key.includes('.')) {
1735+
// recursive process value if value is also a object
1736+
if (isObject(obj[key])) {
1737+
handleFlatJson(obj[key])
1738+
}
1739+
}
1740+
// handle for flat json, transform to normal json
1741+
else {
1742+
// go to the last object
1743+
const subKeys = key.split('.')
1744+
const lastIndex = subKeys.length - 1
1745+
let currentObj = obj
1746+
for (let i = 0; i < lastIndex; i++) {
1747+
if (!(subKeys[i] in currentObj)) {
1748+
currentObj[subKeys[i]] = {}
1749+
}
1750+
currentObj = currentObj[subKeys[i]]
1751+
}
1752+
// update last object value, delete old property
1753+
currentObj[subKeys[lastIndex]] = obj[key]
1754+
delete obj[key]
1755+
// recursive process value if value is also a object
1756+
if (isObject(currentObj[subKeys[lastIndex]])) {
1757+
handleFlatJson(currentObj[subKeys[lastIndex]])
1758+
}
1759+
}
1760+
}
1761+
1762+
return obj
1763+
}
1764+
17191765
export function getLocaleMessages<Messages = {}>(
17201766
locale: Locale,
17211767
options: GetLocaleMessagesOptions<Messages>

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
createComposer,
1313
MissingHandler,
1414
ComposerOptions,
15-
VueMessageType
15+
VueMessageType,
16+
handleFlatJson
1617
} from '../src/composer'
1718
import {
1819
TransrateVNodeSymbol,
@@ -1699,4 +1700,30 @@ describe('root', () => {
16991700
})
17001701
})
17011702

1703+
test('handleFlatJson', () => {
1704+
const obj = {
1705+
a: { a1: 'a1.value' },
1706+
'a.a2': 'a.a2.value',
1707+
'b.x': {
1708+
'b1.x': 'b1.x.value',
1709+
'b2.x': ['b2.x.value0', 'b2.x.value1'],
1710+
'b3.x': { 'b3.x': 'b3.x.value' }
1711+
}
1712+
}
1713+
const expectObj = {
1714+
a: {
1715+
a1: 'a1.value',
1716+
a2: 'a.a2.value'
1717+
},
1718+
b: {
1719+
x: {
1720+
b1: { x: 'b1.x.value' },
1721+
b2: { x: ['b2.x.value0', 'b2.x.value1'] },
1722+
b3: { x: { b3: { x: 'b3.x.value' } } }
1723+
}
1724+
}
1725+
}
1726+
expect(handleFlatJson(obj)).toEqual(expectObj)
1727+
})
1728+
17021729
/* eslint-enable @typescript-eslint/no-empty-function, @typescript-eslint/no-explicit-any */

0 commit comments

Comments
 (0)