Skip to content
Open
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 @@ -74,6 +74,17 @@ describe('import', () => {
)
expect(columns.map((c) => c.type)).toEqual(['string', 'number'])
})

it('keeps integers past the safe range as text so no digit is rounded away', () => {
const { columns } = inferSchemaFromCsv(
['discord_id', 'quantity'],
[
{ discord_id: '1234567890123456789', quantity: '9007199254740991' },
{ discord_id: '987654321098765432', quantity: '-12' },
]
)
expect(columns.map((c) => c.type)).toEqual(['string', 'number'])
})
})

describe('coerceValue', () => {
Expand Down
11 changes: 9 additions & 2 deletions apps/sim/lib/table/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,17 @@ export function inferColumnType(values: unknown[]): InferredCsvColumnType {
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.
// quantity: storing it as a number would drop the zero for good. So does an
// integer past 2^53 (snowflake ids, long account numbers): a number
// column would round away its last digits.
const allNumber = nonEmpty.every((v) => {
const s = String(v).trim()
return s !== '' && !Number.isNaN(Number(s)) && !/^[+-]?0\d/.test(s)
return (
s !== '' &&
!Number.isNaN(Number(s)) &&
!/^[+-]?0\d/.test(s) &&
!(/^[+-]?\d+$/.test(s) && !Number.isSafeInteger(Number(s)))
)
})
if (allNumber) return 'number'

Expand Down