Skip to content

Commit

Permalink
Date: Add formatDateToParts (1/2)
Browse files Browse the repository at this point in the history
  • Loading branch information
rpayami authored and rxaviers committed Apr 12, 2017
1 parent 687207b commit 6919f43
Show file tree
Hide file tree
Showing 12 changed files with 2,395 additions and 505 deletions.
25 changes: 21 additions & 4 deletions src/date-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ define([
"./common/validate/parameter-type/string",
"./core-runtime",
"./date/format",
"./date/formatter-fn",
"./date/parse",
"./date/parser-fn",
"./date/tokenizer",

"./number-runtime"
], function( runtimeKey, validateParameterPresence, validateParameterTypeDate,
validateParameterTypeString, Globalize, dateFormat, dateFormatterFn, dateParse, dateParserFn,
validateParameterTypeString, Globalize, dateFormat, dateParse, dateParserFn,
dateTokenizer ) {

Globalize._dateFormatterFn = dateFormatterFn;
Globalize._dateParserFn = dateParserFn;
Globalize._dateFormat = dateFormat;
Globalize._dateParser = dateParse;
Expand All @@ -24,8 +22,19 @@ Globalize._validateParameterTypeDate = validateParameterTypeDate;

Globalize.dateFormatter =
Globalize.prototype.dateFormatter = function( options ) {
var formatterFn = this.dateToPartsFormatter( options );
return function() {
var parts = formatterFn.apply( this, arguments );
return parts.map( function( part ) {
return part.value;
}).join( "" );
};
};

Globalize.dateToPartsFormatter =
Globalize.prototype.dateToPartsFormatter = function( options ) {
options = options || { skeleton: "yMd" };
return Globalize[ runtimeKey( "dateFormatter", this._locale, [ options ] ) ];
return Globalize[ runtimeKey( "dateToPartsFormatter", this._locale, [ options ] ) ];
};

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

Globalize.formatDateToParts =
Globalize.prototype.formatDateToParts = function( value, options ) {
validateParameterPresence( value, "value" );
validateParameterTypeDate( value, "value" );

return this.dateToPartsFormatter( options )( value );
};

Globalize.parseDate =
Globalize.prototype.parseDate = function( value, options ) {
validateParameterPresence( value, "value" );
Expand Down
50 changes: 47 additions & 3 deletions src/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ define([
"./common/validate/parameter-type/string",
"./core",
"./date/expand-pattern",
"./date/formatter-fn",
"./date/to-parts-formatter-fn",
"./date/format-properties",
"./date/parser-fn",
"./date/parse-properties",
Expand All @@ -21,7 +21,7 @@ define([
"./number"
], function( Cldr, runtimeBind, validate, validateCldr, validateDefaultLocale,
validateParameterPresence, validateParameterTypeDate, validateParameterTypePlainObject,
validateParameterTypeString, Globalize, dateExpandPattern, dateFormatterFn,
validateParameterTypeString, Globalize, dateExpandPattern, dateToPartsFormatterFn,
dateFormatProperties, dateParserFn, dateParseProperties, dateTokenizerProperties ) {

function validateRequiredCldr( path, value ) {
Expand Down Expand Up @@ -77,6 +77,33 @@ function validateOptionsSkeleton( pattern, skeleton ) {
*/
Globalize.dateFormatter =
Globalize.prototype.dateFormatter = function( options ) {
var formatterFn = this.dateToPartsFormatter( options );
return function() {
var parts = formatterFn.apply( this, arguments );
return parts.map( function( part ) {
return part.value;
}).join( "" );
};
};

/**
* .dateToPartsFormatter( options )
*
* @options [Object] see date/expand_pattern for more info.
*
* Return a date formatter function (of the form below) according to the given options and the
* default/instance locale.
*
* fn( value )
*
* @value [Date]
*
* Return a function that formats a date to parts according to the given `format`
* and the default/instance
* locale.
*/
Globalize.dateToPartsFormatter =
Globalize.prototype.dateToPartsFormatter = function( options ) {
var args, cldr, numberFormatters, pad, pattern, properties, returnFn;

validateParameterTypePlainObject( options, "options" );
Expand Down Expand Up @@ -104,7 +131,7 @@ Globalize.prototype.dateFormatter = function( options ) {
});
}

returnFn = dateFormatterFn( numberFormatters, properties );
returnFn = dateToPartsFormatterFn( numberFormatters, properties );

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

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

/**
* .formatDateToParts( value, options )
*
* @value [Date]
*
* @options [Object] see date/expand_pattern for more info.
*
* Formats a date or number to parts according to the given options and the default/instance locale.
*/
Globalize.formatDateToParts =
Globalize.prototype.formatDateToParts = function( value, options ) {
validateParameterPresence( value, "value" );
validateParameterTypeDate( value, "value" );

return this.dateToPartsFormatter( options )( value );
};

/**
* .parseDate( value, options )
*
Expand Down
14 changes: 14 additions & 0 deletions src/date/fields-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
define([
"./inverse-fields-map"
], function( dateInverseFieldsMap ) {

// Invert key and values, e.g., {"year": "yY"} ==> {"y": "year", "Y": "year"}
return Object.keys( dateInverseFieldsMap ).reduce( function( map, key ) {
var value = dateInverseFieldsMap[ key ];
value.split( "" ).forEach(function( symbol ) {
map[ symbol ] = key;
});
return map;
}, {});

});
Loading

0 comments on commit 6919f43

Please sign in to comment.