Browse Source

Task/preserve negative numbers in extractNumberFromString (#7377)

* Preserve negative numbers in extractNumberFromString

* Update changelog
pull/7382/head
Arham Amin 2 days ago
committed by GitHub
parent
commit
3739b76885
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 1
      CHANGELOG.md
  2. 20
      libs/common/src/lib/helper.spec.ts
  3. 7
      libs/common/src/lib/helper.ts

1
CHANGELOG.md

@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Extended the `extractNumberFromString()` function to support negative values
- Restricted the symbol data endpoint (`GET /api/v1/symbol/:dataSource/:symbol`) to authenticated users
- Removed the deprecated `auth` endpoint of the login with _Security Token_ (`GET`)
- Simplified the `getHistorical()` function response in the data provider interface

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

@ -11,6 +11,10 @@ describe('Helper', () => {
expect(extractNumberFromString({ value: '999.99' })).toEqual(999.99);
});
it('Get negative decimal number', () => {
expect(extractNumberFromString({ value: '-999.99' })).toEqual(-999.99);
});
it('Get decimal number (with spaces)', () => {
expect(extractNumberFromString({ value: ' 999.99 ' })).toEqual(999.99);
});
@ -19,6 +23,12 @@ describe('Helper', () => {
expect(extractNumberFromString({ value: '999.99 CHF' })).toEqual(999.99);
});
it('Get negative decimal number (with currency)', () => {
expect(extractNumberFromString({ value: '-999.99 CHF' })).toEqual(
-999.99
);
});
it('Get decimal number (comma notation)', () => {
expect(
extractNumberFromString({ locale: 'de-DE', value: '999,99' })
@ -37,12 +47,22 @@ describe('Helper', () => {
).toEqual(99999.99);
});
it('Get negative decimal number with group (comma notation)', () => {
expect(
extractNumberFromString({ locale: 'de-DE', value: '-99.999,99' })
).toEqual(-99999.99);
});
it('Get decimal number (comma notation) for locale where currency is not grouped by default', () => {
expect(
extractNumberFromString({ locale: 'es-ES', value: '999,99' })
).toEqual(999.99);
});
it('Get decimal number (with hyphenated text)', () => {
expect(extractNumberFromString({ value: 'BRK-B 425.30' })).toEqual(425.3);
});
it('Not a number', () => {
expect(extractNumberFromString({ value: 'X' })).toEqual(NaN);
});

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

@ -209,12 +209,17 @@ export function extractNumberFromString({
value: string;
}): number | undefined {
try {
// Only a leading minus sign indicates a negative value. Detect it before
// stripping so that hyphens within the text cannot flip the sign.
const isNegative = value.trim().startsWith('-');
// Remove non-numeric characters (excluding international formatting characters)
const numericValue = value.replace(/[^\d.,'’\s]/g, '');
const parser = new NumberParser(locale);
const parsedValue = parser.parse(numericValue);
return parser.parse(numericValue);
return isNegative ? -parsedValue : parsedValue;
} catch {
return undefined;
}

Loading…
Cancel
Save