Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript π€
TypeScript angularetffinancefintechghostfoliohacktoberfestinvestingnestjsosspersonal-financeportfolioprismasoftwarestocktrackertradingtypescriptwealth-managementweb
Task/preserve negative numbers in extractNumberFromString (#7377)
Arham Amin3739b768856f
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 2ca1c9291f..82954caa4d 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 interfacediff --git a/libs/common/src/lib/helper.spec.ts b/libs/common/src/lib/helper.spec.tsindex 6a6fe47734..d33f104522 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.tsindex 6a135eef63..ad8674d815 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;}