Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: STRF-9707 Improve money helper to support input params #164

Merged
merged 1 commit into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions helpers/money.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const _ = require('lodash');

/**
* Format numbers
*
Expand All @@ -17,19 +15,32 @@ function numberFormat(value, n, s, c) {
}

const factory = globals => {
return function(value) {
return function(...args) {
const siteSettings = globals.getSiteSettings();
const money = siteSettings.money;

if (!_.isNumber(value)) {
return '';
// remove options hash object
args.pop();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we able to exclude this by doing something like return function(options, ...args) to pull out the first param as "options" and just use the remaining in args to avoid this step?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really, cause I'm removing the last parameter, rather then the first one.
So it should be like return function(...args, options), but this kind of syntax is not supported 😒

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it necessary to remove this at all, since we're just accessing the other elements by args[0] etc, wont those indexes be the same regardless if the last element in present?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jmwiese the thing is that number of arguments may vary. Just to be sure, that hash object (that has handlebars utility functions) was removed, I'm using a pop(). We are also using same approach in other helpers, to actually read that hash object by assigning it to a variable.


let value = args[0];

if (isNaN(value)) {
throw new TypeError("money helper accepts only Number's as first parameter");
}

const decimalPlaces = args[1] || money.decimal_places;

if (isNaN(decimalPlaces)) {
throw new TypeError("money helper accepts only Number's for decimal places");
}
const thousandsToken = args[2] || money.thousands_token;
const decimalToken = args[3] || money.decimal_token;

value = numberFormat(
value,
money.decimal_places,
money.thousands_token,
money.decimal_token
decimalPlaces,
thousandsToken,
decimalToken
);

return money.currency_location === 'left'
Expand Down
72 changes: 72 additions & 0 deletions spec/helpers/money.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const Lab = require('lab');
const { expect } = require('code');
const lab = exports.lab = Lab.script();
const describe = lab.experiment;
const it = lab.it;
const { testRunner, renderString } = require('../spec-helpers');

describe('money helper', function() {
const context = {
price: 1234.56,
};
const siteSettings = {
money: {
currency_location: "left",
currency_token: "$",
decimal_places: 2,
thousands_token: ',',
decimal_token: '.',
}
};


it('should correctly set money value from money site settings', function(done) {
const runTestCases = testRunner({context, siteSettings});
runTestCases([
{
input: '{{money price}}',
output: '$ 1,234.56',
},
{
input: '{{money 12.34}}',
output: '$ 12.34',
},
], done);
});

it('should read money site settings from input parameters', function(done) {
const runTestCases = testRunner({context, siteSettings});
runTestCases([
{
input: '{{money price 1}}',
output: '$ 1,234.6',
},
{
input: '{{money price 3}}',
output: '$ 1,234.560',
},
{
input: '{{money price 2 "."}}',
output: '$ 1.234.56',
},
{
input: '{{money price 2 "," ","}}',
output: '$ 1,234,56',
},
], done);
});

it('should throw an exception if the price value parameter has an invalid type', function(done) {
renderString('{{money "hello"}}').catch(err => {
expect(err.message).to.equal("money helper accepts only Number's as first parameter");
done();
});
});

it('should throw an exception if the decimal places parameter has an invalid type', function(done) {
renderString('{{money 1.2 "hello"}}').catch(err => {
expect(err.message).to.equal("money helper accepts only Number's for decimal places");
done();
});
});
});