From c85af7c406a6df0f7b1e6315987ba72298f9c04f Mon Sep 17 00:00:00 2001 From: Nikola Kovacs Date: Thu, 11 May 2017 16:05:37 +0200 Subject: [PATCH] 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 --- src/common/runtime-bind.js | 5 +++-- src/common/runtime-key.js | 5 +++-- src/common/runtime-stringify.js | 12 ++++++++++++ test/compiler/cases/unit.js | 5 ++--- test/functional/unit/unit-formatter.js | 6 ++++++ 5 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 src/common/runtime-stringify.js diff --git a/src/common/runtime-bind.js b/src/common/runtime-bind.js index bfd85ddea..72468f1ed 100644 --- a/src/common/runtime-bind.js +++ b/src/common/runtime-bind.js @@ -1,11 +1,12 @@ define([ "./runtime-key", + "./runtime-stringify", "../util/function-name" -], function( runtimeKey, functionName ) { +], function( runtimeKey, runtimeStringify, functionName ) { return function( args, cldr, fn, runtimeArgs ) { - var argsStr = JSON.stringify( args ), + var argsStr = runtimeStringify( args ), fnName = functionName( fn ), locale = cldr.locale; diff --git a/src/common/runtime-key.js b/src/common/runtime-key.js index c1513aa38..904092ffc 100644 --- a/src/common/runtime-key.js +++ b/src/common/runtime-key.js @@ -1,10 +1,11 @@ define([ + "./runtime-stringify", "../util/string/hash" -], function( stringHash ) { +], function( runtimeStringify, stringHash ) { return function( fnName, locale, args, argsStr ) { var hash; - argsStr = argsStr || JSON.stringify( args ); + argsStr = argsStr || runtimeStringify( args ); hash = stringHash( fnName + locale + argsStr ); return hash > 0 ? "a" + hash : "b" + Math.abs( hash ); }; diff --git a/src/common/runtime-stringify.js b/src/common/runtime-stringify.js new file mode 100644 index 000000000..eac58f871 --- /dev/null +++ b/src/common/runtime-stringify.js @@ -0,0 +1,12 @@ +define([], function( ) { + +return function( args ) { + return JSON.stringify( args, function( key, value ) { + if ( typeof value === "function" ) { + return value.runtimeKey; // if undefined, the value will be filtered out. + } + return value; + } ); +}; + +}); diff --git a/test/compiler/cases/unit.js b/test/compiler/cases/unit.js index 39eab07ed..c7f42035c 100644 --- a/test/compiler/cases/unit.js +++ b/test/compiler/cases/unit.js @@ -28,10 +28,9 @@ module.exports = { { formatter: Globalize.unitFormatter( "hour", { numberFormatter: Globalize.numberFormatter( { minimumIntegerDigits: 1 } ) } ), args: [ 3 ] }, - // TODO: Fails because of https://github.com/globalizejs/globalize/issues/704 - /* { formatter: Globalize.unitFormatter( "hour", { + { formatter: Globalize.unitFormatter( "hour", { numberFormatter: Globalize.numberFormatter( { minimumIntegerDigits: 2 } ) - } ), args: [ 3 ] } */ + } ), args: [ 3 ] }, { formatter: en.unitFormatter( "day" ), args: [ 1 ] }, { formatter: en.unitFormatter( "day" ), args: [ 100 ] }, diff --git a/test/functional/unit/unit-formatter.js b/test/functional/unit/unit-formatter.js index c8d721a2d..0c15b35ab 100644 --- a/test/functional/unit/unit-formatter.js +++ b/test/functional/unit/unit-formatter.js @@ -129,4 +129,10 @@ QUnit.test( "should allow for runtime compilation", function( assert ) { ); }); +QUnit.test( "should generate different runtime key when using different numberFormatter", function( assert ) { + var formatter1 = Globalize.unitFormatter( "hour", { numberFormatter: Globalize.numberFormatter( { minimumIntegerDigits:1 } ) }); + var formatter2 = Globalize.unitFormatter( "hour", { numberFormatter: Globalize.numberFormatter( { minimumIntegerDigits:2 } ) }); + assert.notEqual( formatter1.runtimeKey, formatter2.runtimeKey ); +}); + });