File tree Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Expand file tree Collapse file tree 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change
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 )
8
+ || isNaN ( price . value ) || ! common . isString ( price . formatted ) ) {
9
+ // Return empty string if this does not appear to be a price object
10
+ return ''
11
+ }
12
+
13
+ if ( ! common . isString ( locale ) || locale . length < 2 ) {
14
+ // Valid browser language strings are at least two characters
15
+ // https://www.metamodpro.com/browser-language-codes
16
+ // If provided locale is less than two characters (or not a string),
17
+ // return the normal formatted price
18
+ return price . formatted ;
19
+ }
20
+
21
+ // Try to format the price to the provided locale,
22
+ // but if anything goes wrong,
23
+ // just return the usual price
24
+ try {
25
+ return new Intl . NumberFormat (
26
+ locale , { style : 'currency' , currency : price . currency }
27
+ ) . format ( price . value ) ;
28
+ }
29
+ catch {
30
+ return price . formatted ;
31
+ }
32
+ } ;
33
+ } ;
34
+
35
+ module . exports = [ {
36
+ name : 'localizePrice' ,
37
+ factory : factory ,
38
+ } ] ;
Original file line number Diff line number Diff line change @@ -39,6 +39,7 @@ describe('helper registration', () => {
39
39
'lang' ,
40
40
'langJson' ,
41
41
'limit' ,
42
+ 'localizePrice' ,
42
43
'money' ,
43
44
'nl2br' ,
44
45
'or' ,
You can’t perform that action at this time.
0 commit comments