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
Draft
mikimandoki
wants to merge
4
commits into
oss-apps:main
Choose a base branch
from
mikimandoki:feat/expression-parser
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 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 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,55 @@ | ||
| import { isExpression, 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('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(); | ||
| }); | ||
| }); |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔎 Supported by static analysis
Script executed:
Repository: oss-apps/split-pro
Length of output: 3651
Script executed:
Repository: oss-apps/split-pro
Length of output: 8936
Script executed:
Repository: oss-apps/split-pro
Length of output: 23686
Script executed:
Repository: oss-apps/split-pro
Length of output: 18812
Recalculate the expression and participant splits when the currency changes.
A currency switch can persist the expense amount in the new currency while participant amounts remain in the old currency.
BalanceViewcalculates group balances from participant amounts and labels them withExpense.currency, so the group balance can show the old-unit amount as the new currency. No inspected constraint reconciles these fields. Editing or deleting the expense provides only a manual recovery path.Do not use the stored amount alone at submit. When the currency changes, re-evaluate the expression with the selected currency and pass the result through
setAmount, which also recalculates participant splits. Alternatively, clear the expression and require a new amount.