Skip to content

Commit 99691c5

Browse files
veeceeyTest Userclaude
authored
fix: update updateLocale plugin to merge nested object properties instead of replacing (#3012)
When updating a locale with a partial nested object (e.g., only one key in `formats`), the plugin now merges the new values into the existing object rather than replacing the entire object. This preserves other keys that were not included in the update. Arrays and non-object values continue to be replaced entirely. Fixes #1118 Co-authored-by: Test User <test@example.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9d2b6a1 commit 99691c5

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

src/plugin/updateLocale/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ export default (option, Dayjs, dayjs) => {
55
if (!localeConfig) return
66
const customConfigKeys = customConfig ? Object.keys(customConfig) : []
77
customConfigKeys.forEach((c) => {
8-
localeConfig[c] = customConfig[c]
8+
if (localeConfig[c] && customConfig[c] && typeof localeConfig[c] === 'object' && typeof customConfig[c] === 'object'
9+
&& !Array.isArray(localeConfig[c])) {
10+
localeConfig[c] = {
11+
...localeConfig[c],
12+
...customConfig[c]
13+
}
14+
} else {
15+
localeConfig[c] = customConfig[c]
16+
}
917
})
1018
return localeConfig // eslint-disable-line consistent-return
1119
}

test/plugin/updateLocale.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,35 @@ describe('Update locale', () => {
6969
.toEqual(moment().format(formatString))
7070
})
7171

72+
it('Partial update to nested object (formats)', () => {
73+
dayjs.locale('en')
74+
// First, get the original formats
75+
const originalLocale = dayjs.Ls.en
76+
const originalLT = originalLocale.formats && originalLocale.formats.LT
77+
78+
// Update only L format
79+
dayjs.updateLocale('en', {
80+
formats: {
81+
L: 'DD/MM/YYYY'
82+
}
83+
})
84+
85+
const updatedLocale = dayjs.Ls.en
86+
// The updated key should have the new value
87+
expect(updatedLocale.formats.L).toBe('DD/MM/YYYY')
88+
// Other keys in formats should be preserved
89+
expect(updatedLocale.formats.LT).toBe(originalLT)
90+
})
91+
92+
it('Non-object values should still be replaced entirely', () => {
93+
const newMonths = new Array(12).fill('newMonth')
94+
dayjs.updateLocale('en', {
95+
months: newMonths
96+
})
97+
const updatedLocale = dayjs.Ls.en
98+
expect(updatedLocale.months).toEqual(newMonths)
99+
})
100+
72101
it('Update invalid date string', () => {
73102
const locale = 'en'
74103
const localeSetting = { invalidDate: 'bad date' }

0 commit comments

Comments
 (0)