Skip to content

Commit 6919f43

Browse files
rpayamirxaviers
authored andcommitted
Date: Add formatDateToParts (1/2)
Ref globalizejs#678 Ref globalizejs#697 Ref globalizejs#700
1 parent 687207b commit 6919f43

File tree

12 files changed

+2395
-505
lines changed

12 files changed

+2395
-505
lines changed

src/date-runtime.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,15 @@ define([
55
"./common/validate/parameter-type/string",
66
"./core-runtime",
77
"./date/format",
8-
"./date/formatter-fn",
98
"./date/parse",
109
"./date/parser-fn",
1110
"./date/tokenizer",
1211

1312
"./number-runtime"
1413
], function( runtimeKey, validateParameterPresence, validateParameterTypeDate,
15-
validateParameterTypeString, Globalize, dateFormat, dateFormatterFn, dateParse, dateParserFn,
14+
validateParameterTypeString, Globalize, dateFormat, dateParse, dateParserFn,
1615
dateTokenizer ) {
1716

18-
Globalize._dateFormatterFn = dateFormatterFn;
1917
Globalize._dateParserFn = dateParserFn;
2018
Globalize._dateFormat = dateFormat;
2119
Globalize._dateParser = dateParse;
@@ -24,8 +22,19 @@ Globalize._validateParameterTypeDate = validateParameterTypeDate;
2422

2523
Globalize.dateFormatter =
2624
Globalize.prototype.dateFormatter = function( options ) {
25+
var formatterFn = this.dateToPartsFormatter( options );
26+
return function() {
27+
var parts = formatterFn.apply( this, arguments );
28+
return parts.map( function( part ) {
29+
return part.value;
30+
}).join( "" );
31+
};
32+
};
33+
34+
Globalize.dateToPartsFormatter =
35+
Globalize.prototype.dateToPartsFormatter = function( options ) {
2736
options = options || { skeleton: "yMd" };
28-
return Globalize[ runtimeKey( "dateFormatter", this._locale, [ options ] ) ];
37+
return Globalize[ runtimeKey( "dateToPartsFormatter", this._locale, [ options ] ) ];
2938
};
3039

3140
Globalize.dateParser =
@@ -42,6 +51,14 @@ Globalize.prototype.formatDate = function( value, options ) {
4251
return this.dateFormatter( options )( value );
4352
};
4453

54+
Globalize.formatDateToParts =
55+
Globalize.prototype.formatDateToParts = function( value, options ) {
56+
validateParameterPresence( value, "value" );
57+
validateParameterTypeDate( value, "value" );
58+
59+
return this.dateToPartsFormatter( options )( value );
60+
};
61+
4562
Globalize.parseDate =
4663
Globalize.prototype.parseDate = function( value, options ) {
4764
validateParameterPresence( value, "value" );

src/date.js

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ define([
1010
"./common/validate/parameter-type/string",
1111
"./core",
1212
"./date/expand-pattern",
13-
"./date/formatter-fn",
13+
"./date/to-parts-formatter-fn",
1414
"./date/format-properties",
1515
"./date/parser-fn",
1616
"./date/parse-properties",
@@ -21,7 +21,7 @@ define([
2121
"./number"
2222
], function( Cldr, runtimeBind, validate, validateCldr, validateDefaultLocale,
2323
validateParameterPresence, validateParameterTypeDate, validateParameterTypePlainObject,
24-
validateParameterTypeString, Globalize, dateExpandPattern, dateFormatterFn,
24+
validateParameterTypeString, Globalize, dateExpandPattern, dateToPartsFormatterFn,
2525
dateFormatProperties, dateParserFn, dateParseProperties, dateTokenizerProperties ) {
2626

2727
function validateRequiredCldr( path, value ) {
@@ -77,6 +77,33 @@ function validateOptionsSkeleton( pattern, skeleton ) {
7777
*/
7878
Globalize.dateFormatter =
7979
Globalize.prototype.dateFormatter = function( options ) {
80+
var formatterFn = this.dateToPartsFormatter( options );
81+
return function() {
82+
var parts = formatterFn.apply( this, arguments );
83+
return parts.map( function( part ) {
84+
return part.value;
85+
}).join( "" );
86+
};
87+
};
88+
89+
/**
90+
* .dateToPartsFormatter( options )
91+
*
92+
* @options [Object] see date/expand_pattern for more info.
93+
*
94+
* Return a date formatter function (of the form below) according to the given options and the
95+
* default/instance locale.
96+
*
97+
* fn( value )
98+
*
99+
* @value [Date]
100+
*
101+
* Return a function that formats a date to parts according to the given `format`
102+
* and the default/instance
103+
* locale.
104+
*/
105+
Globalize.dateToPartsFormatter =
106+
Globalize.prototype.dateToPartsFormatter = function( options ) {
80107
var args, cldr, numberFormatters, pad, pattern, properties, returnFn;
81108

82109
validateParameterTypePlainObject( options, "options" );
@@ -104,7 +131,7 @@ Globalize.prototype.dateFormatter = function( options ) {
104131
});
105132
}
106133

107-
returnFn = dateFormatterFn( numberFormatters, properties );
134+
returnFn = dateToPartsFormatterFn( numberFormatters, properties );
108135

109136
runtimeBind( args, cldr, returnFn, [ numberFormatters, properties ] );
110137

@@ -166,6 +193,23 @@ Globalize.prototype.formatDate = function( value, options ) {
166193
return this.dateFormatter( options )( value );
167194
};
168195

196+
/**
197+
* .formatDateToParts( value, options )
198+
*
199+
* @value [Date]
200+
*
201+
* @options [Object] see date/expand_pattern for more info.
202+
*
203+
* Formats a date or number to parts according to the given options and the default/instance locale.
204+
*/
205+
Globalize.formatDateToParts =
206+
Globalize.prototype.formatDateToParts = function( value, options ) {
207+
validateParameterPresence( value, "value" );
208+
validateParameterTypeDate( value, "value" );
209+
210+
return this.dateToPartsFormatter( options )( value );
211+
};
212+
169213
/**
170214
* .parseDate( value, options )
171215
*

src/date/fields-map.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
define([
2+
"./inverse-fields-map"
3+
], function( dateInverseFieldsMap ) {
4+
5+
// Invert key and values, e.g., {"year": "yY"} ==> {"y": "year", "Y": "year"}
6+
return Object.keys( dateInverseFieldsMap ).reduce( function( map, key ) {
7+
var value = dateInverseFieldsMap[ key ];
8+
value.split( "" ).forEach(function( symbol ) {
9+
map[ symbol ] = key;
10+
});
11+
return map;
12+
}, {});
13+
14+
});

0 commit comments

Comments
 (0)