Skip to content
Merged
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
1 change: 1 addition & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/table-cells/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"zod": "4.3.6"
},
"devDependencies": {
"@sim/testing": "workspace:*",
"@sim/tsconfig": "workspace:*",
"typescript": "^7.0.2",
"vitest": "^5.1.0"
Expand Down
17 changes: 17 additions & 0 deletions packages/table-cells/src/currency.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* filters and sorts cast a currency cell to `numeric`, so anything it lets
* through unparsed would break every query against the column.
*/
import { expectLinearGrowth } from '@sim/testing/helpers/scaling'
import { describe, expect, it } from 'vitest'
import { parseCurrencyInput } from './currency'

Expand Down Expand Up @@ -246,4 +247,20 @@ describe('parseCurrencyInput', () => {
expect(parseCurrencyInput('1,234,567.89')).toBe(1234567.89)
expect(parseCurrencyInput('12,34,567')).toBe(1234567)
})

it.each([
['whitespace where a leading marker would start', (n: number) => `(${' '.repeat(n)}#)`],
['whitespace where a trailing marker would start', (n: number) => `a${'\t'.repeat(n)}!`],
['digits before a stray character', (n: number) => `${'0'.repeat(n)}#`],
['digits and separators before a stray character', (n: number) => `${'0.'.repeat(n)}!`],
])('rejects a long run of %s in linear time', (_shape, make) => {
// Table imports and coercion parse every cell on the server, so a parser
// that backtracks quadratically lets one crafted cell stall the event loop.
expectLinearGrowth((n) => {
const input = make(n)
return () => {
expect(parseCurrencyInput(input)).toBeNull()
}
}, 2000)
})
})
41 changes: 27 additions & 14 deletions packages/table-cells/src/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,20 @@ function isCurrencyMarkerLetters(letters: string): boolean {
* A leading currency marker: up to three letters and/or a symbol, optionally
* behind a sign. The sign is captured so `-$12.50` keeps it, and the letters
* are captured so {@link isCurrencyMarkerLetters} can vet them.
*
* The sign and the whitespace after it form one optional group, so a
* whitespace run splits only one way and a failed match backtracks in linear
* time. `\s` already covers U+00A0 and U+202F.
*/
const CURRENCY_MARKER_PREFIX =
/^[\s\u00a0\u202f]*([+-]?)[\s\u00a0\u202f]*(?:\p{Sc}\p{L}{0,3}|(\p{L}{1,3})(\p{Sc})?)[\s\u00a0\u202f]*/u
const CURRENCY_MARKER_PREFIX = /^\s*(?:([+-])\s*)?(?:\p{Sc}\p{L}{0,3}|(\p{L}{1,3})(\p{Sc})?)\s*/u

/** The same, trailing. */
const CURRENCY_MARKER_SUFFIX =
/[\s\u00a0\u202f]*(?:\p{Sc}\p{L}{0,3}|(\p{L}{1,3})(\p{Sc})?)\.?[\s\u00a0\u202f]*$/u
/**
* The same, trailing, matched against a right-trimmed string. It has no
* leading whitespace run, so each start position costs at most the marker's
* length; the whitespace before the marker survives the strip and is removed
* with every other space afterwards.
*/
const CURRENCY_MARKER_SUFFIX = /(?:\p{Sc}\p{L}{0,3}|(\p{L}{1,3})(\p{Sc})?)\.?$/u

/**
* Whether a matched marker may be stripped. A marker carrying a currency
Expand All @@ -100,8 +107,12 @@ function isStrippableMarker(letters: string | undefined, symbol: string | undefi
return isCurrencyMarkerLetters(letters)
}

/** Only digits and separators, with at least one digit. */
const AMOUNT_SHAPE = /^[+-]?[\d.,]*\d[\d.,]*$/
/**
* Only digits and separators, with at least one digit. The run before the
* first digit admits only separators, so no character can be claimed by both
* runs and a failed match backtracks in linear time.
*/
const AMOUNT_SHAPE = /^[+-]?[.,]*\d[\d.,]*$/

/**
* Whether `text` is validly grouped by `separator`: a first group of 1-3 digits
Expand Down Expand Up @@ -251,14 +262,16 @@ export function parseCurrencyInput(raw: unknown, currencyCode?: string): number
const withoutPrefix = body.replace(CURRENCY_MARKER_PREFIX, (match, sign, letters, symbol) => {
if (!isStrippableMarker(letters, symbol)) return match
hadMarker = true
return sign
})
const withoutMarkers = withoutPrefix.replace(CURRENCY_MARKER_SUFFIX, (match, letters, symbol) => {
if (!isStrippableMarker(letters, symbol)) return match
hadMarker = true
return ''
return sign ?? ''
})
const cleaned = withoutMarkers.replace(/[\s\u00a0\u202f\u2019']/gu, '')
const withoutMarkers = withoutPrefix
.trimEnd()
.replace(CURRENCY_MARKER_SUFFIX, (match, letters, symbol) => {
if (!isStrippableMarker(letters, symbol)) return match
hadMarker = true
return ''
})
const cleaned = withoutMarkers.replace(/[\s\u2019']/gu, '')
// What remains must be ONLY digits and separators. Anything else — a
// US-format date, leftover prose — is not an amount.
if (!AMOUNT_SHAPE.test(cleaned)) return null
Expand Down
Loading