Repository navigation
feat: allow calculations in expense amounts #785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
afee0ee
feat: allow calculations in expense amounts
mikimandoki 9d17f8b
fix: preserve expressions and currency precision
mikimandoki d9aedc6
fix: normalize expression decimals to locale
mikimandoki 85df5f3
fix: remove invalid expression toast
mikimandoki ffc9b4f
fix: validate expressions and recalculate splits
mikimandoki b4a8c4c
fix: ignore grouped separators in expression validation
mikimandoki 91f91da
fix: normalize grouped amounts before expression parsing
mikimandoki c9544d8
refactor: bind expression helpers to currency config
mikimandoki 92dc332
Fix expression currency precision and bound arithmetic parsing
krokosik 5f97a48
Merge main into expense expression branch
krokosik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import { | ||
| hasDuplicateDecimalSeparator, | ||
| isExpression, | ||
| removeValidGroupingSeparators, | ||
| safeEvaluateExpression, | ||
| sanitizeExpressionInput, | ||
| } from '../utils/expression'; | ||
|
|
||
| describe('isExpression', () => { | ||
| it.each([ | ||
| ['123.45', false], | ||
| ['-123.45', false], | ||
| ['-', false], | ||
| ['30.5+10.75-12-1.86', true], | ||
| ['10*5', true], | ||
| ['10/4', true], | ||
| ['(2+3)*4', true], | ||
| ])('returns %p for %p', (input, expected) => { | ||
| expect(isExpression(input)).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| describe('hasDuplicateDecimalSeparator', () => { | ||
| it.each([ | ||
| ['1.2.3', '.', ',', '', true], | ||
| ['1.2.3+4', '.', ',', '', true], | ||
| ['(1.2.3)+4', '.', ',', '', true], | ||
| ['1.2+3.4', '.', ',', '', false], | ||
| ['1,2,3', ',', '.', '', true], | ||
| ['1,234.56', '.', ',', ',', false], | ||
| ['1,000,000', '.', ',', ',', false], | ||
| ['1,000,000.56', '.', ',', ',', false], | ||
| ['1,2,3', '.', ',', ',', true], | ||
| ['1.000.000,56', ',', '.', '.', false], | ||
| ])( | ||
| 'returns %p for %p with separators %p, %p, and group %p', | ||
| (input, decimal, alternative, group, expected) => { | ||
| expect(hasDuplicateDecimalSeparator(input, decimal, alternative, group)).toBe(expected); | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| describe('removeValidGroupingSeparators', () => { | ||
| it.each([ | ||
| ['1,000,000', '.', ',', '1000000'], | ||
| ['1,000,000.50+1,000', '.', ',', '1000000.50+1000'], | ||
| ['1.000.000,50+1.000', ',', '.', '1000000,50+1000'], | ||
| ['1,5+2', '.', ',', '1,5+2'], | ||
| ['1,2,3+4', '.', ',', '1,2,3+4'], | ||
| ])('normalizes grouped amounts %p', (input, decimalSeparator, groupSeparator, expected) => { | ||
| expect(removeValidGroupingSeparators(input, decimalSeparator, groupSeparator)).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| describe('sanitizeExpressionInput', () => { | ||
| it('keeps arithmetic and decimal characters only', () => { | ||
| expect(sanitizeExpressionInput('$30.5 + 10,75 xyz', 2)).toBe('30.5+10.75'); | ||
| }); | ||
|
|
||
| it('limits each operand to the currency precision', () => { | ||
| expect(sanitizeExpressionInput('1.234+3.456/0.789', 2)).toBe('1.23+3.45/0.78'); | ||
| }); | ||
|
|
||
| it('normalizes alternate decimal marks to the locale separator', () => { | ||
| expect(sanitizeExpressionInput('12,5+3.75', 2, '.', ',')).toBe('12.5+3.75'); | ||
| expect(sanitizeExpressionInput('12,5+3.75', 2, ',', '.')).toBe('12,5+3,75'); | ||
| }); | ||
|
|
||
| it('drops fractional digits for zero-decimal currencies', () => { | ||
| expect(sanitizeExpressionInput('12.34+5.6', 0)).toBe('12+5'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('safeEvaluateExpression', () => { | ||
| it.each([ | ||
| ['30.5+10.75-12-1.86', { numerator: 2739n, denominator: 100n }], | ||
| ['2+3*4', { numerator: 14n, denominator: 1n }], | ||
| ['(2+3)*4', { numerator: 20n, denominator: 1n }], | ||
| ['10/4', { numerator: 5n, denominator: 2n }], | ||
| ['2++3', { numerator: 5n, denominator: 1n }], | ||
| ['0.1+0.2', { numerator: 3n, denominator: 10n }], | ||
| ['1,5+2', { numerator: 7n, denominator: 2n }], | ||
| ['12,5+3,75', { numerator: 65n, denominator: 4n }], | ||
| ['9007199254740992+1', { numerator: 9007199254740993n, denominator: 1n }], | ||
| ['-10+5', { numerator: -5n, denominator: 1n }], | ||
| ])('evaluates %p exactly', (input, expected) => { | ||
| expect(safeEvaluateExpression(input)).toEqual(expected); | ||
| }); | ||
|
|
||
| it.each(['', 'abc', '30.5+', '()', '(2+3', '30/0', '1.2.3'])('rejects %p', (input) => { | ||
| expect(safeEvaluateExpression(input)).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('expression limits', () => { | ||
| it.each([ | ||
| `${'('.repeat(10000)}1${')'.repeat(10000)}`, | ||
| `${'('.repeat(33)}1${')'.repeat(33)}`, | ||
| `${'9'.repeat(33)}+1`, | ||
| `${'1+'.repeat(257)}1`, | ||
| ])('rejects oversized input safely', (input) => { | ||
| expect(safeEvaluateExpression(input)).toBeNull(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.