From e859cd58ca2b738d10dd532f26093da4650f079d Mon Sep 17 00:00:00 2001 From: Phillip Wills Date: Sat, 15 Dec 2018 22:28:09 -0800 Subject: [PATCH] Unit: Fix exception using compound units with languages w/o "one" prop Fixes #849 --- src/unit/format.js | 5 ++-- test/unit.js | 3 ++- test/unit/unit/format-ja.js | 48 +++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 test/unit/unit/format-ja.js diff --git a/src/unit/format.js b/src/unit/format.js index f8ca83b10..81cf1e5e6 100644 --- a/src/unit/format.js +++ b/src/unit/format.js @@ -25,7 +25,7 @@ define([ */ return function( value, numberFormatter, pluralGenerator, unitProperties ) { var compoundUnitPattern = unitProperties.compoundUnitPattern, dividend, dividendProperties, - formattedValue, divisor, divisorProperties, message, pluralValue; + formattedValue, divisor, divisorProperties, message, pluralValue, oneProperty; unitProperties = unitProperties.unitProperties; formattedValue = numberFormatter( value ); @@ -35,9 +35,10 @@ return function( value, numberFormatter, pluralGenerator, unitProperties ) { if ( unitProperties instanceof Array ) { dividendProperties = unitProperties[ 0 ]; divisorProperties = unitProperties[ 1 ]; + oneProperty = divisorProperties.hasOwnProperty( "one" ) ? "one" : "other"; dividend = formatMessage( dividendProperties[ pluralValue ], [ formattedValue ] ); - divisor = formatMessage( divisorProperties.one, [ "" ] ).trim(); + divisor = formatMessage( divisorProperties[oneProperty], [ "" ] ).trim(); return formatMessage( compoundUnitPattern, [ dividend, divisor ] ); } diff --git a/test/unit.js b/test/unit.js index d9d1651ca..0250cec65 100644 --- a/test/unit.js +++ b/test/unit.js @@ -48,7 +48,8 @@ require([ /* unit */ "./unit/unit/get", - "./unit/unit/format" + "./unit/unit/format", + "./unit/unit/format-ja" ], function() { QUnit.start(); diff --git a/test/unit/unit/format-ja.js b/test/unit/unit/format-ja.js new file mode 100644 index 000000000..648c99758 --- /dev/null +++ b/test/unit/unit/format-ja.js @@ -0,0 +1,48 @@ +define([ + "cldr", + "src/core", + "src/unit/format", + "src/unit/properties", + "json!cldr-data/main/ja/units.json", + "json!cldr-data/supplemental/likelySubtags.json" +], function( Cldr, Globalize, formatUnit, unitProperties, jaUnits, likelySubtags ) { + +var cldr, globalize; + +Cldr.load( jaUnits ); +Cldr.load( likelySubtags ); + +globalize = new Globalize( "ja" ); +cldr = globalize.cldr; + +QUnit.module( "Unit Format - Compound Form with no one property in cldr" ); + +function oneOrOtherPluralGenerator() { + return "other"; +} + +function stubNumberFormatter( number ) { + return number.toString(); +} + +QUnit.assert.unitFormatJa = function ( value, unit, options, expected ) { + var unitProps = unitProperties( unit, options.form, cldr ); + + this.equal( + formatUnit( value, options.numberFormatter || stubNumberFormatter, oneOrOtherPluralGenerator, unitProps ), + expected + ); +}; + +QUnit.test( "Compound form (without precomputed)", function ( assert ) { + assert.unitFormatJa( 1, "length-foot-per-second", { form: "long" }, "1 フィート毎秒" ); + assert.unitFormatJa( 100, "length-foot-per-second", { form: "long" }, "100 フィート毎秒" ); + assert.unitFormatJa( 1, "megabyte-per-second", { form: "narrow" }, "1MB/秒" ); + assert.unitFormatJa( 100, "megabyte-per-second", { form: "narrow" }, "100MB/秒" ); + + assert.unitFormatJa( 1.2345678910, "megabyte-per-second", + { form: "narrow", numberFormatter: function (number) { return number.toFixed(1); }}, + "1.2MB/秒" ); +}); + +});