Skip to content

Commit

Permalink
Unit: pass in pluralGenerator, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alunny committed Sep 25, 2015
1 parent c953d17 commit cdb605a
Show file tree
Hide file tree
Showing 3 changed files with 278 additions and 70 deletions.
12 changes: 9 additions & 3 deletions src/unit.js
@@ -1,6 +1,8 @@
define([
"./core",
"./unit/format"
"./unit/format",

"./plural"
], function( Globalize, unitFormat ) {

/**
Expand All @@ -15,7 +17,9 @@ define([
*
* Format units such as seconds, minutes, days, weeks, etc.
*/
Globalize.formatUnit = function( value, unit, options ) {
Globalize.prototype.formatUnit = function( value, unit, options ) {
var pluralGenerator;

if ( typeof value !== "number" ) {
throw new Error( "Value is not a number" );
}
Expand All @@ -24,7 +28,9 @@ Globalize.formatUnit = function( value, unit, options ) {
throw new Error( "Missing unit" );
}

return unitFormat( value, unit, options, this.cldr, this );
pluralGenerator = this.pluralGenerator();

return unitFormat( value, unit, options, pluralGenerator, this.cldr, this );
};

return Globalize;
Expand Down
8 changes: 5 additions & 3 deletions src/unit/format.js
Expand Up @@ -25,8 +25,8 @@ define([
* Duration Unit (for composed time unit durations) is not implemented.
* http://www.unicode.org/reports/tr35/tr35-35/tr35-general.html#durationUnit
*/
return function( value, unit, options, cldr, globalize ) {
var dividend, divisor, form, ret;
return function( value, unit, options, pluralGenerator, cldr, globalize ) {
var dividend, divisor, form, message, ret;
options = options || {};
form = options.form || "long";

Expand All @@ -53,7 +53,9 @@ return function( value, unit, options, cldr, globalize ) {
[ dividend, divisor ] );
}

return globalize.formatPlural( value, ret );
message = ret[ pluralGenerator( value ) ];

return formatMessage( message, [ value ] );
};

});
328 changes: 264 additions & 64 deletions test/unit/unit/format.js
Expand Up @@ -16,80 +16,280 @@ cldr = globalize.cldr;

QUnit.module( "Unit Format" );

function oneOrOtherPluralGenerator( plural ) {
if ( plural === 1 ) {
return "one";
} else {
return "other";
}
}

QUnit.test( "Default form (long)", function( assert ) {
assert.equal( formatUnit( 1, "millisecond", {}, cldr, globalize ), "1 millisecond" );
assert.equal( formatUnit( 2, "millisecond", {}, cldr, globalize ), "2 milliseconds" );
assert.equal( formatUnit( 1, "second", {}, cldr, globalize ), "1 second" );
assert.equal( formatUnit( 2, "second", {}, cldr, globalize ), "2 seconds" );
assert.equal( formatUnit( 1, "minute", {}, cldr, globalize ), "1 minute" );
assert.equal( formatUnit( 2, "minute", {}, cldr, globalize ), "2 minutes" );
assert.equal( formatUnit( 1, "hour", {}, cldr, globalize ), "1 hour" );
assert.equal( formatUnit( 2, "hour", {}, cldr, globalize ), "2 hours" );
assert.equal( formatUnit( 1, "day", {}, cldr, globalize ), "1 day" );
assert.equal( formatUnit( 2, "day", {}, cldr, globalize ), "2 days" );
assert.equal( formatUnit( 1, "week", {}, cldr, globalize ), "1 week" );
assert.equal( formatUnit( 2, "week", {}, cldr, globalize ), "2 weeks" );
assert.equal( formatUnit( 1, "month", {}, cldr, globalize ), "1 month" );
assert.equal( formatUnit( 2, "month", {}, cldr, globalize ), "2 months" );
assert.equal( formatUnit( 1, "year", {}, cldr, globalize ), "1 year" );
assert.equal( formatUnit( 2, "year", {}, cldr, globalize ), "2 years" );
assert.equal(
formatUnit( 1, "millisecond", {}, oneOrOtherPluralGenerator, cldr, globalize ),
"1 millisecond"
);
assert.equal(
formatUnit( 2, "millisecond", {}, oneOrOtherPluralGenerator, cldr, globalize ),
"2 milliseconds"
);
assert.equal(
formatUnit( 1, "second", {}, oneOrOtherPluralGenerator, cldr, globalize ),
"1 second"
);
assert.equal(
formatUnit( 2, "second", {}, oneOrOtherPluralGenerator, cldr, globalize ),
"2 seconds"
);
assert.equal(
formatUnit( 1, "minute", {}, oneOrOtherPluralGenerator, cldr, globalize ),
"1 minute"
);
assert.equal(
formatUnit( 2, "minute", {}, oneOrOtherPluralGenerator, cldr, globalize ),
"2 minutes"
);
assert.equal(
formatUnit( 1, "hour", {}, oneOrOtherPluralGenerator, cldr, globalize ),
"1 hour"
);
assert.equal(
formatUnit( 2, "hour", {}, oneOrOtherPluralGenerator, cldr, globalize ),
"2 hours"
);
assert.equal(
formatUnit( 1, "day", {}, oneOrOtherPluralGenerator, cldr, globalize ),
"1 day"
);
assert.equal(
formatUnit( 2, "day", {}, oneOrOtherPluralGenerator, cldr, globalize ),
"2 days"
);
assert.equal(
formatUnit( 1, "week", {}, oneOrOtherPluralGenerator, cldr, globalize ),
"1 week"
);
assert.equal(
formatUnit( 2, "week", {}, oneOrOtherPluralGenerator, cldr, globalize ),
"2 weeks"
);
assert.equal(
formatUnit( 1, "month", {}, oneOrOtherPluralGenerator, cldr, globalize ),
"1 month"
);
assert.equal(
formatUnit( 2, "month", {}, oneOrOtherPluralGenerator, cldr, globalize ),
"2 months"
);
assert.equal(
formatUnit( 1, "year", {}, oneOrOtherPluralGenerator, cldr, globalize ),
"1 year"
);
assert.equal(
formatUnit( 2, "year", {}, oneOrOtherPluralGenerator, cldr, globalize ),
"2 years"
);
});

QUnit.test( "Long form", function( assert ) {
assert.equal( formatUnit( 1, "millisecond", { form: "long" }, cldr, globalize ), "1 millisecond" );
assert.equal( formatUnit( 2, "millisecond", { form: "long" }, cldr, globalize ), "2 milliseconds" );
assert.equal( formatUnit( 1, "second", { form: "long" }, cldr, globalize ), "1 second" );
assert.equal( formatUnit( 2, "second", { form: "long" }, cldr, globalize ), "2 seconds" );
assert.equal( formatUnit( 1, "minute", { form: "long" }, cldr, globalize ), "1 minute" );
assert.equal( formatUnit( 2, "minute", { form: "long" }, cldr, globalize ), "2 minutes" );
assert.equal( formatUnit( 1, "hour", { form: "long" }, cldr, globalize ), "1 hour" );
assert.equal( formatUnit( 2, "hour", { form: "long" }, cldr, globalize ), "2 hours" );
assert.equal( formatUnit( 1, "day", { form: "long" }, cldr, globalize ), "1 day" );
assert.equal( formatUnit( 2, "day", { form: "long" }, cldr, globalize ), "2 days" );
assert.equal( formatUnit( 1, "week", { form: "long" }, cldr, globalize ), "1 week" );
assert.equal( formatUnit( 2, "week", { form: "long" }, cldr, globalize ), "2 weeks" );
assert.equal( formatUnit( 1, "month", { form: "long" }, cldr, globalize ), "1 month" );
assert.equal( formatUnit( 2, "month", { form: "long" }, cldr, globalize ), "2 months" );
assert.equal( formatUnit( 1, "year", { form: "long" }, cldr, globalize ), "1 year" );
assert.equal( formatUnit( 2, "year", { form: "long" }, cldr, globalize ), "2 years" );
assert.equal(
formatUnit( 1, "millisecond", { form: "long" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1 millisecond"
);
assert.equal(
formatUnit( 2, "millisecond", { form: "long" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2 milliseconds"
);
assert.equal(
formatUnit( 1, "second", { form: "long" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1 second"
);
assert.equal(
formatUnit( 2, "second", { form: "long" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2 seconds"
);
assert.equal(
formatUnit( 1, "minute", { form: "long" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1 minute"
);
assert.equal(
formatUnit( 2, "minute", { form: "long" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2 minutes"
);
assert.equal(
formatUnit( 1, "hour", { form: "long" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1 hour"
);
assert.equal(
formatUnit( 2, "hour", { form: "long" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2 hours"
);
assert.equal(
formatUnit( 1, "day", { form: "long" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1 day"
);
assert.equal(
formatUnit( 2, "day", { form: "long" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2 days"
);
assert.equal(
formatUnit( 1, "week", { form: "long" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1 week"
);
assert.equal(
formatUnit( 2, "week", { form: "long" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2 weeks"
);
assert.equal(
formatUnit( 1, "month", { form: "long" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1 month"
);
assert.equal(
formatUnit( 2, "month", { form: "long" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2 months"
);
assert.equal(
formatUnit( 1, "year", { form: "long" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1 year"
);
assert.equal(
formatUnit( 2, "year", { form: "long" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2 years"
);
});

QUnit.test( "Short form", function( assert ) {
assert.equal( formatUnit( 1, "millisecond", { form: "short" }, cldr, globalize ), "1 ms" );
assert.equal( formatUnit( 2, "millisecond", { form: "short" }, cldr, globalize ), "2 ms" );
assert.equal( formatUnit( 1, "second", { form: "short" }, cldr, globalize ), "1 sec" );
assert.equal( formatUnit( 2, "second", { form: "short" }, cldr, globalize ), "2 secs" );
assert.equal( formatUnit( 1, "minute", { form: "short" }, cldr, globalize ), "1 min" );
assert.equal( formatUnit( 2, "minute", { form: "short" }, cldr, globalize ), "2 mins" );
assert.equal( formatUnit( 1, "hour", { form: "short" }, cldr, globalize ), "1 hr" );
assert.equal( formatUnit( 2, "hour", { form: "short" }, cldr, globalize ), "2 hrs" );
assert.equal( formatUnit( 1, "day", { form: "short" }, cldr, globalize ), "1 day" );
assert.equal( formatUnit( 2, "day", { form: "short" }, cldr, globalize ), "2 days" );
assert.equal( formatUnit( 1, "week", { form: "short" }, cldr, globalize ), "1 wk" );
assert.equal( formatUnit( 2, "week", { form: "short" }, cldr, globalize ), "2 wks" );
assert.equal( formatUnit( 1, "month", { form: "short" }, cldr, globalize ), "1 mth" );
assert.equal( formatUnit( 2, "month", { form: "short" }, cldr, globalize ), "2 mths" );
assert.equal( formatUnit( 1, "year", { form: "short" }, cldr, globalize ), "1 yr" );
assert.equal( formatUnit( 2, "year", { form: "short" }, cldr, globalize ), "2 yrs" );
assert.equal(
formatUnit( 1, "millisecond", { form: "short" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1 ms"
);
assert.equal(
formatUnit( 2, "millisecond", { form: "short" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2 ms"
);
assert.equal(
formatUnit( 1, "second", { form: "short" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1 sec"
);
assert.equal(
formatUnit( 2, "second", { form: "short" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2 sec"
);
assert.equal(
formatUnit( 1, "minute", { form: "short" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1 min"
);
assert.equal(
formatUnit( 2, "minute", { form: "short" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2 min"
);
assert.equal(
formatUnit( 1, "hour", { form: "short" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1 hr"
);
assert.equal(
formatUnit( 2, "hour", { form: "short" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2 hr"
);
assert.equal(
formatUnit( 1, "day", { form: "short" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1 day"
);
assert.equal(
formatUnit( 2, "day", { form: "short" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2 days"
);
assert.equal(
formatUnit( 1, "week", { form: "short" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1 wk"
);
assert.equal(
formatUnit( 2, "week", { form: "short" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2 wks"
);
assert.equal(
formatUnit( 1, "month", { form: "short" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1 mth"
);
assert.equal(
formatUnit( 2, "month", { form: "short" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2 mths"
);
assert.equal(
formatUnit( 1, "year", { form: "short" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1 yr"
);
assert.equal(
formatUnit( 2, "year", { form: "short" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2 yrs"
);
});

QUnit.test( "Narrow form", function( assert ) {
assert.equal( formatUnit( 1, "millisecond", { form: "narrow" }, cldr, globalize ), "1ms" );
assert.equal( formatUnit( 2, "millisecond", { form: "narrow" }, cldr, globalize ), "2ms" );
assert.equal( formatUnit( 1, "second", { form: "narrow" }, cldr, globalize ), "1s" );
assert.equal( formatUnit( 2, "second", { form: "narrow" }, cldr, globalize ), "2s" );
assert.equal( formatUnit( 1, "minute", { form: "narrow" }, cldr, globalize ), "1m" );
assert.equal( formatUnit( 2, "minute", { form: "narrow" }, cldr, globalize ), "2m" );
assert.equal( formatUnit( 1, "hour", { form: "narrow" }, cldr, globalize ), "1h" );
assert.equal( formatUnit( 2, "hour", { form: "narrow" }, cldr, globalize ), "2h" );
assert.equal( formatUnit( 1, "day", { form: "narrow" }, cldr, globalize ), "1d" );
assert.equal( formatUnit( 2, "day", { form: "narrow" }, cldr, globalize ), "2d" );
assert.equal( formatUnit( 1, "week", { form: "narrow" }, cldr, globalize ), "1w" );
assert.equal( formatUnit( 2, "week", { form: "narrow" }, cldr, globalize ), "2w" );
assert.equal( formatUnit( 1, "month", { form: "narrow" }, cldr, globalize ), "1m" );
assert.equal( formatUnit( 2, "month", { form: "narrow" }, cldr, globalize ), "2m" );
assert.equal( formatUnit( 1, "year", { form: "narrow" }, cldr, globalize ), "1y" );
assert.equal( formatUnit( 2, "year", { form: "narrow" }, cldr, globalize ), "2y" );
assert.equal(
formatUnit( 1, "millisecond", { form: "narrow" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1ms"
);
assert.equal(
formatUnit( 2, "millisecond", { form: "narrow" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2ms"
);
assert.equal(
formatUnit( 1, "second", { form: "narrow" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1s"
);
assert.equal(
formatUnit( 2, "second", { form: "narrow" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2s"
);
assert.equal(
formatUnit( 1, "minute", { form: "narrow" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1m"
);
assert.equal(
formatUnit( 2, "minute", { form: "narrow" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2m"
);
assert.equal(
formatUnit( 1, "hour", { form: "narrow" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1h"
);
assert.equal(
formatUnit( 2, "hour", { form: "narrow" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2h"
);
assert.equal(
formatUnit( 1, "day", { form: "narrow" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1d"
);
assert.equal(
formatUnit( 2, "day", { form: "narrow" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2d"
);
assert.equal(
formatUnit( 1, "week", { form: "narrow" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1w"
);
assert.equal(
formatUnit( 2, "week", { form: "narrow" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2w"
);
assert.equal(
formatUnit( 1, "month", { form: "narrow" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1m"
);
assert.equal(
formatUnit( 2, "month", { form: "narrow" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2m"
);
assert.equal(
formatUnit( 1, "year", { form: "narrow" }, oneOrOtherPluralGenerator, cldr, globalize ),
"1y"
);
assert.equal(
formatUnit( 2, "year", { form: "narrow" }, oneOrOtherPluralGenerator, cldr, globalize ),
"2y"
);
});

});

0 comments on commit cdb605a

Please sign in to comment.