Ghost/core/frontend/helpers/price.js
Hannah Wolfe 658a6dd284 Cleaned all usages of proxy in helpers
- the proxy should always be used to access other parts of Ghost, including the urlService etc
- use consistent ES6 style for requires
- minimise use of lodash where possible
- remove circular dependency between proxy and template util
- End goal here is to enforce that the only link between helpers + the rest of Ghost is the proxy
2020-03-31 12:42:15 +01:00

33 lines
984 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 isNumber = require('lodash/isNumber');
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;
};