From 3739b768856fc4148027af44b3c22c6592bc268c Mon Sep 17 00:00:00 2001 From: Arham Amin <132888838+arhxam@users.noreply.github.com> Date: Sun, 19 Jul 2026 21:12:32 +0530 Subject: [PATCH] Task/preserve negative numbers in extractNumberFromString (#7377) * Preserve negative numbers in extractNumberFromString * Update changelog --- CHANGELOG.md | 1 + libs/common/src/lib/helper.spec.ts | 20 ++++++++++++++++++++ libs/common/src/lib/helper.ts | 7 ++++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ca1c9291..82954caa4 100644 --- a/CHANGELOG.md +++ b/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 diff --git a/libs/common/src/lib/helper.spec.ts b/libs/common/src/lib/helper.spec.ts index 6a6fe4773..d33f10452 100644 --- a/libs/common/src/lib/helper.spec.ts +++ b/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); }); diff --git a/libs/common/src/lib/helper.ts b/libs/common/src/lib/helper.ts index 6a135eef6..ad8674d81 100644 --- a/libs/common/src/lib/helper.ts +++ b/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; }