-
Notifications
You must be signed in to change notification settings - Fork 0
Feat: Add util to compute price diff #265
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
+137
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a4fbb3f
Feat: Add util to compute price diff
paulofph f90d906
Merge branch 'main' into feat/add-price-diff-util
paulofph 612e23b
Fix lint
paulofph 50c2137
Merge branch 'main' into feat/add-price-diff-util
paulofph e227daf
Add tests
paulofph 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,64 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { computePriceDiff } from './compute-price-diff'; | ||
|
|
||
| describe('computePriceDiff', () => { | ||
| describe('returns null', () => { | ||
| it('when current amount is zero', () => { | ||
| expect(computePriceDiff('10.00', '0.00', { format: 'absolute' })).toBeNull(); | ||
| }); | ||
|
|
||
| it('when offer and current amounts are equal', () => { | ||
| expect(computePriceDiff('30.00', '30.00', { format: 'absolute' })).toBeNull(); | ||
| }); | ||
|
|
||
| it('when offer is worse and onlyIfBetter is true', () => { | ||
| expect(computePriceDiff('40.00', '30.00', { format: 'absolute', onlyIfBetter: true })).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('percentage format', () => { | ||
| it('returns correct percentage when offer is better (lower)', () => { | ||
| const result = computePriceDiff('25.00', '30.00', { format: 'percentage' }); | ||
| expect(result).not.toBeNull(); | ||
| expect(result!.isBetter).toBe(true); | ||
| expect(result!.formattedDiff).toBe('16.7%'); | ||
| }); | ||
|
|
||
| it('returns correct percentage when offer is worse (higher)', () => { | ||
| const result = computePriceDiff('36.00', '30.00', { format: 'percentage' }); | ||
| expect(result).not.toBeNull(); | ||
| expect(result!.isBetter).toBe(false); | ||
| expect(result!.formattedDiff).toBe('20.0%'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('absolute format', () => { | ||
| it('returns formatted absolute diff when offer is better', () => { | ||
| const result = computePriceDiff('25.00', '30.00', { format: 'absolute', currency: 'EUR', locale: 'de-DE' }); | ||
| expect(result).not.toBeNull(); | ||
| expect(result!.isBetter).toBe(true); | ||
| expect(result!.formattedDiff).toContain('5'); | ||
| }); | ||
|
|
||
| it('returns formatted absolute diff when offer is worse', () => { | ||
| const result = computePriceDiff('35.00', '30.00', { format: 'absolute', currency: 'EUR', locale: 'de-DE' }); | ||
| expect(result).not.toBeNull(); | ||
| expect(result!.isBetter).toBe(false); | ||
| expect(result!.formattedDiff).toContain('5'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('onlyIfBetter', () => { | ||
| it('returns result when offer is better and onlyIfBetter is true', () => { | ||
| const result = computePriceDiff('20.00', '30.00', { format: 'percentage', onlyIfBetter: true }); | ||
| expect(result).not.toBeNull(); | ||
| expect(result!.isBetter).toBe(true); | ||
| }); | ||
|
|
||
| it('returns result when offer is worse and onlyIfBetter is false', () => { | ||
| const result = computePriceDiff('40.00', '30.00', { format: 'percentage', onlyIfBetter: false }); | ||
| expect(result).not.toBeNull(); | ||
| expect(result!.isBetter).toBe(false); | ||
| }); | ||
| }); | ||
| }); |
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,70 @@ | ||
| import type { Currency as DineroCurrency } from 'dinero.js'; | ||
| import { DEFAULT_CURRENCY, DEFAULT_INTEGER_AMOUNT_PRECISION } from '../money/constants'; | ||
| import { formatAmount, parseDecimalValue } from '../money/formatters'; | ||
| import { toDinero } from '../money/to-dinero'; | ||
| import type { Currency } from '../shared/types'; | ||
|
|
||
| export type PriceDiffFormat = 'percentage' | 'absolute'; | ||
|
|
||
| export type PriceDiffOptions = { | ||
| format: PriceDiffFormat; | ||
| onlyIfBetter?: boolean; | ||
| currency?: string; | ||
| locale?: string; | ||
| }; | ||
|
|
||
| export type PriceDiff = { | ||
| isBetter: boolean; | ||
| formattedDiff: string; | ||
| }; | ||
|
|
||
| /** | ||
| * Computes the price difference between an offer price and a current price, | ||
| * returning the formatted savings or increase for display purposes. | ||
| * | ||
| * @param offerAmount - The new/offer price as a decimal string (e.g. "25.00") | ||
| * @param currentAmount - The current/reference price as a decimal string (e.g. "30.00") | ||
| * @param options - Configuration: format, onlyIfBetter, currency, locale | ||
| * @returns `{ isBetter, formattedDiff }` or `null` if not applicable | ||
| */ | ||
| export const computePriceDiff = ( | ||
| offerAmount: string, | ||
| currentAmount: string, | ||
| options: PriceDiffOptions, | ||
| ): PriceDiff | null => { | ||
| const { format, onlyIfBetter = false, currency, locale } = options; | ||
| const resolvedCurrency = (currency || DEFAULT_CURRENCY) as DineroCurrency; | ||
|
|
||
| const currentDinero = toDinero(parseDecimalValue(currentAmount), resolvedCurrency); | ||
| const offerDinero = toDinero(parseDecimalValue(offerAmount), resolvedCurrency); | ||
|
|
||
| if (currentDinero.isZero()) return null; | ||
|
|
||
| const diff = currentDinero.subtract(offerDinero); | ||
|
|
||
| if (diff.isZero()) return null; | ||
|
|
||
| const isBetter = !diff.isNegative(); | ||
|
|
||
| if (onlyIfBetter && !isBetter) return null; | ||
|
|
||
| const resolvedLocale = locale ?? (typeof navigator !== 'undefined' ? navigator.language : undefined); | ||
|
|
||
| const absDiff = diff.isNegative() ? diff.multiply(-1) : diff; | ||
|
|
||
| let formattedDiff: string; | ||
|
|
||
| if (format === 'percentage') { | ||
| const percentage = (absDiff.getAmount() / currentDinero.getAmount()) * 100; | ||
|
|
||
| formattedDiff = `${percentage.toFixed(1)}%`; | ||
| } else { | ||
| formattedDiff = formatAmount({ | ||
| amount: absDiff.convertPrecision(DEFAULT_INTEGER_AMOUNT_PRECISION).getAmount(), | ||
| currency: resolvedCurrency as Currency, | ||
| locale: resolvedLocale, | ||
| }); | ||
| } | ||
|
|
||
| return { isBetter, formattedDiff }; | ||
| }; | ||
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.