-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat[ATLAS-123]: add minor/major unit conversion apis in currency mod…
…ule (#63) * [chore]: added additional types for better type checking --------- Co-authored-by: tarun khanna <[email protected]>
- Loading branch information
1 parent
f8b1e78
commit 41852b9
Showing
13 changed files
with
735 additions
and
204 deletions.
There are no files selected for viewing
This file contains 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,5 @@ | ||
--- | ||
'@razorpay/i18nify-js': patch | ||
--- | ||
|
||
[feat]: add major/minor conversion apis in currency module |
This file contains 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 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
28 changes: 28 additions & 0 deletions
28
packages/i18nify-js/src/modules/currency/__tests__/convertToMajorUnit.test.ts
This file contains 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,28 @@ | ||
import convertToMajorUnit from '../convertToMajorUnit'; | ||
import { CurrencyCodeType } from '../types'; | ||
|
||
describe('currency - convertToMajorUnit', () => { | ||
const testCases: { | ||
amount: number; | ||
currency: CurrencyCodeType; | ||
expectedResult: number; | ||
}[] = [ | ||
{ amount: 100, currency: 'USD', expectedResult: 1 }, | ||
{ amount: 100, currency: 'GBP', expectedResult: 1 }, | ||
]; | ||
|
||
testCases.forEach(({ amount, currency, expectedResult }) => { | ||
it(`should correctly convert ${amount} of minor unit ${currency} to ${expectedResult}`, () => { | ||
const result = convertToMajorUnit(amount, { currency: currency }); | ||
expect(result).toBe(expectedResult); | ||
}); | ||
}); | ||
|
||
it('should throw an error for unsupported currency codes', () => { | ||
const unsupportedCurrencyCode = 'XXX'; | ||
expect(() => { | ||
// @ts-expect-error intented invalid currencyCode for testing | ||
convertToMajorUnit(100, { currency: unsupportedCurrencyCode }); | ||
}).toThrow('Unsupported currency XXX'); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
packages/i18nify-js/src/modules/currency/__tests__/convertToMinorUnit.test.ts
This file contains 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,28 @@ | ||
import convertToMinorUnit from '../convertToMinorUnit'; | ||
import { CurrencyCodeType } from '../types'; | ||
|
||
describe('currency - convertToMinorUnit', () => { | ||
const testCases: { | ||
amount: number; | ||
currency: CurrencyCodeType; | ||
expectedResult: number; | ||
}[] = [ | ||
{ amount: 1, currency: 'USD', expectedResult: 100 }, | ||
{ amount: 1, currency: 'GBP', expectedResult: 100 }, | ||
]; | ||
|
||
testCases.forEach(({ amount, currency, expectedResult }) => { | ||
it(`should correctly convert ${amount} of minor unit ${currency} to ${expectedResult}`, () => { | ||
const result = convertToMinorUnit(amount, { currency: currency }); | ||
expect(result).toBe(expectedResult); | ||
}); | ||
}); | ||
|
||
it('should throw an error for unsupported currency codes', () => { | ||
const unsupportedCurrencyCode = 'XXX'; | ||
expect(() => { | ||
// @ts-expect-error intented invalid currencyCode for testing | ||
convertToMinorUnit(100, { currency: unsupportedCurrencyCode }); | ||
}).toThrow('Unsupported currency XXX'); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
packages/i18nify-js/src/modules/currency/convertToMajorUnit.ts
This file contains 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,34 @@ | ||
import { withErrorBoundary } from '../../common/errorBoundary'; | ||
import { CURRENCIES } from './data/currencies'; | ||
import { CurrencyCodeType, CurrencyType } from './types'; | ||
|
||
/** | ||
* Converts an amount from a minor currency unit to a major currency unit. | ||
* | ||
* The function takes an amount in the minor unit (e.g., cents, pence) and a currency code, | ||
* then converts the amount to the major unit (e.g., dollars, pounds) using the conversion rate | ||
* defined in the CURRENCIES object. If the currency code is not supported, it throws an error. | ||
* | ||
* @param {number} amount - The amount in the minor currency unit. | ||
* @param {object} options - The options object | ||
* @returns {number} - The amount converted to the major currency unit. | ||
* @throws Will throw an error if the currency code is not supported. | ||
*/ | ||
const convertToMajorUnit = ( | ||
amount: number, | ||
options: { | ||
currency: CurrencyCodeType; | ||
}, | ||
): number => { | ||
const currencyInfo = CURRENCIES[options.currency] as CurrencyType; | ||
|
||
if (!currencyInfo) | ||
throw new Error(`Unsupported currency ${options.currency}`); | ||
|
||
const minorUnitMultiplier = currencyInfo.minorUnitMultiplier || 100; | ||
|
||
const higherCurrencyValue = amount / minorUnitMultiplier; | ||
return higherCurrencyValue; | ||
}; | ||
|
||
export default withErrorBoundary<typeof convertToMajorUnit>(convertToMajorUnit); |
34 changes: 34 additions & 0 deletions
34
packages/i18nify-js/src/modules/currency/convertToMinorUnit.ts
This file contains 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,34 @@ | ||
import { withErrorBoundary } from '../../common/errorBoundary'; | ||
import { CURRENCIES } from './data/currencies'; | ||
import { CurrencyCodeType, CurrencyType } from './types'; | ||
|
||
/** | ||
* Converts an amount from a major currency unit to a minor currency unit. | ||
* | ||
* The function takes an amount in the major unit (e.g., dollars, pounds) and a currency code, | ||
* then converts the amount to the minor unit (e.g., cents, pence) using the conversion rate | ||
* defined in the CURRENCIES object. If the currency code is not supported, it throws an error. | ||
* | ||
* @param {number} amount - The amount in the major currency unit. | ||
* @param {object} options - The options object | ||
* @returns {number} - The amount converted to the minor currency unit. | ||
* @throws Will throw an error if the currency code is not supported. | ||
*/ | ||
const convertToMinorUnit = ( | ||
amount: number, | ||
options: { | ||
currency: CurrencyCodeType; | ||
}, | ||
): number => { | ||
const currencyInfo = CURRENCIES[options.currency] as CurrencyType; | ||
|
||
if (!currencyInfo) | ||
throw new Error(`Unsupported currency ${options.currency}`); | ||
|
||
const minorUnitMultiplier = currencyInfo.minorUnitMultiplier || 100; | ||
|
||
const lowerCurrencyValue = amount * minorUnitMultiplier; | ||
return lowerCurrencyValue; | ||
}; | ||
|
||
export default withErrorBoundary<typeof convertToMinorUnit>(convertToMinorUnit); |
Oops, something went wrong.