Browse Source

Refactoring

pull/7377/head
Thomas Kaul 1 month ago
parent
commit
b49438209f
  1. 4
      libs/common/src/lib/helper.spec.ts
  2. 12
      libs/common/src/lib/helper.ts

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

@ -29,6 +29,10 @@ describe('Helper', () => {
); );
}); });
it('Get decimal number (with hyphenated text)', () => {
expect(extractNumberFromString({ value: 'BRK-B 425.30' })).toEqual(425.3);
});
it('Get decimal number (comma notation)', () => { it('Get decimal number (comma notation)', () => {
expect( expect(
extractNumberFromString({ locale: 'de-DE', value: '999,99' }) extractNumberFromString({ locale: 'de-DE', value: '999,99' })

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

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

Loading…
Cancel
Save