diff --git a/packages/shared/src/portfolio-import/parsers.test.ts b/packages/shared/src/portfolio-import/parsers.test.ts index 3e0e175b..d2fb4ae3 100644 --- a/packages/shared/src/portfolio-import/parsers.test.ts +++ b/packages/shared/src/portfolio-import/parsers.test.ts @@ -79,6 +79,22 @@ describe('toFiniteNumber', () => { expect(toFiniteNumber(' 1,234.56 ')).toBe(1234.56) }) + it('reads HK$, US$, yuan, and ISO currency glued onto the number', () => { + expect(toFiniteNumber('HK$1,234.50')).toBe(1234.5) + expect(toFiniteNumber('hk$180.5')).toBe(180.5) + expect(toFiniteNumber('US$180.50')).toBe(180.5) + expect(toFiniteNumber('¥180.5')).toBe(180.5) + expect(toFiniteNumber('¥1,600')).toBe(1600) + expect(toFiniteNumber('CN¥180')).toBe(180) + expect(toFiniteNumber('RMB 180.5')).toBe(180.5) + expect(toFiniteNumber('180.50 USD')).toBe(180.5) + expect(toFiniteNumber('320 HKD')).toBe(320) + expect(toFiniteNumber('-HK$12.5')).toBe(-12.5) + expect(toFiniteNumber('-$0.00')).toBe(0) + expect(toFiniteNumber('USD')).toBeUndefined() + expect(toFiniteNumber('HK$')).toBeUndefined() + }) + it('returns undefined for null, empty, and junk', () => { expect(toFiniteNumber(null)).toBeUndefined() expect(toFiniteNumber(undefined)).toBeUndefined() @@ -132,6 +148,13 @@ describe('parseCsv', () => { expect(mapped).toMatchObject({ symbol: 'AAPL.US', quantity: 1000, costPrice: 180.5, issues: [] }) }) + it('parses currency-prefixed cost cells from a brokerage CSV', () => { + const rows = parseCsv('Symbol,Quantity,Cost,Currency\nAAPL.US,"1,000","US$180.50",USD\n0700.HK,500,HK$320,HKD\n600519.SH,10,¥1600,CNY') + expect(rows[0]).toMatchObject({ symbol: 'AAPL.US', quantity: 1000, costPrice: 180.5, currency: 'USD', confidence: 1, issues: [] }) + expect(rows[1]).toMatchObject({ symbol: '0700.HK', quantity: 500, costPrice: 320, currency: 'HKD' }) + expect(rows[2]).toMatchObject({ symbol: '600519.SH', quantity: 10, costPrice: 1600, currency: 'CNY' }) + }) + it('flags formatting-only quantities and costs without inventing zeros', () => { const rows = parseCsv('Symbol,Quantity,Cost\nAAPL.US,100,$\nMSFT.US,",",180.5\n0700.HK,500,320') expect(rows).toHaveLength(3) @@ -172,6 +195,24 @@ describe('parseCsv', () => { expect(rows[0]).toMatchObject({ symbol: 'AAPL.US', quantity: 100, costPrice: 180.5 }) }) + it('keeps Price as a cost fallback only when no specific cost column exists', () => { + const rows = parseCsv('Symbol,Price,Shares\nAAPL.US,180.5,100') + expect(rows[0]).toMatchObject({ symbol: 'AAPL.US', quantity: 100, costPrice: 180.5, confidence: 1, issues: [] }) + }) + + it('uses the specific cost column when an earlier Price column is also present', () => { + const rows = parseCsv('Symbol,Price,Cost,Qty\nAAPL.US,190.25,180.5,100\n0700.HK,330,320,500') + expect(rows[0]).toMatchObject({ symbol: 'AAPL.US', quantity: 100, costPrice: 180.5, confidence: 1, issues: [] }) + expect(rows[1]).toMatchObject({ symbol: '0700.HK', quantity: 500, costPrice: 320 }) + }) + + it('prefers Avg Cost and 成本 over a generic price column', () => { + const english = parseCsv('Ticker,Price,Avg Cost,Shares\nAAPL.US,190.25,180.5,100') + expect(english[0]).toMatchObject({ symbol: 'AAPL.US', quantity: 100, costPrice: 180.5 }) + const chinese = parseCsv('代码,价格,成本,数量\n600519.SH,1700,1600,10') + expect(chinese[0]).toMatchObject({ symbol: '600519.SH', quantity: 10, costPrice: 1600, confidence: 1, issues: [] }) + }) + it('supports an explicit headerMapping override', () => { const rows = parseCsv('Ticker,Avg Cost,Units\nAAPL.US,180.5,100', { symbol: 'Ticker', cost: 'Avg Cost', quantity: 'Units' }) expect(rows[0]).toMatchObject({ symbol: 'AAPL.US', quantity: 100, costPrice: 180.5 }) diff --git a/packages/shared/src/portfolio-import/parsers.ts b/packages/shared/src/portfolio-import/parsers.ts index a2a141fd..b5b2778b 100644 --- a/packages/shared/src/portfolio-import/parsers.ts +++ b/packages/shared/src/portfolio-import/parsers.ts @@ -26,18 +26,24 @@ export const HEADER_ALIASES: Record = { symbol: ['symbol', 'ticker', 'code', 'stock', '代码', '股票代码', '证券代码'], name: ['name', 'company', 'stockname', '名称', '股票名称', '证券名称'], quantity: ['quantity', 'qty', 'shares', '数量', '持股数量', '股数', '持仓数量'], + // Specific cost headers come first. `price` / `价格` are fallbacks only: + // a leftmost "Price" column must not steal the value when "Cost" or + // "Avg Cost" is also present. cost: [ 'cost', 'costprice', 'cost price', - 'price', 'avgcost', 'avg cost', + 'averagecost', + 'average cost', '成本', '成本价', '买入价', '持仓成本', '均价', + '价格', + 'price', ], currency: ['currency', 'ccy', '货币', '币种'], account: ['account', '账户', '账号', '组合'], @@ -83,9 +89,28 @@ export function checkSymbol(raw: string): SymbolCheck { } } +/** + * Drop currency markers brokerage exports glue onto a number. + * `HK$1,234.50`, `US$180`, `¥180.5`, `180.50 USD` all reduce to the digits. + * A leading sign is preserved (`-HK$12` stays negative). + */ +function stripCurrencyAffixes(value: string): string { + let text = value.trim() + const sign = text.startsWith('-') || text.startsWith('+') ? text[0] : '' + if (sign !== '') text = text.slice(1).trim() + text = text.replace(/^(?:HK|US|S|A|C)\s*\$\s*/i, '') + text = text.replace(/^(?:CN|RMB)\s*[¥¥]\s*/i, '') + text = text.replace(/^(?:RMB|CNY|USD|HKD|SGD|EUR|GBP|AUD|CAD)\s*/i, '') + text = text.replace(/^[¥¥€£$]\s*/, '') + text = text.replace(/\s*(?:RMB|CNY|USD|HKD|SGD|EUR|GBP|AUD|CAD)\s*$/i, '') + text = text.replace(/\s*[$¥¥€£]\s*$/, '') + return `${sign}${text.trim()}` +} + /** * Coerce a CSV/paste cell to a finite number. Strings with thousands - * separators or a leading `$` are handled; empty/null/non-numeric → undefined. + * separators or a currency prefix (`$`, `HK$`, `US$`, `¥`, `USD`) are + * handled; empty/null/non-numeric → undefined. */ export function toFiniteNumber(value: unknown): number | undefined { if (typeof value === 'number') { @@ -94,17 +119,18 @@ export function toFiniteNumber(value: unknown): number | undefined { if (typeof value !== 'string') { return undefined } - const trimmed = value.trim() + const trimmed = stripCurrencyAffixes(value) if (trimmed === '') { return undefined } const cleaned = trimmed.replace(/[,$\s]/g, '') // Number('') is zero, but formatting alone does not supply a numeric value. - if (cleaned === '') { + if (cleaned === '' || cleaned === '+' || cleaned === '-') { return undefined } const parsed = Number(cleaned) - return Number.isFinite(parsed) ? parsed : undefined + if (!Number.isFinite(parsed)) return undefined + return parsed === 0 ? 0 : parsed } // ── CSV ──────────────────────────────────────────────────────────────────── @@ -175,6 +201,20 @@ function normalizeHeaderCell(cell: string): string { return cell.trim().toLowerCase().replace(/[\s_\-]/g, '') } +/** + * Pick a column by alias specificity, not by leftmost match. + * The first alias that appears anywhere in the row wins, so a generic + * fallback such as `price` does not override an earlier specific header + * that happens to sit further right. + */ +function columnForAliases(cells: readonly string[], aliases: readonly string[]): number { + for (const alias of aliases) { + const index = cells.indexOf(normalizeHeaderCell(alias)) + if (index >= 0) return index + } + return -1 +} + /** Field → resolved column index for one CSV file. */ export interface CsvColumnMap { isHeader: boolean @@ -204,7 +244,7 @@ export function resolveCsvColumns( found = cells.indexOf(normalizeHeaderCell(explicit)) } if (found === -1) { - found = cells.findIndex((cell) => HEADER_ALIASES[field].some((alias) => normalizeHeaderCell(alias) === cell)) + found = columnForAliases(cells, HEADER_ALIASES[field]) } if (found >= 0) byIndex.set(found, field) }