Skip to content

Commit

Permalink
actually use decimal separator when importing transaction values
Browse files Browse the repository at this point in the history
  • Loading branch information
powerpaul17 committed Oct 12, 2022
1 parent a63b67f commit e92e0d7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/components/TransactionImportDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
import FileInput from './FileInput.vue';
import Upload from 'vue-material-design-icons/Upload.vue';
import { Utils } from '../utils/utils';
const transactionService = useTransactionService();
const splitStore = useSplitStore();
Expand Down Expand Up @@ -214,7 +215,7 @@
lines: [],
isValid: false,
validator: (line) => {
return !Number.isNaN(Number(line));
return !Number.isNaN(Utils.parseNumber(line, decimalSeparator.value));
}
}
});
Expand Down
19 changes: 19 additions & 0 deletions src/utils/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,23 @@ describe('Utils', () => {
]);
});
});

describe('parseNumber', () => {
it('should return a number of a string', () => {
expect(Utils.parseNumber('123.456')).to.be.equal(123.456);
});

it('should return NaN if it is not a parseable number', () => {
expect(Utils.parseNumber('abc')).to.be.NaN;
});

it('should respect decimal separator', () => {
expect(Utils.parseNumber('123,456', ',')).to.be.equal(123.456);
});

it('should ignore thousand separators', () => {
expect(Utils.parseNumber('12,345.678')).to.be.equal(12345.678);
expect(Utils.parseNumber('12.456,789', ',')).to.be.equal(12456.789);
});
});
});
9 changes: 9 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,13 @@ export class Utils {
decimalDigits ? `${decimalSeparator}${decimalDigits}` : ''
}`;
}

public static parseNumber(numberString: string, decimalSeparator = '.'): number {
return Number(
numberString
.replace(decimalSeparator, '#')
.replace(/[\s,.]+/, '')
.replace('#', '.'));
}

}

0 comments on commit e92e0d7

Please sign in to comment.