-
Notifications
You must be signed in to change notification settings - Fork 603
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
Add unit formatting module #512
Changes from all commits
a6c40d6
c953d17
cdb605a
604cc10
4c019df
cc5e2d9
be264ee
3825340
088e0b4
f81ff92
9e9ca03
6ced895
d5f900b
e72be91
1b11473
88782a8
0d35408
63138a8
70e5e28
0a6871c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
## .unitFormatter( unit, options ) ➜ function( value ) | ||
|
||
Returns a function that formats a unit according to the given unit, options, and the | ||
default/instance locale. | ||
|
||
The returned function is invoked with one argument: the number `value` to | ||
be formatted. | ||
|
||
### Parameters | ||
|
||
**unit** | ||
|
||
String value indicating the unit to be formatted. eg. "day", "week", "month", etc. | ||
Could also be a compound unit, eg. "mile-per-hour" or "mile/hour" | ||
|
||
**options** | ||
|
||
- form: [String] eg. "long", "short" or "narrow". | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are no options for formatting the number itself? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shoot. Good point. I would add a |
||
|
||
- numberFormatter: [Object] `Globalize.numberFormatter` instance. Defaults to the | ||
default numberFormatter for the current locale. | ||
|
||
**value** | ||
|
||
The number to be formatted. | ||
|
||
### Example | ||
|
||
Prior to using any unit methods, you must load `cldr/main/{locale}/units.json` and the | ||
CLDR content required by the plural module. Read [CLDR content][] if you need | ||
more information. | ||
|
||
[CLDR content]: ../../../README.md#2-cldr-content | ||
|
||
You can use the static method `Globalize.unitFormatter()`, which uses the default | ||
locale. | ||
|
||
```javascript | ||
var customNumberFormatter, formatter; | ||
|
||
Globalize.locale( "en" ); | ||
formatter = Globalize.unitFormatter( "month", { form: "long" } ); | ||
|
||
formatter( 1 ); | ||
// > "1 month" | ||
|
||
formatter( 3 ); | ||
// > "3 months" | ||
|
||
formatter( 3000 ); | ||
// > "3,000 months" | ||
``` | ||
|
||
You can pass a custom number formatter to format the number of units. | ||
|
||
```javascript | ||
var customNumberFormatter, formatter; | ||
|
||
Globalize.locale( "en" ); | ||
customNumberFormatter = Globalize.numberFormatter({ useGrouping = false }) | ||
formatter = Globalize.unitFormatter( "mile-per-hour", { | ||
form: "narrow", numberFormatter: customNumberFormatter | ||
} ); | ||
|
||
formatter(5000) | ||
// > "5000mph" | ||
``` | ||
|
||
You can use the instance method `.unitFormatter()`, which uses the instance locale. | ||
|
||
```javascript | ||
var globalize = new Globalize( "en" ), | ||
formatter = globalize.unitFormatter( "mile-per-hour", { form: "narrow" } ); | ||
|
||
formatter( 10 ); | ||
// > "10mph" | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Globalize Runtime v@VERSION | ||
* | ||
* http://github.com/jquery/globalize | ||
* | ||
* Copyright jQuery Foundation and other contributors | ||
* Released under the MIT license | ||
* http://jquery.org/license | ||
* | ||
* Date: @DATE | ||
*/ | ||
/*! | ||
* Globalize Runtime v@VERSION @DATE Released under the MIT license | ||
* http://git.io/TrdQbw | ||
*/ | ||
(function( root, factory ) { | ||
|
||
// UMD returnExports | ||
if ( typeof define === "function" && define.amd ) { | ||
|
||
// AMD | ||
define([ | ||
"../globalize-runtime", | ||
"./plural" | ||
], factory ); | ||
} else if ( typeof exports === "object" ) { | ||
|
||
// Node, CommonJS | ||
module.exports = factory( | ||
require( "../globalize-runtime" ), | ||
require( "./plural" ) | ||
); | ||
} else { | ||
|
||
// Extend global | ||
factory( root.Globalize ); | ||
} | ||
}(this, function( Globalize ) { | ||
|
||
var runtimeKey = Globalize._runtimeKey; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Globalize v@VERSION | ||
* | ||
* http://github.com/jquery/globalize | ||
* | ||
* Copyright 2010, 2014 jQuery Foundation, Inc. and other contributors | ||
* Released under the MIT license | ||
* http://jquery.org/license | ||
* | ||
* Date: @DATE | ||
*/ | ||
/*! | ||
* Globalize v@VERSION @DATE Released under the MIT license | ||
* http://git.io/TrdQbw | ||
*/ | ||
(function( root, factory ) { | ||
|
||
// UMD returnExports | ||
if ( typeof define === "function" && define.amd ) { | ||
|
||
// AMD | ||
define([ | ||
"cldr", | ||
"../globalize", | ||
"./number", | ||
"./plural" | ||
], factory ); | ||
} else if ( typeof exports === "object" ) { | ||
|
||
// Node, CommonJS | ||
module.exports = factory( require( "cldrjs" ), require( "globalize" ) ); | ||
} else { | ||
|
||
// Extend global | ||
factory( root.Cldr, root.Globalize ); | ||
} | ||
}(this, function( Cldr, Globalize ) { | ||
|
||
var formatMessage = Globalize._formatMessage, | ||
runtimeBind = Globalize._runtimeBind, | ||
validateParameterPresence = Globalize._validateParameterPresence, | ||
validateParameterTypePlainObject = Globalize._validateParameterTypePlainObject, | ||
validateParameterTypeNumber = Globalize._validateParameterTypeNumber, | ||
validateParameterTypeString = Globalize._validateParameterTypeString; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
define([ | ||
"./common/runtime-key", | ||
"./core-runtime", | ||
"./unit/formatter-fn", | ||
|
||
"./number-runtime", | ||
"./plural-runtime" | ||
], function( runtimeKey, Globalize, unitFormatterFn ) { | ||
|
||
Globalize._unitFormatterFn = unitFormatterFn; | ||
|
||
Globalize.formatUnit = | ||
Globalize.prototype.formatUnit = function( value, unit, options ) { | ||
return this.unitFormatter( unit, options )( value ); | ||
}; | ||
|
||
Globalize.unitFormatter = | ||
Globalize.prototype.unitFormatter = function( unit, options ) { | ||
return Globalize[ runtimeKey( "unitFormatter", this._locale, [ unit, options ] ) ]; | ||
}; | ||
|
||
return Globalize; | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
define([ | ||
"./core", | ||
"./common/runtime-bind", | ||
"./common/validate/parameter-presence", | ||
"./common/validate/parameter-type/number", | ||
"./common/validate/parameter-type/plain-object", | ||
"./common/validate/parameter-type/string", | ||
"./unit/formatter-fn", | ||
"./unit/properties", | ||
|
||
"./number", | ||
"./plural" | ||
], function( Globalize, runtimeBind, validateParameterPresence, validateParameterTypeNumber, | ||
validateParameterTypePlainObject, validateParameterTypeString, unitFormatterFn, | ||
unitProperties ) { | ||
|
||
/** | ||
* Globalize.formatUnit( value, unit, options ) | ||
* | ||
* @value [Number] | ||
* | ||
* @unit [String]: The unit (e.g "second", "day", "year") | ||
* | ||
* @options [Object] | ||
* - form: [String] "long", "short" (default), or "narrow". | ||
* | ||
* Format units such as seconds, minutes, days, weeks, etc. | ||
*/ | ||
Globalize.formatUnit = | ||
Globalize.prototype.formatUnit = function( value, unit, options ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's update this according to the below please. So, it's accessible via both static function ( Globalize.formatUnit =
Globalize.prototype.formatUnit = function( ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed on my local branch (commenting for my own tracking benefit) |
||
validateParameterPresence( value, "value" ); | ||
validateParameterTypeNumber( value, "value" ); | ||
|
||
return this.unitFormatter( unit, options )( value ); | ||
}; | ||
|
||
/** | ||
* Globalize.unitFormatter( unit, options ) | ||
* | ||
* @unit [String]: The unit (e.g "second", "day", "year") | ||
* | ||
* @options [Object] | ||
* - form: [String] "long", "short" (default), or "narrow". | ||
* | ||
* - numberFormatter: [Object] Globalize.numberFormatter object. The default value is the | ||
* default numberFormatter for the current locale. | ||
*/ | ||
Globalize.unitFormatter = | ||
Globalize.prototype.unitFormatter = function( unit, options ) { | ||
var args, form, numberFormatter, pluralGenerator, returnFn, properties; | ||
|
||
validateParameterPresence( unit, "unit" ); | ||
validateParameterTypeString( unit, "unit" ); | ||
|
||
validateParameterPresence( options, "options" ); | ||
validateParameterTypePlainObject( options, "options" ); | ||
|
||
args = [ unit, options ]; | ||
form = options.form || "long"; | ||
properties = unitProperties( unit, form, this.cldr ); | ||
|
||
numberFormatter = options.numberFormatter || this.numberFormatter(); | ||
pluralGenerator = this.pluralGenerator(); | ||
returnFn = unitFormatterFn( properties, numberFormatter, pluralGenerator ); | ||
|
||
runtimeBind( args, this.cldr, returnFn, [ numberFormatter, pluralGenerator, properties ] ); | ||
|
||
return returnFn; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In general, Note the process of setting up the formatter in general is somewhat an expensive operation. This process includes traversing CLDR data and cherry-picking the specific data (which I name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a little confused by this - is it right to say Here's an example of one approach I was looking at for this, let me know if it looks sound. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your gist diff seems perfect! The point of keeping There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good - had to update the tests too, but now everything's passing. |
||
|
||
return Globalize; | ||
|
||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GitHub has some kind of issue with this file. Looking at the source locally, I also get messed up syntax highlighting in my editor (SublimeText). Looks like an issue with the regex on line 192, completely unrelated to this PR though. Somehow this helps:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jzaefferer can you file a separate ticket for this please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for the record, #527 (fixed by #529)