Skip to content

Commit dd7ee4d

Browse files
committed
Number & Currency: Add format to parts support
- Currency: Fix code style - Currency: pluralGenerator better error handling
1 parent d723680 commit dd7ee4d

35 files changed

+3395
-497
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,21 @@ Return a function that formats a number according to the given options or locale
475475

476476
[Read more...](doc/api/number/number-formatter.md)
477477

478+
#### `.numberToPartsFormatter( [options] )`
479+
480+
Return a function that formats a number into parts tokens according to the given options or locale's defaults.
481+
482+
```javascript
483+
.numberToPartsFormatter()( new Date() )
484+
// > [
485+
// { "type": "integer", "value": "3" },
486+
// { "type": "decimal", "value": "." },
487+
// { "type": "fraction", "value": "142" }
488+
// ]
489+
```
490+
491+
[Read more...](doc/api/number/number-to-parts-formatter.md)
492+
478493
#### `.numberParser( [options] )`
479494

480495
Return a function that parses a string representing a number according to the given options or locale's defaults.
@@ -496,6 +511,10 @@ Return a function that parses a string representing a number according to the gi
496511

497512
Alias for `.numberFormatter( [options] )( value )`.
498513

514+
#### `.formatNumberToParts( value [, options] )`
515+
516+
Alias for `.numberToPartsFormatter( [options] )( value )`.
517+
499518
#### `.parseNumber( value [, options] )`
500519

501520
Alias for `.numberParser( [options] )( value )`.
@@ -525,10 +544,33 @@ Return a function that formats a currency according to the given options or loca
525544

526545
[Read more...](doc/api/currency/currency-formatter.md)
527546

547+
#### `.currencyToPartsFormatter( currency [, options] )`
548+
549+
Return a function that formats a currency into parts tokens according to the given options or locale's defaults.
550+
551+
```javascript
552+
.currencyToPartsFormatter()( new Date() )
553+
// > [
554+
// { "type": "currency", "value": "USD" },
555+
// { "type": "literal", "value": " " },
556+
// { "type": "integer", "value": "69" },
557+
// { "type": "group", "value": "," },
558+
// { "type": "integer", "value": "900" },
559+
// { "type": "decimal", "value": "." },
560+
// { "type": "fraction", "value": "00" }
561+
// ]
562+
```
563+
564+
[Read more...](doc/api/currency/currency-to-parts-formatter.md)
565+
528566
#### `.formatCurrency( value, currency [, options] )`
529567

530568
Alias for `.currencyFormatter( currency [, options] )( value )`.
531569

570+
#### `.formatCurrencyToParts( value, currency [, options] )`
571+
572+
Alias for `.currencyToPartsFormatter( currency [, options] )( value )`.
573+
532574
### Plural module
533575

534576
#### `.pluralGenerator( [options] )`

src/build/intro-currency-runtime.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141

4242
"use strict";
4343

44-
var formatMessage = Globalize._formatMessage,
44+
var formatMessageToParts = Globalize._formatMessageToParts,
45+
partsJoin = Globalize._partsJoin,
46+
partsPush = Globalize._partsPush,
4547
runtimeKey = Globalize._runtimeKey,
4648
validateParameterPresence = Globalize._validateParameterPresence,
4749
validateParameterTypeNumber = Globalize._validateParameterTypeNumber;

