From 73c852543f8d82a80b16f0d3de42c1c144359fdc Mon Sep 17 00:00:00 2001 From: Rafael Xavier de Souza Date: Tue, 14 Mar 2017 09:09:04 -0300 Subject: [PATCH] fixup! Date: Dynamically augment skeleton (3/3) --- src/build/intro-date.js | 1 + src/date.js | 7 +++++-- src/date/expand-pattern.js | 4 +++- test/functional/date/date-formatter.js | 23 ++++++++++++++++++++++- test/functional/date/date-parser.js | 23 ++++++++++++++++++++++- test/functional/date/format-date.js | 23 ++++++++++++++++++++++- test/functional/date/parse-date.js | 23 ++++++++++++++++++++++- 7 files changed, 97 insertions(+), 7 deletions(-) diff --git a/src/build/intro-date.js b/src/build/intro-date.js index 12fcd8f16..3ca022f23 100644 --- a/src/build/intro-date.js +++ b/src/build/intro-date.js @@ -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, diff --git a/src/date.js b/src/date.js index 98500c9fa..61b235ace 100644 --- a/src/date.js +++ b/src/date.js @@ -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", @@ -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, { @@ -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 ); @@ -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 ); diff --git a/src/date/expand-pattern.js b/src/date/expand-pattern.js index 995e3691a..ce9729cd5 100644 --- a/src/date/expand-pattern.js +++ b/src/date/expand-pattern.js @@ -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. diff --git a/test/functional/date/date-formatter.js b/test/functional/date/date-formatter.js index d4934701e..7419df05a 100644 --- a/test/functional/date/date-formatter.js +++ b/test/functional/date/date-formatter.js @@ -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" }); }); }); diff --git a/test/functional/date/date-parser.js b/test/functional/date/date-parser.js index ee10b79ef..44e721cf3 100644 --- a/test/functional/date/date-parser.js +++ b/test/functional/date/date-parser.js @@ -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" } ); }); }); diff --git a/test/functional/date/format-date.js b/test/functional/date/format-date.js index a23d69e16..f74634f6f 100644 --- a/test/functional/date/format-date.js +++ b/test/functional/date/format-date.js @@ -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" }); }); }); diff --git a/test/functional/date/parse-date.js b/test/functional/date/parse-date.js index a7ca0d60d..a09710c2f 100644 --- a/test/functional/date/parse-date.js +++ b/test/functional/date/parse-date.js @@ -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" } ); }); });