Skip to content

Commit faa49b3

Browse files
authored
fix(form-core): preserve leading zeros in numeric string field names (#1620)
1 parent 117b743 commit faa49b3

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

packages/form-core/src/utils.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,14 @@ export function makePathArray(str: string | Array<string | number>) {
171171
.replace(reMultipleDots, '.')
172172
.split('.')
173173
.map((d) => {
174-
if (d.indexOf(intPrefix) === 0) {
175-
return parseInt(d.substring(intPrefix.length), 10)
174+
if (d.startsWith(intPrefix)) {
175+
const numStr = d.substring(intPrefix.length)
176+
const num = parseInt(numStr, 10)
177+
178+
if (String(num) === numStr) {
179+
return num
180+
}
181+
return numStr
176182
}
177183
return d
178184
})

packages/form-core/tests/utils.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ describe('setBy', () => {
133133
],
134134
])
135135
})
136+
137+
it('should correctly set a value on a key with leading zeros', () => {
138+
const initial = { name: 'test' }
139+
const result = setBy(initial, '01234', 'some-value')
140+
141+
expect(result).toHaveProperty('01234')
142+
expect(result['01234']).toBe('some-value')
143+
144+
expect(result).not.toHaveProperty('1234')
145+
})
136146
})
137147

138148
describe('deleteBy', () => {
@@ -223,6 +233,15 @@ describe('makePathArray', () => {
223233
expect(makePathArray('[2][3].a')).toEqual([2, 3, 'a'])
224234
expect(makePathArray('[4][5][6].b[7]')).toEqual([4, 5, 6, 'b', 7])
225235
})
236+
237+
it('should preserve leading zeros on purely numeric strings', () => {
238+
expect(makePathArray('01234')).toEqual(['01234'])
239+
expect(makePathArray('007')).toEqual(['007'])
240+
})
241+
242+
it('should still convert non-leading-zero numbers to number types', () => {
243+
expect(makePathArray('12345')).toEqual([12345])
244+
})
226245
})
227246

228247
describe('determineFormLevelErrorSourceAndValue', () => {

0 commit comments

Comments
 (0)