2020-01-27 14:41:12 +03:00
|
|
|
// # {{price}} helper
|
|
|
|
//
|
|
|
|
// Usage: `{{price 2100}}`
|
|
|
|
//
|
|
|
|
// Returns amount equal to the dominant denomintation of the currency.
|
|
|
|
// For example, if 2100 is passed, it will return 21.
|
2020-03-30 23:23:02 +03:00
|
|
|
const isNumber = require('lodash/isNumber');
|
2020-04-08 18:56:37 +03:00
|
|
|
const {errors, i18n} = require('../services/proxy');
|
2020-01-27 14:41:12 +03:00
|
|
|
|
|
|
|
module.exports = function price(amount) {
|
|
|
|
// CASE: if no amount is passed, e.g. `{{price}}` we throw an error
|
|
|
|
if (arguments.length < 2) {
|
|
|
|
throw new errors.IncorrectUsageError({
|
|
|
|
message: i18n.t('warnings.helpers.price.attrIsRequired')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// CASE: if amount is passed, but it is undefined we throw an error
|
|
|
|
if (amount === undefined) {
|
|
|
|
throw new errors.IncorrectUsageError({
|
|
|
|
message: i18n.t('warnings.helpers.price.attrIsRequired')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-30 23:23:02 +03:00
|
|
|
if (!isNumber(amount)) {
|
2020-01-27 14:41:12 +03:00
|
|
|
throw new errors.IncorrectUsageError({
|
|
|
|
message: i18n.t('warnings.helpers.price.attrMustBeNumeric')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return amount / 100;
|
|
|
|
};
|