Skip to content

Commit dc9798d

Browse files
authored
fix(tables): keep leading-zero codes as text when inferring CSV column types (#9754)
1 parent 2441e8d commit dc9798d

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎apps/sim/lib/table/import.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ describe('import', () => {
6363
)
6464
expect(columns.map((c) => c.name)).toEqual(['a_b', 'a_b_2', 'a_b_3'])
6565
})
66+
67+
it('keeps codes with a leading zero as text so the zero survives', () => {
68+
const { columns } = inferSchemaFromCsv(
69+
['zip', 'ratio'],
70+
[
71+
{ zip: '02134', ratio: '0.5' },
72+
{ zip: '10001', ratio: '0' },
73+
]
74+
)
75+
expect(columns.map((c) => c.type)).toEqual(['string', 'number'])
76+
})
6677
})
6778

6879
describe('coerceValue', () => {

‎apps/sim/lib/table/import.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,11 @@ export function inferColumnType(values: unknown[]): InferredCsvColumnType {
388388
const nonEmpty = values.filter((v) => v !== null && v !== undefined && v !== '')
389389
if (nonEmpty.length === 0) return 'string'
390390

391+
// A leading zero before another digit marks a code (ZIP, phone, SKU), not a
392+
// quantity: storing it as a number would drop the zero for good.
391393
const allNumber = nonEmpty.every((v) => {
392-
const n = Number(v)
393-
return !Number.isNaN(n) && String(v).trim() !== ''
394+
const s = String(v).trim()
395+
return s !== '' && !Number.isNaN(Number(s)) && !/^[+-]?0\d/.test(s)
394396
})
395397
if (allNumber) return 'number'
396398

0 commit comments

Comments
 (0)