Skip to content

Commit 64bde0e

Browse files
shota-kizawakazupon
authored andcommitted
refactor: flat option of diff API (#185)
* fix: change the return value of the diff API to an object * feat: add the normalize option to the diff API. * fix: diffInfo type definition * fix: types of return value of diff API * fix: return value of diff * fix: delete warning of eslint * fix: flat option of diff API * fix: unit test of returnDiff * refactor: delete unused test
1 parent 2cd283e commit 64bde0e

File tree

4 files changed

+74
-6
lines changed

4 files changed

+74
-6
lines changed

src/utils.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -472,9 +472,12 @@ export async function returnDiff (options: DiffOptions): Promise<DiffInfo> {
472472

473473
if (ret) {
474474
if (options.normalize === 'flat') {
475-
const flattenedServiceMessages = flatten(serviceMessages)
476-
const flattenedlocaleMessages = flatten(localeMessages)
477-
const flattenedDiffObj = jsonDiff.diff(flattenedServiceMessages, flattenedlocaleMessages)
475+
const flattenedDiffObj = {} as Record<Locale, Record<string, string>>
476+
for (const locale in serviceMessages) {
477+
flattenedDiffObj[locale] = jsonDiff.diff(
478+
flatten(serviceMessages[locale]), flatten(localeMessages[locale])
479+
)
480+
}
478481
return Promise.resolve(flattenedDiffObj)
479482
}
480483
if (options.normalize === 'hierarchy') {

test/__snapshots__/utils.test.ts.snap

+26
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,32 @@ Object {
7373
}
7474
`;
7575

76+
exports[`returnDiff: no normalize 1`] = `
77+
Object {
78+
"en": Object {
79+
"hello": Object {
80+
"__new": "world",
81+
"__old": "hello!",
82+
},
83+
"nest__deleted": Object {
84+
"world": "world!",
85+
},
86+
},
87+
}
88+
`;
89+
90+
exports[`returnDiff: normalize=flat 1`] = `
91+
Object {
92+
"en": Object {
93+
"hello": Object {
94+
"__new": "world",
95+
"__old": "hello!",
96+
},
97+
"nest.world__deleted": "world!",
98+
},
99+
}
100+
`;
101+
76102
exports[`splitLocaleMessages: basic usage 1`] = `
77103
Object {
78104
"navigation": Object {

test/utils.test.ts

+41-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
reflectSFCDescriptor,
1111
getTranslationStatus,
1212
getExternalLocaleMessages,
13-
splitLocaleMessages
13+
splitLocaleMessages,
14+
returnDiff
1415
} from '../src/utils'
1516

1617
import ignore from 'ignore'
@@ -20,9 +21,10 @@ import ignore from 'ignore'
2021

2122
// l10n service provider module
2223
const mockStatus = jest.fn()
24+
const mockPull = jest.fn()
2325
jest.mock('@scope/l10n-service-provider', () => {
2426
return jest.fn().mockImplementation(() => {
25-
return { status: mockStatus }
27+
return { status: mockStatus, pull: mockPull }
2628
})
2729
})
2830
import L10nServiceProvider from '@scope/l10n-service-provider'
@@ -136,3 +138,40 @@ test('splitLocaleMessages: no namespace', async () => {
136138
expect(sfc).toEqual(messages)
137139
expect(external).toBeUndefined()
138140
})
141+
142+
test('returnDiff: no normalize', async() => {
143+
mockPull.mockImplementation(() => Promise.resolve({
144+
en: {
145+
hello: 'hello!',
146+
nest: {
147+
world: 'world!'
148+
}
149+
}
150+
}))
151+
const diffMsg = await returnDiff({
152+
provider: '@scope/l10n-service-provider',
153+
conf: './test/fixtures/conf/l10n-service-provider-conf.json',
154+
format: 'json',
155+
target: './test/fixtures/locales/en.json'
156+
})
157+
expect(diffMsg).toMatchSnapshot()
158+
})
159+
160+
test('returnDiff: normalize=flat', async() => {
161+
mockPull.mockImplementation(() => Promise.resolve({
162+
en: {
163+
hello: 'hello!',
164+
nest: {
165+
world: 'world!'
166+
}
167+
}
168+
}))
169+
const diffMsg = await returnDiff({
170+
provider: '@scope/l10n-service-provider',
171+
conf: './test/fixtures/conf/l10n-service-provider-conf.json',
172+
format: 'json',
173+
target: './test/fixtures/locales/en.json',
174+
normalize: 'flat'
175+
})
176+
expect(diffMsg).toMatchSnapshot()
177+
})

types/index.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ export type DiffOptions = {
224224
normalize?: string
225225
} & PushableOptions
226226

227-
export type DiffInfo = Record<string, any>
227+
export type DiffInfo = Record<Locale, Record<string, any>>
228228

229229
declare function diff (options: DiffOptions): Promise<DiffInfo>
230230

0 commit comments

Comments
 (0)