src/build/intro-currency.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@
3434
}(this, function( Cldr, Globalize ) {
3535

3636
var alwaysArray = Globalize._alwaysArray,
37-
formatMessage = Globalize._formatMessage,
37+
createError = Globalize._createError,
38+
formatMessageToParts = Globalize._formatMessageToParts,
3839
numberNumberingSystem = Globalize._numberNumberingSystem,
3940
numberPattern = Globalize._numberPattern,
41+
partsJoin = Globalize._partsJoin,
42+
partsPush = Globalize._partsPush,
4043
runtimeBind = Globalize._runtimeBind,
4144
stringPad = Globalize._stringPad,
4245
validateCldr = Globalize._validateCldr,

src/build/intro-date-runtime.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343

4444
var createErrorUnsupportedFeature = Globalize._createErrorUnsupportedFeature,
4545
looseMatching = Globalize._looseMatching,
46+
partsJoin = Globalize._partsJoin,
47+
partsPush = Globalize._partsPush,
4648
regexpEscape = Globalize._regexpEscape,
4749
removeLiteralQuotes = Globalize._removeLiteralQuotes,
4850
runtimeKey = Globalize._runtimeKey,

src/build/intro-date.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ var createError = Globalize._createError,
4444
looseMatching = Globalize._looseMatching,
4545
numberNumberingSystemDigitsMap = Globalize._numberNumberingSystemDigitsMap,
4646
numberSymbol = Globalize._numberSymbol,
47+
partsJoin = Globalize._partsJoin,
48+
partsPush = Globalize._partsPush,
4749
regexpEscape = Globalize._regexpEscape,
4850
removeLiteralQuotes = Globalize._removeLiteralQuotes,
4951
runtimeBind = Globalize._runtimeBind,

src/build/intro-number-runtime.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
"use strict";
3939

4040
var createError = Globalize._createError,
41+
partsJoin = Globalize._partsJoin,
42+
partsPush = Globalize._partsPush,
4143
regexpEscape = Globalize._regexpEscape,
4244
runtimeKey = Globalize._runtimeKey,
4345
stringPad = Globalize._stringPad,

src/build/intro-number.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
}(this, function( Cldr, Globalize ) {
3838

3939
var createError = Globalize._createError,
40+
partsJoin = Globalize._partsJoin,
41+
partsPush = Globalize._partsPush,
4042
regexpEscape = Globalize._regexpEscape,
4143
runtimeBind = Globalize._runtimeBind,
4244
stringPad = Globalize._stringPad,

src/common/format-message-to-parts.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
define([
2+
"./parts/push"
3+
], function( partsPush ) {
4+
5+
/**
6+
* formatMessage( message, data )
7+
*
8+
* @message [String] A message with optional {vars} to be replaced.
9+
*
10+
* @data [Array or JSON] Object with replacing-variables content.
11+
*
12+
* Return the formatted message. For example:
13+
*
14+
* - formatMessage( "{0} second", [ 1 ] );
15+
* > [{type: "variable", value: "1", name: "0"}, {type: "literal", value: " second"}]
16+
*
17+
* - formatMessage( "{0}/{1}", ["m", "s"] );
18+
* > [
19+
* { type: "variable", value: "m", name: "0" },
20+
* { type: "literal", value: " /" },
21+
* { type: "variable", value: "s", name: "1" }
22+
* ]
23+
*/
24+
return function( message, data ) {
25+
26+
var lastOffset = 0,
27+
parts = [];
28+
29+
// Create parts.
30+
message.replace( /{[0-9a-zA-Z-_. ]+}/g, function( nameIncludingBrackets, offset ) {
31+
var name = nameIncludingBrackets.slice( 1, -1 );
32+
partsPush( parts, "literal", message.slice( lastOffset, offset ));
33+
partsPush( parts, "variable", data[ name ] );
34+
parts[ parts.length - 1 ].name = name;
35+
lastOffset += offset + nameIncludingBrackets.length;
36+
});
37+
38+
// Skip empty ones such as `{ type: 'literal', value: '' }`.
39+
return parts.filter(function( part ) {
40+
return part.value !== "";
41+
});
42+
};
43+
44+
});

src/common/parts/join.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
define(function() {
2+
3+
/**
4+
* Returns joined parts values.
5+
*/
6+
return function( parts ) {
7+
return parts.map( function( part ) {
8+
return part.value;
9+
}).join( "" );
10+
};
11+
12+
});

src/common/parts/push.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
define(function() {
2+
3+
/**
4+
* Pushes part to parts array, concat two consecutive parts of the same type.
5+
*/
6+
return function( parts, type, value ) {
7+
8+
// Concat two consecutive parts of same type
9+
if ( parts.length && parts[ parts.length - 1 ].type === type ) {
10+
parts[ parts.length - 1 ].value += value;
11+
return;
12+
}
13+
14+
parts.push( { type: type, value: value } );
15+
};
16+
17+
});

0 commit comments

Comments
 (0)