Skip to content

Commit d8f9ece

Browse files
committed
Runtime: Fix unitFormatter with numberFormatter
JSON.stringify omits functions, so the generated runtimeKey did not depend on the value of the numberFormatter option, causing different unitFormatters to have an identical runtimeKey. Fixes #704
1 parent 04ee130 commit d8f9ece

File tree

12 files changed

+50
-14
lines changed

12 files changed

+50
-14
lines changed

src/common/runtime-bind.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
define([
22
"./runtime-key",
3+
"./runtime-stringify",
34
"../util/function-name"
4-
], function( runtimeKey, functionName ) {
5+
], function( runtimeKey, runtimeStringify, functionName ) {
56

67
return function( args, cldr, fn, runtimeArgs ) {
78

8-
var argsStr = JSON.stringify( args ),
9+
var argsStr = runtimeStringify( args ),
910
fnName = functionName( fn ),
1011
locale = cldr.locale;
1112

src/common/runtime-key.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
define([
2+
"./runtime-stringify",
23
"../util/string/hash"
3-
], function( stringHash ) {
4+
], function( runtimeStringify, stringHash ) {
45

56
return function( fnName, locale, args, argsStr ) {
67
var hash;
7-
argsStr = argsStr || JSON.stringify( args );
8+
argsStr = argsStr || runtimeStringify( args );
89
hash = stringHash( fnName + locale + argsStr );
910
return hash > 0 ? "a" + hash : "b" + Math.abs( hash );
1011
};

src/common/runtime-stringify.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
define([], function( ) {
2+
3+
return function( args ) {
4+
return JSON.stringify( args, function( key, value ) {
5+
if ( typeof value === "function" ) {
6+
return value.runtimeKey; // if undefined, the value will be filtered out.
7+
}
8+
return value;
9+
} );
10+
};
11+
12+
});

src/core-runtime.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ Globalize.locale = function( locale ) {
3030
return this._locale;
3131
};
3232

33+
Globalize._loadRuntimeFn = function( key ) {
34+
var fn = Globalize[ key ];
35+
if ( typeof fn === "function" ) {
36+
fn.runtimeKey = key;
37+
}
38+
return fn;
39+
};
40+
3341
Globalize._createError = createError;
3442
Globalize._formatMessage = formatMessage;
3543
Globalize._regexpEscape = regexpEscape;

src/currency-runtime.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ Globalize._currencyNameFormat = currencyNameFormat;
1616
Globalize.currencyFormatter =
1717
Globalize.prototype.currencyFormatter = function( currency, options ) {
1818
options = options || {};
19-
return Globalize[ runtimeKey( "currencyFormatter", this._locale, [ currency, options ] ) ];
19+
return Globalize._loadRuntimeFn(
20+
runtimeKey( "currencyFormatter", this._locale, [ currency, options ] )
21+
);
2022
};
2123

2224
Globalize.formatCurrency =

src/date-runtime.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ Globalize.prototype.dateFormatter = function( options ) {
3636
Globalize.dateToPartsFormatter =
3737
Globalize.prototype.dateToPartsFormatter = function( options ) {
3838
options = options || { skeleton: "yMd" };
39-
return Globalize[ runtimeKey( "dateToPartsFormatter", this._locale, [ options ] ) ];
39+
return Globalize._loadRuntimeFn(
40+
runtimeKey( "dateToPartsFormatter", this._locale, [ options ] )
41+
);
4042
};
4143

4244
Globalize.dateParser =
4345
Globalize.prototype.dateParser = function( options ) {
4446
options = options || { skeleton: "yMd" };
45-
return Globalize[ runtimeKey( "dateParser", this._locale, [ options ] ) ];
47+
return Globalize._loadRuntimeFn( runtimeKey( "dateParser", this._locale, [ options ] ) );
4648
};
4749

4850
Globalize.formatDate =

src/message-runtime.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ Globalize._validateParameterTypeMessageVariables = validateParameterTypeMessageV
1111

1212
Globalize.messageFormatter =
1313
Globalize.prototype.messageFormatter = function( /* path */ ) {
14-
return Globalize[
14+
return Globalize._loadRuntimeFn(
1515
runtimeKey( "messageFormatter", this._locale, [].slice.call( arguments, 0 ) )
16-
];
16+
);
1717
};
1818

1919
Globalize.formatMessage =

src/number-runtime.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ Globalize._validateParameterTypeString = validateParameterTypeString;
2929
Globalize.numberFormatter =
3030
Globalize.prototype.numberFormatter = function( options ) {
3131
options = options || {};
32-
return Globalize[ runtimeKey( "numberFormatter", this._locale, [ options ] ) ];
32+
return Globalize._loadRuntimeFn( runtimeKey( "numberFormatter", this._locale, [ options ] ) );
3333
};
3434

3535
Globalize.numberParser =
3636
Globalize.prototype.numberParser = function( options ) {
3737
options = options || {};
38-
return Globalize[ runtimeKey( "numberParser", this._locale, [ options ] ) ];
38+
return Globalize._loadRuntimeFn( runtimeKey( "numberParser", this._locale, [ options ] ) );
3939
};
4040

4141
Globalize.formatNumber =

src/plural-runtime.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Globalize.prototype.plural = function( value, options ) {
2020
Globalize.pluralGenerator =
2121
Globalize.prototype.pluralGenerator = function( options ) {
2222
options = options || {};
23-
return Globalize[ runtimeKey( "pluralGenerator", this._locale, [ options ] ) ];
23+
return Globalize._loadRuntimeFn( runtimeKey( "pluralGenerator", this._locale, [ options ] ) );
2424
};
2525

2626
return Globalize;

src/relative-time-runtime.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ Globalize.prototype.formatRelativeTime = function( value, unit, options ) {
2323
Globalize.relativeTimeFormatter =
2424
Globalize.prototype.relativeTimeFormatter = function( unit, options ) {
2525
options = options || {};
26-
return Globalize[ runtimeKey( "relativeTimeFormatter", this._locale, [ unit, options ] ) ];
26+
return Globalize._loadRuntimeFn(
27+
runtimeKey( "relativeTimeFormatter", this._locale, [ unit, options ] )
28+
);
2729
};
2830

2931
return Globalize;

0 commit comments

Comments
 (0)