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
29 changes: 29 additions & 0 deletions packages/shared/src/portfolio-import/parsers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ describe('toFiniteNumber', () => {
expect(toFiniteNumber(' 1,234.56 ')).toBe(1234.56)
})

it('reads accounting parentheses and a trailing minus as negative amounts', () => {
expect(toFiniteNumber('(180.50)')).toBe(-180.5)
expect(toFiniteNumber('($1,234.56)')).toBe(-1234.56)
expect(toFiniteNumber('(180.50)')).toBe(-180.5)
expect(toFiniteNumber('1,234.56-')).toBe(-1234.56)
expect(toFiniteNumber('($0.00)')).toBe(0)
expect(toFiniteNumber('()')).toBeUndefined()
expect(toFiniteNumber('($)')).toBeUndefined()
expect(toFiniteNumber('-45.2')).toBe(-45.2)
})

it('folds fullwidth digits and separators before parsing', () => {
expect(toFiniteNumber('1,234.56')).toBe(1234.56)
expect(toFiniteNumber('$180.50')).toBe(180.5)
expect(toFiniteNumber('(180.50)')).toBe(-180.5)
expect(toFiniteNumber('-45.2')).toBe(-45.2)
expect(toFiniteNumber(',,,')).toBeUndefined()
})

it('returns undefined for null, empty, and junk', () => {
expect(toFiniteNumber(null)).toBeUndefined()
expect(toFiniteNumber(undefined)).toBeUndefined()
Expand Down Expand Up @@ -132,6 +151,16 @@ describe('parseCsv', () => {
expect(mapped).toMatchObject({ symbol: 'AAPL.US', quantity: 1000, costPrice: 180.5, issues: [] })
})

it('parses fullwidth quantity and cost cells', () => {
const [row] = parseCsv('代码,数量,成本\n600519.SH,10,1,600.50')
expect(row).toMatchObject({ symbol: '600519.SH', quantity: 10, costPrice: 1600.5, confidence: 1, issues: [] })
})

it('parses parenthesized cost cells as negative amounts', () => {
const [row] = parseCsv('Symbol,Quantity,Cost\nAAPL.US,(100),"($1,234.50)"')
expect(row).toMatchObject({ symbol: 'AAPL.US', quantity: -100, costPrice: -1234.5, confidence: 1, issues: [] })
})

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)
Expand Down
40 changes: 37 additions & 3 deletions packages/shared/src/portfolio-import/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,40 @@ export function checkSymbol(raw: string): SymbolCheck {
}
}

/** Map fullwidth digits and separators onto the ASCII forms `Number` understands. */
function foldNumericWidth(value: string): string {
return value
.replace(/[0-9]/g, (digit) => String.fromCharCode(digit.charCodeAt(0) - 0xfee0))
.replace(/./g, '.')
.replace(/,/g, ',')
.replace(/[-−]/g, '-')
.replace(/$/g, '$')
}

/**
* Accounting exports write negatives as `(180.50)` or `180.50-`.
* Fullwidth parentheses are accepted. A normal leading minus is left for
* the numeric parse.
*/
function takeAccountingSign(value: string): { text: string; negative: boolean } {
let text = value.trim()
let negative = false
const wrapped = /^(?:\(|()\s*(.*?)\s*(?:\)|))$/.exec(text)
if (wrapped) {
negative = true
text = wrapped[1].trim()
}
if (text.endsWith('-') && !/^[+-]/.test(text)) {
negative = true
text = text.slice(0, -1).trim()
}
return { text, negative }
}

/**
* Coerce a CSV/paste cell to a finite number. Strings with thousands
* separators or a leading `$` are handled; empty/null/non-numeric → undefined.
* Accounting parentheses and a trailing minus are negative amounts.
*/
export function toFiniteNumber(value: unknown): number | undefined {
if (typeof value === 'number') {
Expand All @@ -94,17 +125,20 @@ export function toFiniteNumber(value: unknown): number | undefined {
if (typeof value !== 'string') {
return undefined
}
const trimmed = value.trim()
const accounting = takeAccountingSign(foldNumericWidth(value))
const trimmed = accounting.text.trim()
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
if (parsed === 0) return 0
return accounting.negative ? -Math.abs(parsed) : parsed
}

// ── CSV ────────────────────────────────────────────────────────────────────
Expand Down