-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Number & Currency: Add format to parts support
- Loading branch information
Showing
44 changed files
with
5,021 additions
and
503 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
## .currencyToPartsFormatter( currency [, options] ) ➜ function( value ) | ||
|
||
Return a function that formats a `currency` into parts tokens according to the given `options` or locale's defaults. | ||
|
||
The returned function is invoked with one argument: the Number `value` to be formatted. | ||
|
||
### Parameters | ||
|
||
#### currency | ||
|
||
3-letter currency code as defined by ISO 4217, eg. `"USD"`. | ||
|
||
#### options | ||
|
||
Please, see [.currencyFormatter() options](./currency-formatter.md#parameters). | ||
|
||
#### value | ||
|
||
Number to be formatted, eg. `9.99`. | ||
|
||
### Returns | ||
|
||
An Array of objects containing the formatted currency in parts. The returned structure looks like this: | ||
|
||
```js | ||
[ | ||
{ type: "day", value: "17" }, | ||
{ type: "weekday", value: "Monday" } | ||
] | ||
``` | ||
|
||
Possible types are the following: | ||
|
||
- `currency` | ||
|
||
The currency string, such as the symbols `"$"` and `"€"` or the name `"Dollar"`, `"Euro"` depending on which style is used. | ||
|
||
Please, see [.numberToPartsFormatter()](../number/number-to-parts-formatter.md#returns) for details about the inherited number parts such as `decimal`, `fraction`, `group`, `infinity`, `integer`, `literal`, `minusSign`, `nan`, `plusSign`, `percentSign`, and `compact`. | ||
|
||
### Example | ||
|
||
Prior to using any currency methods, you must load `cldr/main/{locale}/currencies.json`, `cldr/supplemental/currencyData.json`, and the CLDR content required by the number module. If using plural messages, you also must load the CLDR content required by the plural module. Read [CLDR content][] if you need more information. | ||
|
||
[CLDR content]: ../../../README.md#2-cldr-content | ||
|
||
#### Static Formatter | ||
|
||
#### Using the default options | ||
|
||
You can use the static method `Globalize.currencyToPartsFormatter()`, which uses the default locale. | ||
|
||
```javascript | ||
var formatter; | ||
|
||
Globalize.locale( "en" ); | ||
formatter = Globalize.currencyToPartsFormatter( "USD" ); | ||
|
||
formatter( 9.99 ); | ||
// > [ | ||
// { "type": "currency", "value": "$" }, | ||
// { "type": "integer", "value": "9" }, | ||
// { "type": "decimal", "value": "." }, | ||
// { "type": "fraction", "value": "99" } | ||
// ] | ||
``` | ||
|
||
#### Instance Formatter | ||
|
||
You can use the instance method `.currencyFormatter()`, which uses the instance locale. | ||
|
||
```javascript | ||
var deFormatter = Globalize( "de" ).currencyToPartsFormatter( "EUR" ), | ||
zhFormatter = Globalize( "zh" ).currencyToPartsFormatter( "EUR" ); | ||
|
||
deFormatter( 9.99 ); | ||
// > [ | ||
// { "type": "integer", "value": "9" }, | ||
// { "type": "decimal", "value": "," }, | ||
// { "type": "fraction", "value": "99" }, | ||
// { "type": "literal", "value": " " }, | ||
// { "type": "currency", "value": "€" } | ||
// ] | ||
|
||
zhFormatter( 9.99 ); | ||
// > [ | ||
// { "type": "currency", "value": "€" }, | ||
// { "type": "integer", "value": "9" }, | ||
// { "type": "decimal", "value": "." }, | ||
// { "type": "fraction", "value": "99" } | ||
// ] | ||
``` | ||
|
||
The information is available separately and it can be formatted and concatenated again in a customized way. For example by using [`Array.prototype.map()`][], [arrow functions][], a [switch statement][], [template literals][], and [`Array.prototype.reduce()`][]. | ||
|
||
[`Array.prototype.map()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map | ||
[arrow functions]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions | ||
[switch statement]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch | ||
[template literals]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals | ||
[`Array.prototype.reduce()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | ||
|
||
#### More Examples | ||
|
||
Please, see [.currencyFormatter() example](./currency-formatter.md#example) for additional examples such as using alternative `symbolForm`, configuring `style` (symbol, accounting, and name styles), and the inherited number options (e.g., compact numbers). | ||
|
||
#### Performance Suggestion | ||
|
||
For improved performance on iterations, first create the formatter. Then, reuse it on each loop. | ||
|
||
```javascript | ||
var formatter = Globalize( "en" ).currencyToPartsFormatter( "USD" ); | ||
|
||
renderInvoice({ | ||
prices: prices.map(function( price ) { | ||
return formatter( price ); | ||
}) | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
## .numberToPartsFormatter( [options] ) ➜ function( value ) | ||
|
||
Return a function that formats a number into parts tokens according to the given options. | ||
|
||
The returned function is invoked with one argument: the Number `value` to be formatted. | ||
|
||
### Parameters | ||
|
||
#### options | ||
|
||
Please, see [.numberFormatter() options](./number-formatter.md#parameters). | ||
|
||
### Returns | ||
|
||
An Array of objects containing the formatted number in parts. The returned structure looks like this: | ||
|
||
- `decimal` | ||
|
||
The decimal separator string, e.g., `"."`. | ||
|
||
- `fraction` | ||
|
||
The fraction number. | ||
|
||
- `group` | ||
|
||
The group separator string, e.g., `","`. | ||
|
||
- `infinity` | ||
|
||
The Infinity string, e.g., `"∞"`. | ||
|
||
- `integer` | ||
|
||
The integer number. | ||
|
||
- `literal` | ||
|
||
Any literal strings or whitespace in the formatted number. | ||
|
||
- `minusSign` | ||
|
||
The minus sign string, e.g., `"-"`. | ||
|
||
- `nan` | ||
|
||
The NaN string, e.g., `"NaN"`. | ||
|
||
- `plusSign` | ||
|
||
The plus sign string, e.g., `"+"`. | ||
|
||
- `percentSign` | ||
|
||
The percent sign string, e.g., `"%"`. | ||
|
||
- `compact` | ||
|
||
The compact string, e.g., `"thousand"`. | ||
|
||
### Examples | ||
|
||
Prior to using any number methods, you must load `cldr/main/{locale}/numbers.json` and `cldr/supplemental/numberingSystems.json`. Read [CLDR content][] if you need more information. | ||
|
||
[CLDR content]: ../../../README.md#2-cldr-content | ||
|
||
#### Static Formatter | ||
|
||
You can use the static method `Globalize.numberToPartsFormatter()`, which uses the default locale. | ||
|
||
```javascript | ||
var formatter; | ||
|
||
Globalize.locale( "en" ); | ||
formatter = Globalize.numberToPartsFormatter(); | ||
|
||
formatter( 3.141592 ); | ||
// > [ | ||
// { "type": "integer", "value": "3" }, | ||
// { "type": "decimal", "value": "." }, | ||
// { "type": "fraction", "value": "142" } | ||
// ] | ||
``` | ||
|
||
#### Instance Formatter | ||
|
||
You can use the instance method `.numberFormatter()`, which uses the instance | ||
locale. | ||
|
||
```javascript | ||
var arFormatter = Globalize( "ar" ).numberToPartsFormatter(), | ||
esFormatter = Globalize( "es" ).numberToPartsFormatter(), | ||
zhFormatter = Globalize( "zh-u-nu-native" ).numberToPartsFormatter(); | ||
|
||
arFormatter( 3.141592 ); | ||
// > [ | ||
// { "type": "integer", "value": "٣" }, | ||
// { "type": "decimal", "value": "٫" }, | ||
// { "type": "fraction", "value": "١٤٢" } | ||
// ] | ||
|
||
esFormatter( 3.141592 ); | ||
// > [ | ||
// { "type": "integer", "value": "3" }, | ||
// { "type": "decimal", "value": "," }, | ||
// { "type": "fraction", "value": "142" } | ||
// ] | ||
|
||
zhFormatter( 3.141592 ); | ||
// > [ | ||
// { "type": "integer", "value": "三" }, | ||
// { "type": "decimal", "value": "." }, | ||
// { "type": "fraction", "value": "一四二" } | ||
// ] | ||
``` | ||
|
||
The information is available separately and it can be formatted and concatenated again in a customized way. For example by using [`Array.prototype.map()`][], [arrow functions][], a [switch statement][], [template literals][], and [`Array.prototype.reduce()`][]. | ||
|
||
[`Array.prototype.map()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map | ||
[arrow functions]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions | ||
[switch statement]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch | ||
[template literals]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals | ||
[`Array.prototype.reduce()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | ||
|
||
#### More Examples | ||
|
||
Please, see [.numberFormatter() example](./number-formatter.md#example) for additional examples such as configuring decimal places, significant digits, percentages, and compact numbers. | ||
|
||
#### Performance Suggestions | ||
|
||
For improved performance on iterations, the formatter should be created before the loop. Then, it can be reused in each iteration. | ||
|
||
```javascript | ||
var numbers = [ 1, 1, 2, 3, ... ]; | ||
var formatter = Globalize( "en" ).numberFormatter(); | ||
|
||
formattedNumbers = numbers.map(function( number ) { | ||
return formatter( number ); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.