Skip to content
Closed
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
14 changes: 14 additions & 0 deletions packages/shared/src/portfolio-import/parsers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ describe('parseCsv', () => {
expect(rows[1]).toMatchObject({ symbol: '0700.HK', currency: 'HKD' })
})

it('strips a leading UTF-8 BOM from English headers', () => {
const plain = 'Symbol,Quantity,Cost\nAAPL.US,10,100'
const rows = parseCsv(`\uFEFF${plain}`)
expect(rows).toEqual(parseCsv(plain))
expect(rows[0]).toMatchObject({ symbol: 'AAPL.US', quantity: 10, costPrice: 100, confidence: 1, issues: [] })
})

it('strips a leading UTF-8 BOM from 中文 headers', () => {
const plain = '代码,数量,成本\nAAPL.US,10,100'
const rows = parseCsv(`\uFEFF${plain}`)
expect(rows).toEqual(parseCsv(plain))
expect(rows[0]).toMatchObject({ symbol: 'AAPL.US', quantity: 10, costPrice: 100, confidence: 1, issues: [] })
})

it('detects alias variants (qty / shares / price / code)', () => {
const rows = parseCsv('Code,Shares,Price\nAAPL.US,100,180.5')
expect(rows[0]).toMatchObject({ symbol: 'AAPL.US', quantity: 100, costPrice: 180.5 })
Expand Down
7 changes: 5 additions & 2 deletions packages/shared/src/portfolio-import/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export type HeaderField = 'symbol' | 'name' | 'quantity' | 'cost' | 'currency' |

/**
* Header aliases for CSV column detection (English + 中文). Header cells are
* matched after lowercasing and stripping spaces/underscores/hyphens.
* matched after stripping a leading UTF-8 BOM (U+FEFF), lowercasing, and
* removing spaces, underscores, and hyphens.
*/
export const HEADER_ALIASES: Record<HeaderField, readonly string[]> = {
symbol: ['symbol', 'ticker', 'code', 'stock', '代码', '股票代码', '证券代码'],
Expand Down Expand Up @@ -172,7 +173,9 @@ export function splitCsvLine(line: string, delimiter: ',' | '\t' = csvDelimiter(
}

function normalizeHeaderCell(cell: string): string {
return cell.trim().toLowerCase().replace(/[\s_\-]/g, '')
// Excel "CSV UTF-8" prefixes the first cell with U+FEFF. Strip that BOM
// before alias matching so the header matches the same file without one.
return cell.replace(/^\uFEFF+/, '').trim().toLowerCase().replace(/[\s_\-]/g, '')
}

/** Field → resolved column index for one CSV file. */
Expand Down