Browse Source

Bugfix/preserve negative numbers in extractNumberFromString

extractNumberFromString() stripped the minus sign together with other
non-numeric characters, so negative values were silently converted to
their positive counterparts (e.g. '-999.99' was parsed as 999.99).

Keep the minus sign in the character allow-list so NumberParser can
correctly parse negative values. This helper is used by the manual data
provider, where a user-configured selector can legitimately point at a
negative number.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
pull/7377/head
Arham Wani 1 month ago
parent
commit
1833469f17
  1. 4
      CHANGELOG.md
  2. 16
      libs/common/src/lib/helper.spec.ts
  3. 5
      libs/common/src/lib/helper.ts

4
CHANGELOG.md

@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Removed the deprecated `auth` endpoint of the login with _Security Token_ (`GET`)
- Simplified the `getHistorical()` function response in the data provider interface
### Fixed
- Fixed the parsing of negative numbers in `extractNumberFromString()` (used by the manual data provider) which incorrectly dropped the minus sign
## 3.29.0 - 2026-07-18
### Added

16
libs/common/src/lib/helper.spec.ts

@ -43,6 +43,22 @@ describe('Helper', () => {
).toEqual(999.99);
});
it('Get negative decimal number', () => {
expect(extractNumberFromString({ value: '-999.99' })).toEqual(-999.99);
});
it('Get negative decimal number (with currency)', () => {
expect(extractNumberFromString({ value: '-999.99 CHF' })).toEqual(
-999.99
);
});
it('Get negative decimal number with group (comma notation)', () => {
expect(
extractNumberFromString({ locale: 'de-DE', value: '-99.999,99' })
).toEqual(-99999.99);
});
it('Not a number', () => {
expect(extractNumberFromString({ value: 'X' })).toEqual(NaN);
});

5
libs/common/src/lib/helper.ts

@ -210,8 +210,9 @@ export function extractNumberFromString({
value: string;
}): number | undefined {
try {
// Remove non-numeric characters (excluding international formatting characters)
const numericValue = value.replace(/[^\d.,'’\s]/g, '');
// Remove non-numeric characters (excluding international formatting
// characters and the minus sign to preserve negative values)
const numericValue = value.replace(/[^\d.,'’\s-]/g, '');
const parser = new NumberParser(locale);

Loading…
Cancel
Save