2
1
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2023-12-13 21:00:40 +01:00

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.
This commit is contained in:
Naz Gargol 2020-01-27 18:41:12 +07:00 committed by Kevin Ansfield
parent 5f49144985
commit 07e1a2406b
5 changed files with 85 additions and 1 deletions

View file

@ -36,6 +36,7 @@ coreHelpers.pagination = require('./pagination');
coreHelpers.plural = require('./plural');
coreHelpers.post_class = require('./post_class');
coreHelpers.prev_post = require('./prev_next');
coreHelpers.price = require('./price');
coreHelpers.next_post = require('./prev_next');
coreHelpers.reading_time = require('./reading_time');
coreHelpers.t = require('./t');
@ -84,6 +85,7 @@ registerAllCoreHelpers = function registerAllCoreHelpers() {
registerThemeHelper('pagination', coreHelpers.pagination);
registerThemeHelper('plural', coreHelpers.plural);
registerThemeHelper('post_class', coreHelpers.post_class);
registerThemeHelper('price', coreHelpers.price);
registerThemeHelper('reading_time', coreHelpers.reading_time);
registerThemeHelper('t', coreHelpers.t);
registerThemeHelper('tags', coreHelpers.tags);

View file

@ -0,0 +1,32 @@
// # {{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;
};

View file

@ -607,6 +607,10 @@
"plural": {
"valuesMustBeDefined": "All values must be defined for empty, singular and plural"
},
"price": {
"attrIsRequired": "Attribute is required e.g. \\{\\{price plan.amount\\}\\}",
"attrMustBeNumeric": "Attribute value should be a number"
},
"img_url": {
"attrIsRequired": "Attribute is required e.g. \\{\\{img_url feature_image\\}\\}",
"attrIsUnknown": "Attribute passed to \\{\\{img_url\\}\\} is unknown"

View file

@ -10,7 +10,7 @@ describe('Helpers', function () {
ghostHelpers = [
'asset', 'author', 'authors', 'body_class', 'cancel_link', 'concat', 'content', 'date', 'encode', 'excerpt', 'facebook_url', 'foreach', 'get',
'ghost_foot', 'ghost_head', 'has', 'img_url', 'is', 'lang', 'link', 'link_class', 'meta_description', 'meta_title', 'navigation',
'next_post', 'page_url', 'pagination', 'plural', 'post_class', 'prev_post', 'reading_time', 't', 'tags', 'title', 'twitter_url',
'next_post', 'page_url', 'pagination', 'plural', 'post_class', 'prev_post', 'price', 'reading_time', 't', 'tags', 'title', 'twitter_url',
'url'
],
expectedHelpers = _.concat(hbsHelpers, ghostHelpers);

View file

@ -0,0 +1,46 @@
const should = require('should');
const helpers = require.main.require('core/frontend/helpers');
const handlebars = require.main.require('core/frontend/services/themes/engine').handlebars;
function compile(templateString) {
const template = handlebars.compile(templateString);
template.with = (locals = {}, globals) => {
return template(locals, globals);
};
return template;
}
describe('{{price}} helper', function () {
before(function () {
handlebars.registerHelper('price', helpers.price);
});
it('throws an error for no provided parameters', function () {
(function compileWith() {
compile('{{price}}')
.with({});
}).should.throw();
});
it('throws an error for undefined parameter', function () {
(function compileWith() {
compile('{{price @dont.exist}}')
.with({});
}).should.throw();
});
it('throws if argument is not a number', function () {
(function compileWith() {
compile('{{price "not_a_number"}}')
.with({});
}).should.throw();
});
it('will format decimal adjusted amount', function () {
compile('{{price 2000}}')
.with({})
.should.equal('20');
});
});