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

fixing custom formats by string #29

Merged
merged 1 commit into from
Apr 28, 2014
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
4 changes: 2 additions & 2 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ See the accompanying LICENSE file for terms.

if (formatOptions) {
if (typeof formatOptions === 'string') {
formatOptions = intlGet('formats.date.' + format, options);
formatOptions = intlGet('formats.date.' + formatOptions, options);
Copy link
Collaborator

Choose a reason for hiding this comment

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

So this was just a copy/paste error then that was causing it to not work?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

probably a renaming process, but essentially yes, jshint reported the issue when I was copy and pasting this into the react-intl. I wonder why it didn't do it in this repo, will investigate more.

}

formatOptions = extend({}, formatOptions, hash);
Expand All @@ -124,7 +124,7 @@ See the accompanying LICENSE file for terms.

if (formatOptions) {
if (typeof formatOptions === 'string') {
formatOptions = intlGet('formats.number.' + format, options);
formatOptions = intlGet('formats.number.' + formatOptions, options);
}

formatOptions = extend({}, formatOptions, hash);
Expand Down
56 changes: 55 additions & 1 deletion tests/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
var chai,
expect,
Handlebars,
IntlMessageFormat;
IntlMessageFormat,
timeStamp = 1390518044403;

if (typeof require === 'function') {
chai = require('chai');
Expand Down Expand Up @@ -265,4 +266,57 @@ describe('Helper `intl`', function () {
it('should be a function', function () {
expect(Handlebars.helpers.intl).to.be.a('function');
});

describe('should provide formats', function () {
it('for intlNumber', function () {
var tmpl = '{{#intl formats=intl.formats locales="en-US"}}{{intlNumber NUM "usd"}} {{intlNumber NUM "eur"}} {{intlNumber NUM style="currency" currency="USD"}}{{/intl}}',
ctx = {
intl: {
formats: {
number: {
eur: { style: 'currency', currency: 'EUR' },
usd: { style: 'currency', currency: 'USD' }
}
}
},
NUM: 40000.004
};
expect(Handlebars.compile(tmpl)(ctx)).to.equal('$40,000.00 €40,000.00 $40,000.00');
});

it('for intlDate', function () {
var tmpl = '{{#intl formats=intl.formats locales="en-US"}}{{intlDate ' + timeStamp + ' "hm" timeZone="UTC"}}{{/intl}}',
ctx = {
intl: {
formats: {
date: {
hm: { hour: 'numeric', minute: 'numeric' }
}
}
}
},
d = new Date(timeStamp);
expect(Handlebars.compile(tmpl)(ctx)).to.equal("11:00 PM");
});

it('for intlMessage', function () {
var tmpl = '{{#intl formats=intl.formats locales="en-US"}}{{intlMessage MSG product=PRODUCT price=PRICE deadline=DEADLINE timeZone=TZ}}{{/intl}}',
ctx = {
MSG: '{product} cost {price, number, usd} (or {price, number, eur}) if ordered by {deadline, date, medium}',
intl: {
formats: {
number: {
eur: { style: 'currency', currency: 'EUR' },
usd: { style: 'currency', currency: 'USD' }
}
}
},
PRODUCT: 'oranges',
PRICE: 40000.004,
DEADLINE: timeStamp,
TZ: 'UTC'
};
expect(Handlebars.compile(tmpl)(ctx)).to.equal("oranges cost $40,000.00 (or €40,000.00) if ordered by Jan 23, 2014");
});
});
});