Skip to content

Commit

Permalink
Merge pull request #554 from Opetushallitus/decimal-fix
Browse files Browse the repository at this point in the history
Add maybeParseNumber to handle comma
  • Loading branch information
SalamaGofore authored Oct 17, 2023
2 parents d97ee92 + 917ed7a commit 3863fe6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/main/app/src/utils/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
getValuesForSaving,
isDeepEmptyFormValues,
formatDateValue,
maybeParseNumber,
} from '#/src/utils';

const OBJECT_IN_ARRAY = [
Expand Down Expand Up @@ -60,6 +61,14 @@ test('Should return false for zero', () => {
expect(isDeepEmptyFormValues(0)).toEqual(false);
});

test('parses decimal with comma', () => {
expect(maybeParseNumber('5,8')).toEqual(5.8);
});

test('does not convert text to number', () => {
expect(maybeParseNumber('a')).toEqual('a');
});

test.each([
[
{
Expand Down
5 changes: 4 additions & 1 deletion src/main/app/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,10 @@ export const maybeParseNumber = value => {
if (_.isNil(value) || value === '') {
return null;
}
const numberValue = Number(value);
const numberValue =
_.isString(value) && value.includes(',')
? Number(value.replace(',', '.'))
: Number(value);
return _.isNaN(numberValue) ? value : numberValue;
};

Expand Down

0 comments on commit 3863fe6

Please sign in to comment.