Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
54 changes: 39 additions & 15 deletions test/unit/unit/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
};
Expand Down Expand Up @@ -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" );
});

});