Skip to content

Commit 0d31631

Browse files
nkovacsrxaviers
authored andcommitted
Runtime: Fix unitFormatter with numberFormatter (1/2)
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 Closes #719
1 parent 7997b6c commit 0d31631

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
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+
});

test/compiler/cases/unit.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ module.exports = {
2828
{ formatter: Globalize.unitFormatter( "hour", {
2929
numberFormatter: Globalize.numberFormatter( { minimumIntegerDigits: 1 } )
3030
} ), args: [ 3 ] },
31-
// TODO: Fails because of https://github.com/globalizejs/globalize/issues/704
32-
/* { formatter: Globalize.unitFormatter( "hour", {
31+
{ formatter: Globalize.unitFormatter( "hour", {
3332
numberFormatter: Globalize.numberFormatter( { minimumIntegerDigits: 2 } )
34-
} ), args: [ 3 ] } */
33+
} ), args: [ 3 ] },
3534

3635
{ formatter: en.unitFormatter( "day" ), args: [ 1 ] },
3736
{ formatter: en.unitFormatter( "day" ), args: [ 100 ] },

test/functional/unit/unit-formatter.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,9 @@ QUnit.test( "should accept custom number formatter", function( assert ) {
9595
assert.equal( deUnitFormatter( 3.14159 ), "3,14 Meter" );
9696
});
9797

98+
QUnit.test( "should generate different runtime key when using different numberFormatter", function( assert ) {
99+
var formatter1 = Globalize.unitFormatter( "hour", { numberFormatter: Globalize.numberFormatter( { minimumIntegerDigits:1 } ) });
100+
var formatter2 = Globalize.unitFormatter( "hour", { numberFormatter: Globalize.numberFormatter( { minimumIntegerDigits:2 } ) });
101+
assert.notEqual( formatter1.runtimeKey, formatter2.runtimeKey );
102+
});
98103
});

0 commit comments

Comments
 (0)