-
Notifications
You must be signed in to change notification settings - Fork 600
/
Copy pathformat.js
51 lines (43 loc) · 1.7 KB
/
format.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
define([
"../common/format-message"
], function( formatMessage ) {
/**
* format( value, numberFormatter, pluralGenerator, unitProperies )
*
* @value [Number]
*
* @numberFormatter [Object]: A numberFormatter from Globalize.numberFormatter.
*
* @pluralGenerator [Object]: A pluralGenerator from Globalize.pluralGenerator.
*
* @unitProperies [Object]: localized unit data from cldr.
*
* Format units such as seconds, minutes, days, weeks, etc.
*
* OBS:
*
* Unit Sequences are not implemented.
* http://www.unicode.org/reports/tr35/tr35-35/tr35-general.html#Unit_Sequences
*
* 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, numberFormatter, pluralGenerator, unitProperties ) {
var compoundUnitPattern = unitProperties.compoundUnitPattern, dividend, dividendProperties,
formattedValue, divisor, divisorProperties, message, pluralValue, oneProperty;
unitProperties = unitProperties.unitProperties;
formattedValue = numberFormatter( value );
pluralValue = pluralGenerator( value );
// computed compound unit, eg. "megabyte-per-second".
if ( unitProperties instanceof Array ) {
dividendProperties = unitProperties[ 0 ];
divisorProperties = unitProperties[ 1 ];
oneProperty = divisorProperties.hasOwnProperty( "one" ) ? "one" : "other";
dividend = formatMessage( dividendProperties[ pluralValue ], [ formattedValue ] );
divisor = formatMessage( divisorProperties[ oneProperty ], [ "" ] ).trim();
return formatMessage( compoundUnitPattern, [ dividend, divisor ] );
}
message = unitProperties[ pluralValue ];
return formatMessage( message, [ formattedValue ] );
};
});