Skip to content

Commit

Permalink
fixup! Date: Dynamically augment skeleton (3/3)
Browse files Browse the repository at this point in the history
  • Loading branch information
rxaviers committed Mar 14, 2017
1 parent 4b85404 commit 73c8525
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/build/intro-date.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var createError = Globalize._createError,
removeLiteralQuotes = Globalize._removeLiteralQuotes,
runtimeBind = Globalize._runtimeBind,
stringPad = Globalize._stringPad,
validate = Globalize._validate,
validateCldr = Globalize._validateCldr,
validateDefaultLocale = Globalize._validateDefaultLocale,
validateParameterPresence = Globalize._validateParameterPresence,
Expand Down
7 changes: 5 additions & 2 deletions src/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ define([
"./common/validate/parameter-type/date",
"./common/validate/parameter-type/plain-object",
"./common/validate/parameter-type/string",
"./common/validate/pattern",
"./core",
"./date/expand-pattern",
"./date/formatter-fn",
Expand All @@ -20,8 +21,8 @@ define([
"./number"
], function( Cldr, runtimeBind, validateCldr, validateDefaultLocale, validateParameterPresence,
validateParameterTypeDate, validateParameterTypePlainObject, validateParameterTypeString,
Globalize, dateExpandPattern, dateFormatterFn, dateFormatProperties, dateParserFn,
dateParseProperties, dateTokenizerProperties ) {
validatePattern, Globalize, dateExpandPattern, dateFormatterFn, dateFormatProperties,
dateParserFn, dateParseProperties, dateTokenizerProperties ) {

function validateRequiredCldr( path, value ) {
validateCldr( path, value, {
Expand Down Expand Up @@ -64,6 +65,7 @@ Globalize.prototype.dateFormatter = function( options ) {

cldr.on( "get", validateRequiredCldr );
pattern = dateExpandPattern( options, cldr );
validatePattern( pattern, options );
properties = dateFormatProperties( pattern, cldr );
cldr.off( "get", validateRequiredCldr );

Expand Down Expand Up @@ -106,6 +108,7 @@ Globalize.prototype.dateParser = function( options ) {

cldr.on( "get", validateRequiredCldr );
pattern = dateExpandPattern( options, cldr );
validatePattern( pattern, options );
tokenizerProperties = dateTokenizerProperties( pattern, cldr );
parseProperties = dateParseProperties( cldr );
cldr.off( "get", validateRequiredCldr );
Expand Down
4 changes: 3 additions & 1 deletion src/date/expand-pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ return function( options, cldr ) {
skeleton = options.skeleton;

// Preferred hour (j).
skeleton = skeleton.replace( /j/g, cldr.supplemental.timeData.preferred() );
skeleton = skeleton.replace( /j/g, function() {
return cldr.supplemental.timeData.preferred();
});

// Try direct map (note that getBestMatchPattern handles it).
// ... or, try to "best match" the whole skeleton.
Expand Down
23 changes: 22 additions & 1 deletion test/functional/date/date-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,32 @@ QUnit.test( "should validate parameters", function( assert ) {
Globalize.dateFormatter( invalidPattern );
};
});

assert.throws(function() {
Globalize.dateFormatter({ skeleton: "invalid-stuff" });
}, "E_INVALID_PATTERN: Invalid `{\"skeleton\":\"invalid-stuff\"}` based on provided CLDR." );
});

QUnit.test( "should validate CLDR content", function( assert ) {
Globalize.load({
"main": {
"en": {
"dates": {
"calendars": {
"gregorian": {
"dateTimeFormats": {
"availableFormats": {
"MMMd": "MMM d"
}
}
}
}
}
}
}
});
util.assertCldrContent( assert, function() {
Globalize.dateFormatter();
Globalize.dateFormatter({ skeleton: "MMMd" });
});
});

Expand Down
23 changes: 22 additions & 1 deletion test/functional/date/date-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,32 @@ QUnit.test( "should validate parameters", function( assert ) {
Globalize.dateParser( invalidValue );
};
});

assert.throws(function() {
Globalize.dateParser({ skeleton: "invalid-stuff" });
}, "E_INVALID_PATTERN: Invalid `{\"skeleton\":\"invalid-stuff\"}` based on provided CLDR." );
});

QUnit.test( "should validate CLDR content", function( assert ) {
Globalize.load({
"main": {
"en": {
"dates": {
"calendars": {
"gregorian": {
"dateTimeFormats": {
"availableFormats": {
"MMMd": "MMM d"
}
}
}
}
}
}
}
});
util.assertCldrContent( assert, function() {
Globalize.dateParser({ skeleton: "d" });
Globalize.dateParser({ skeleton: "MMMd" } );
});
});

Expand Down
23 changes: 22 additions & 1 deletion test/functional/date/format-date.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,32 @@ QUnit.test( "should validate parameters", function( assert ) {
Globalize.formatDate( date, invalidPattern );
};
});

assert.throws(function() {
Globalize.formatDate(date, { skeleton: "invalid-stuff" });
}, "E_INVALID_PATTERN: Invalid `{\"skeleton\":\"invalid-stuff\"}` based on provided CLDR." );
});

QUnit.test( "should validate CLDR content", function( assert ) {
Globalize.load({
"main": {
"en": {
"dates": {
"calendars": {
"gregorian": {
"dateTimeFormats": {
"availableFormats": {
"MMMd": "MMM d"
}
}
}
}
}
}
}
});
util.assertCldrContent( assert, function() {
Globalize.formatDate( date );
Globalize.formatDate( date, { skeleton: "MMMd" });
});
});

Expand Down
23 changes: 22 additions & 1 deletion test/functional/date/parse-date.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,32 @@ QUnit.test( "should validate parameters", function( assert ) {
Globalize.parseDate( "15 Wed", invalidValue );
};
});

assert.throws(function() {
Globalize.parseDate( "15", { skeleton: "invalid-stuff" });
}, "E_INVALID_PATTERN: Invalid `{\"skeleton\":\"invalid-stuff\"}` based on provided CLDR." );
});

QUnit.test( "should validate CLDR content", function( assert ) {
Globalize.load({
"main": {
"en": {
"dates": {
"calendars": {
"gregorian": {
"dateTimeFormats": {
"availableFormats": {
"MMMd": "MMM d"
}
}
}
}
}
}
}
});
util.assertCldrContent( assert, function() {
Globalize.parseDate( "15" );
Globalize.parseDate( "Jan 15", { skeleton: "MMMd" } );
});
});

Expand Down

0 comments on commit 73c8525

Please sign in to comment.