From dcd5b35e928175d6d68c4ce1a6e6971521f1ec7d Mon Sep 17 00:00:00 2001 From: wannT-1 <200890097+wannT-1@users.noreply.github.com> Date: Fri, 30 Oct 2026 07:08:00 +0000 Subject: [PATCH] =?UTF-8?q?fix(portfolio-import):=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=88=86=E5=8F=B7=E5=88=86=E9=9A=94=20CSV?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/portfolio-import/parsers.test.ts | 33 +++++++++++++++++++ .../shared/src/portfolio-import/parsers.ts | 11 ++++--- 2 files changed, 40 insertions(+), 4 deletions(-) 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()