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
33 changes: 33 additions & 0 deletions packages/shared/src/portfolio-import/parsers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ describe('splitCsvLine', () => {
expect(splitCsvLine('"A, Inc.",AAPL.US')).toEqual(['A, Inc.', 'AAPL.US'])
expect(splitCsvLine('"said ""hi""",x')).toEqual(['said "hi"', 'x'])
})

it('splits semicolon-delimited rows and keeps quoted semicolons in one cell', () => {
expect(splitCsvLine('AAPL.US;"Apple; Inc.";10;100.5')).toEqual(['AAPL.US', 'Apple; Inc.', '10', '100.5'])
expect(splitCsvLine('AAPL.US;"Apple; ""Inc.""";10;100.5')).toEqual(['AAPL.US', 'Apple; "Inc."', '10', '100.5'])
})
})

describe('parseCsv', () => {
Expand Down Expand Up @@ -146,6 +151,34 @@ describe('parseCsv', () => {
expect(rows[2]).toMatchObject({ quantity: 500, costPrice: 320, confidence: 1, issues: [] })
})

it('parses semicolon-delimited headers and rows like the comma equivalent', () => {
const semicolon = parseCsv('Symbol;Quantity;Cost\nAAPL.US;10;100.5\n0700.HK;500;320')
const comma = parseCsv('Symbol,Quantity,Cost\nAAPL.US,10,100.5\n0700.HK,500,320')
expect(semicolon).toEqual(comma)
expect(semicolon[0]).toMatchObject({
symbol: 'AAPL.US',
quantity: 10,
costPrice: 100.5,
confidence: 1,
issues: [],
})
expect(semicolon[1]).toMatchObject({ symbol: '0700.HK', quantity: 500, costPrice: 320, issues: [] })
})

it('does not split semicolons that sit inside quoted fields', () => {
const [row] = parseCsv('Symbol;Name;Quantity;Cost\nAAPL.US;"Apple; Inc.";10;100.5')
expect(row).toMatchObject({
symbol: 'AAPL.US',
name: 'Apple; Inc.',
quantity: 10,
costPrice: 100.5,
confidence: 1,
issues: [],
})
const [comma] = parseCsv('Symbol,Name,Quantity,Cost\nAAPL.US,"Apple; Inc.",10,100.5')
expect(comma).toMatchObject({ name: 'Apple; Inc.', quantity: 10, costPrice: 100.5, issues: [] })
})

it('detects English headers and maps columns', () => {
const rows = parseCsv('Symbol,Name,Quantity,Cost\nAAPL.US,Apple,100,180.5\n0700.HK,Tencent,500,320')
expect(rows).toHaveLength(2)
Expand Down
11 changes: 7 additions & 4 deletions packages/shared/src/portfolio-import/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,21 @@ export function splitCsvRows(text: string): string[] {
return rows
}

/** Separators recognized outside quotes. The first one in the row wins. */
type CsvDelimiter = ',' | '\t' | ';'

/** Use the first unquoted separator; quoted punctuation belongs to a cell. */
function csvDelimiter(line: string): ',' | '\t' {
function csvDelimiter(line: string): CsvDelimiter {
let inQuotes = false
for (const ch of line) {
if (ch === '"') inQuotes = !inQuotes
else if (!inQuotes && (ch === ',' || ch === '\t')) return ch
else if (!inQuotes && (ch === ',' || ch === '\t' || ch === ';')) return ch
}
return ','
}

/** Split one CSV row using one delimiter, honoring quoted cells. */
export function splitCsvLine(line: string, delimiter: ',' | '\t' = csvDelimiter(line)): string[] {
export function splitCsvLine(line: string, delimiter: CsvDelimiter = csvDelimiter(line)): string[] {
const cells: string[] = []
let current = ''
let inQuotes = false
Expand Down Expand Up @@ -191,7 +194,7 @@ export interface CsvColumnMap {
export function resolveCsvColumns(
firstRow: string,
headerMapping?: CsvHeaderMapping,
delimiter: ',' | '\t' = csvDelimiter(firstRow)
delimiter: CsvDelimiter = csvDelimiter(firstRow)
): CsvColumnMap {
const cells = splitCsvLine(firstRow, delimiter).map(normalizeHeaderCell)
const byIndex = new Map<number, HeaderField>()
Expand Down