Ghost/core/frontend/helpers/price.js
Naz Gargol 07e1a2406b Added {{price}} helper for formatting stripe amounts (ie. "1935" to "19.35") (#11473)
no issue

- This helper allows to format currencies that use decimal normalization. For example 19.35 USD is served as 1935 from the API which always needs to be divided by 100 to get a dollar ammount.
2020-01-27 11:41:12 +00:00

33 lines
970 B
JavaScript

// # {{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.
const _ = require('lodash');
const {errors, i18n} = require('./proxy');
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')
});
}
if (!_.isNumber(amount)) {
throw new errors.IncorrectUsageError({
message: i18n.t('warnings.helpers.price.attrMustBeNumeric')
});
}
return amount / 100;
};