Skip to content

Commit b0bc634

Browse files
committed
Add "localizePrice" helper
1 parent 395533e commit b0bc634

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

helpers/localizePrice.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
const common = require('./lib/common.js');
3+
4+
const factory = globals => {
5+
return function(price, locale) {
6+
7+
if (!common.isObject(price) || !common.isString(price.currency) || isNaN(price.value)) {
8+
// Return empty string if this does not appear to be a price object
9+
return ''
10+
}
11+
12+
if (!common.isString(locale) || locale.length < 2) {
13+
// Valid browser language strings are at least two characters
14+
// https://www.metamodpro.com/browser-language-codes
15+
// If provided locale is less than two characters (or not a string),
16+
// Default to 'en-US'
17+
locale = 'en-US';
18+
}
19+
20+
return new Intl.NumberFormat(
21+
locale, { style: 'currency', currency: price.currency}
22+
).format(price.value);
23+
24+
};
25+
};
26+
27+
module.exports = [{
28+
name: 'localizePrice',
29+
factory: factory,
30+
}];

spec/helpers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ describe('helper registration', () => {
3939
'lang',
4040
'langJson',
4141
'limit',
42+
'localizePrice',
4243
'money',
4344
'nl2br',
4445
'or',

0 commit comments

Comments
 (0)