From 3b1cbc357451932afebe2847f78f44b6a9ceccc0 Mon Sep 17 00:00:00 2001 From: wannT-1 <200890097+wannT-1@users.noreply.github.com> Date: Tue, 14 Jul 2026 15:20:00 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(portfolio-import):=20=E5=90=8C=E6=97=B6?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=20Price=20=E4=B8=8E=E6=88=90=E6=9C=AC?= =?UTF-8?q?=E5=88=97=E6=97=B6=E6=94=B9=E7=94=A8=E6=9B=B4=E5=85=B7=E4=BD=93?= =?UTF-8?q?=E7=9A=84=E8=A1=A8=E5=A4=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 左侧的 Price/价格列不再抢走同一行里的 Cost、Avg Cost 或成本。只有没有更具体的成本列时,Price 才继续作为成本兜底。 --- .../src/portfolio-import/parsers.test.ts | 18 ++++++++++++++ .../shared/src/portfolio-import/parsers.ts | 24 +++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/packages/shared/src/portfolio-import/parsers.test.ts b/packages/shared/src/portfolio-import/parsers.test.ts index 3e0e175b..76baf324 100644 --- a/packages/shared/src/portfolio-import/parsers.test.ts +++ b/packages/shared/src/portfolio-import/parsers.test.ts @@ -172,6 +172,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..7135d99b 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', '账户', '账号', '组合'], @@ -175,6 +181,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 +224,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) } From 5667396cf1dc2604837ac789b39d098cf09d50d6 Mon Sep 17 00:00:00 2001 From: wannT-1 <200890097+wannT-1@users.noreply.github.com> Date: Wed, 22 Jul 2026 10:40:00 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(portfolio-import):=20=E6=95=B0=E5=80=BC?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E8=AF=86=E5=88=AB=20HK$/US$/=E4=BA=BA?= =?UTF-8?q?=E6=B0=91=E5=B8=81=E7=AD=89=E8=B4=A7=E5=B8=81=E7=AC=A6=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 券商导出把 HK$、US$、¥ 或 USD 粘在数量和成本上时,不再整格判成无效数字。只剩货币符号、没有数字的格子仍然保持无效。 --- .../src/portfolio-import/parsers.test.ts | 23 +++++++++++++++ .../shared/src/portfolio-import/parsers.ts | 28 ++++++++++++++++--- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/packages/shared/src/portfolio-import/parsers.test.ts b/packages/shared/src/portfolio-import/parsers.test.ts index 76baf324..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) diff --git a/packages/shared/src/portfolio-import/parsers.ts b/packages/shared/src/portfolio-import/parsers.ts index 7135d99b..b5b2778b 100644 --- a/packages/shared/src/portfolio-import/parsers.ts +++ b/packages/shared/src/portfolio-import/parsers.ts @@ -89,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') { @@ -100,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 ────────────────────────────────────────────────────────────────────