diff --git a/packages/shared/src/portfolio-import/parsers.test.ts b/packages/shared/src/portfolio-import/parsers.test.ts index 3e0e175b..4c9608c6 100644 --- a/packages/shared/src/portfolio-import/parsers.test.ts +++ b/packages/shared/src/portfolio-import/parsers.test.ts @@ -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', () => { @@ -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) diff --git a/packages/shared/src/portfolio-import/parsers.ts b/packages/shared/src/portfolio-import/parsers.ts index a2a141fd..577f2c32 100644 --- a/packages/shared/src/portfolio-import/parsers.ts +++ b/packages/shared/src/portfolio-import/parsers.ts @@ -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 @@ -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()