ghostfolio/libs/common/src/lib/helper.spec.ts
Thomas Kaul 005890d785
Improve extractNumberFromString() for international number formats (#2843)
* Set up test

* Add support for international formatted numbers

* Expose locale in scraper configuration

* Update changelog
2024-01-13 16:17:38 +01:00

40 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { extractNumberFromString } from '@ghostfolio/common/helper';
describe('Helper', () => {
describe('Extract number from string', () => {
it('Get decimal number', async () => {
expect(extractNumberFromString({ value: '999.99' })).toEqual(999.99);
});
it('Get decimal number (with spaces)', async () => {
expect(extractNumberFromString({ value: ' 999.99 ' })).toEqual(999.99);
});
it('Get decimal number (with currency)', async () => {
expect(extractNumberFromString({ value: '999.99 CHF' })).toEqual(999.99);
});
it('Get decimal number (comma notation)', async () => {
expect(
extractNumberFromString({ locale: 'de-DE', value: '999,99' })
).toEqual(999.99);
});
it('Get decimal number with group (dot notation)', async () => {
expect(
extractNumberFromString({ locale: 'de-CH', value: '99999.99' })
).toEqual(99999.99);
});
it('Get decimal number with group (comma notation)', async () => {
expect(
extractNumberFromString({ locale: 'de-DE', value: '99.999,99' })
).toEqual(99999.99);
});
it('Not a number', async () => {
expect(extractNumberFromString({ value: 'X' })).toEqual(NaN);
});
});
});