Skip to content

Commit

Permalink
Unit: Fix exception using compound units with languages w/o "one" prop
Browse files Browse the repository at this point in the history
Fixes #849
  • Loading branch information
willsp committed Dec 16, 2018
1 parent c56513b commit e859cd5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/unit/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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 ] );
}
Expand Down
3 changes: 2 additions & 1 deletion test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ require([

/* unit */
"./unit/unit/get",
"./unit/unit/format"
"./unit/unit/format",
"./unit/unit/format-ja"

], function() {
QUnit.start();
Expand Down
48 changes: 48 additions & 0 deletions test/unit/unit/format-ja.js
Original file line number Diff line number Diff line change
@@ -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/秒" );
});

});

0 comments on commit e859cd5

Please sign in to comment.