Permalink
Browse files

Unit: Fix exception using compound units with languages w/o "one" prop

Fixes #849
  • Loading branch information...
willsp committed Dec 16, 2018
1 parent c56513b commit 8f4f2bffed00231aa2f7f4f46c3a4687ace46ab6
Showing with 42 additions and 17 deletions.
  1. +3 −2 src/unit/format.js
  2. +39 −15 test/unit/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 ] );
}
@@ -4,36 +4,49 @@ define([
"src/unit/format",
"src/unit/properties",
"json!cldr-data/main/en/units.json",
"json!cldr-data/main/ja/units.json",
"json!cldr-data/supplemental/likelySubtags.json"
], function( Cldr, Globalize, formatUnit, unitProperties, enUnits, likelySubtags ) {
], function( Cldr, Globalize, formatUnit, unitProperties, enUnits, jaUnits, likelySubtags ) {

var cldr, globalize;

Cldr.load( enUnits );
Cldr.load( likelySubtags );

globalize = new Globalize( "en" );
cldr = globalize.cldr;
var cldr, globalize, units, pluralGenerator;

QUnit.module( "Unit Format" );

function oneOrOtherPluralGenerator( plural ) {
if ( plural === 1 ) {
return "one";
} else {
units = {
en: enUnits,
ja: jaUnits
};

pluralGenerator = {
en: function oneOrOtherPluralGenerator( plural ) {
if ( plural === 1 ) {
return "one";
} else {
return "other";
}
},
ja: function otherPluralGenerator() {
return "other";
}
}
};

function stubNumberFormatter( number ) {
return number.toString();
}

QUnit.assert.unitFormat = function ( value, unit, options, expected ) {
QUnit.assert.unitFormat = function ( value, unit, options, expected, language ) {
language = language || "en";

Cldr.load( units[ language ] );
Cldr.load( likelySubtags );

globalize = new Globalize( language );
cldr = globalize.cldr;

var unitProps = unitProperties( unit, options.form, cldr );

this.equal(
formatUnit( value, options.numberFormatter || stubNumberFormatter, oneOrOtherPluralGenerator, unitProps ),
formatUnit( value, options.numberFormatter || stubNumberFormatter, pluralGenerator[ language ], unitProps ),
expected
);
};
@@ -132,4 +145,15 @@ QUnit.test( "Compound form (narrow)", function ( assert ) {
assert.unitFormat( 100, "consumption-mile-per-gallon", { form: "narrow" }, "100mpg" );
});

QUnit.test( "Compound form (without precomputed) in language without 'one' unit", function ( assert ) {
assert.unitFormat( 1, "length-foot-per-second", { form: "long" }, "1 フィート毎秒", "ja" );
assert.unitFormat( 100, "length-foot-per-second", { form: "long" }, "100 フィート毎秒", "ja" );
assert.unitFormat( 1, "megabyte-per-second", { form: "narrow" }, "1MB/秒", "ja" );
assert.unitFormat( 100, "megabyte-per-second", { form: "narrow" }, "100MB/秒", "ja" );

assert.unitFormat( 1.2345678910, "megabyte-per-second",
{ form: "narrow", numberFormatter: function (number) { return number.toFixed(1); }},
"1.2MB/秒", "ja" );
});

});

0 comments on commit 8f4f2bf

Please sign in to comment.