Browse Source

fix: normalize de-CH number separators

pull/6563/head
Winn Cook 2 weeks ago
parent
commit
bcaab993a3
  1. 12
      libs/common/src/lib/helper.spec.ts
  2. 7
      libs/common/src/lib/helper.ts

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

@ -3,6 +3,8 @@ import {
getNumberFormatGroup
} from '@ghostfolio/common/helper';
const DE_CH_GROUP_SEPARATORS = ["'", '’'];
describe('Helper', () => {
describe('Extract number from string', () => {
it('Get decimal number', () => {
@ -29,6 +31,12 @@ describe('Helper', () => {
).toEqual(99999.99);
});
it('Get decimal number with straight apostrophe group (dot notation)', () => {
expect(
extractNumberFromString({ locale: 'de-CH', value: "99'999.99" })
).toEqual(99999.99);
});
it('Get decimal number with group (comma notation)', () => {
expect(
extractNumberFromString({ locale: 'de-DE', value: '99.999,99' })
@ -54,12 +62,12 @@ describe('Helper', () => {
});
it('Get de-CH number format group', () => {
expect(getNumberFormatGroup('de-CH')).toEqual('’');
expect(DE_CH_GROUP_SEPARATORS).toContain(getNumberFormatGroup('de-CH'));
});
it('Get de-CH number format group when it is default', () => {
languageGetter.mockReturnValue('de-CH');
expect(getNumberFormatGroup()).toEqual('’');
expect(DE_CH_GROUP_SEPARATORS).toContain(getNumberFormatGroup());
});
it('Get de-DE number format group', () => {

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

@ -148,10 +148,15 @@ export function extractNumberFromString({
try {
// Remove non-numeric characters (excluding international formatting characters)
const numericValue = value.replace(/[^\d.,'’\s]/g, '');
const groupSeparator = getNumberFormatGroup(locale);
const normalizedNumericValue =
groupSeparator === "'" || groupSeparator === '’'
? numericValue.replace(/['’]/g, groupSeparator)
: numericValue;
const parser = new NumberParser(locale);
return parser.parse(numericValue);
return parser.parse(normalizedNumericValue);
} catch {
return undefined;
}

Loading…
Cancel
Save