mirror of
https://github.com/kingomarnajjar/flex-template-web.git
synced 2026-07-28 20:53:24 +10:00
Convert Money to number
This commit is contained in:
parent
1f103a82f9
commit
1a5a587be4
2 changed files with 52 additions and 0 deletions
|
|
@ -1,5 +1,6 @@
|
|||
import { trimEnd } from 'lodash';
|
||||
import Decimal from 'decimal.js';
|
||||
import { types } from './sdkLoader';
|
||||
|
||||
////////// Currency manipulation in string format //////////
|
||||
|
||||
|
|
@ -148,3 +149,22 @@ export const convertUnitToSubUnit = (value, subUnitDivisor, useComma = false) =>
|
|||
throw new Error(`value must divisible by ${subUnitDivisor}`);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert Money to a number
|
||||
*
|
||||
* @param {Money} value
|
||||
*
|
||||
* @param {Decimal|Number|String} subUnitDivisor - a ratio between currency's
|
||||
* main unit and sub units
|
||||
*
|
||||
* @return {number} converted value
|
||||
*/
|
||||
export const convertMoneyToNumber = (value, subUnitDivisor) => {
|
||||
if (!(value instanceof types.Money)) {
|
||||
throw new Error('Value must be a Money type');
|
||||
}
|
||||
const subUnitDivisorAsDecimal = convertDivisorToDecimal(subUnitDivisor);
|
||||
const amount = new Decimal(value.amount);
|
||||
return amount.dividedBy(subUnitDivisorAsDecimal).toNumber();
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import Decimal from 'decimal.js';
|
||||
import { types } from './sdkLoader';
|
||||
import {
|
||||
convertDecimalToString,
|
||||
convertMoneyToNumber,
|
||||
convertToDecimal,
|
||||
convertUnitToSubUnit,
|
||||
ensureSeparator,
|
||||
|
|
@ -182,4 +184,34 @@ describe('currency utils', () => {
|
|||
expect(() => convertUnitToSubUnit(1, 'asdf')).toThrowError();
|
||||
});
|
||||
});
|
||||
|
||||
describe('convertMoneyToNumber(value, subUnitDivisor)', () => {
|
||||
const subUnitDivisor = 100;
|
||||
const Money = types.Money;
|
||||
|
||||
it('Money as value', () => {
|
||||
expect(convertMoneyToNumber(new Money(10, 'USD'), subUnitDivisor)).toEqual(0.1);
|
||||
expect(convertMoneyToNumber(new Money(1000, 'USD'), subUnitDivisor)).toEqual(10);
|
||||
expect(convertMoneyToNumber(new Money(9900, 'USD'), subUnitDivisor)).toEqual(99);
|
||||
expect(convertMoneyToNumber(new Money(10099, 'USD'), subUnitDivisor)).toEqual(100.99);
|
||||
});
|
||||
|
||||
it('Wrong type of a parameter', () => {
|
||||
expect(() => convertMoneyToNumber(10, subUnitDivisor)).toThrowError(
|
||||
'Value must be a Money type'
|
||||
);
|
||||
expect(() => convertMoneyToNumber('10', subUnitDivisor)).toThrowError(
|
||||
'Value must be a Money type'
|
||||
);
|
||||
expect(() => convertMoneyToNumber(true, subUnitDivisor)).toThrowError(
|
||||
'Value must be a Money type'
|
||||
);
|
||||
expect(() => convertMoneyToNumber({}, subUnitDivisor)).toThrowError(
|
||||
'Value must be a Money type'
|
||||
);
|
||||
expect(() => convertMoneyToNumber(new Money('asdf', 'USD'), subUnitDivisor)).toThrowError(
|
||||
'[DecimalError] Invalid argument'
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue