Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions apps/sim/lib/table/import.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ describe('import', () => {
)
expect(columns.map((c) => c.name)).toEqual(['a_b', 'a_b_2', 'a_b_3'])
})

it('keeps codes with a leading zero as text so the zero survives', () => {
const { columns } = inferSchemaFromCsv(
['zip', 'ratio'],
[
{ zip: '02134', ratio: '0.5' },
{ zip: '10001', ratio: '0' },
]
)
expect(columns.map((c) => c.type)).toEqual(['string', 'number'])
})
})

describe('coerceValue', () => {
Expand Down
6 changes: 4 additions & 2 deletions apps/sim/lib/table/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,11 @@ export function inferColumnType(values: unknown[]): InferredCsvColumnType {
const nonEmpty = values.filter((v) => v !== null && v !== undefined && v !== '')
if (nonEmpty.length === 0) return 'string'

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

Expand Down
Loading