From a4e2a42ab5b12bcae572756a5bc939fcd6814bb9 Mon Sep 17 00:00:00 2001 From: Rafael Xavier de Souza Date: Tue, 14 Mar 2017 09:50:20 -0300 Subject: [PATCH] fixup! Date: Dynamically augment skeleton (3/3) --- src/date.js | 49 ++++++++++++++++++++------ test/functional/date/date-formatter.js | 14 +++++++- test/functional/date/date-parser.js | 14 +++++++- test/functional/date/format-date.js | 14 +++++++- test/functional/date/parse-date.js | 14 +++++++- 5 files changed, 90 insertions(+), 15 deletions(-) diff --git a/src/date.js b/src/date.js index 61b235ace..f83b8db4f 100644 --- a/src/date.js +++ b/src/date.js @@ -1,13 +1,13 @@ define([ "cldr", "./common/runtime-bind", + "./common/validate", "./common/validate/cldr", "./common/validate/default-locale", "./common/validate/parameter-presence", "./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", @@ -19,10 +19,10 @@ define([ "cldr/event", "cldr/supplemental", "./number" -], function( Cldr, runtimeBind, validateCldr, validateDefaultLocale, validateParameterPresence, - validateParameterTypeDate, validateParameterTypePlainObject, validateParameterTypeString, - validatePattern, Globalize, dateExpandPattern, dateFormatterFn, dateFormatProperties, - dateParserFn, dateParseProperties, dateTokenizerProperties ) { +], function( Cldr, runtimeBind, validate, validateCldr, validateDefaultLocale, + validateParameterPresence, validateParameterTypeDate, validateParameterTypePlainObject, + validateParameterTypeString, Globalize, dateExpandPattern, dateFormatterFn, + dateFormatProperties, dateParserFn, dateParseProperties, dateTokenizerProperties ) { function validateRequiredCldr( path, value ) { validateCldr( path, value, { @@ -35,6 +35,31 @@ function validateRequiredCldr( path, value ) { }); } +function validateOptionsPreset( options ) { + validateOptionsPresetEach( "date", options ); + validateOptionsPresetEach( "time", options ); + validateOptionsPresetEach( "datetime", options ); +} + +function validateOptionsPresetEach( style, options ) { + var value = options[ style ]; + validate( + "E_INVALID_OPTIONS", + "Invalid `{" + style + ": \"{value}\"}` based on provided CLDR.", + value === undefined || [ "short", "medium", "long", "full" ].indexOf( value ) !== -1, + { value: value } + ); +} + +function validateOptionsSkeleton( pattern, skeleton ) { + validate( + "E_INVALID_OPTIONS", + "Invalid `{skeleton: \"{skeleton}\"}` based on provided CLDR.", + skeleton === undefined || ( typeof pattern === "string" && pattern ), + { skeleton: skeleton } + ); +} + /** * .dateFormatter( options ) * @@ -59,13 +84,14 @@ Globalize.prototype.dateFormatter = function( options ) { cldr = this.cldr; options = options || { skeleton: "yMd" }; - args = [ options ]; - + validateOptionsPreset( options ); validateDefaultLocale( cldr ); + args = [ options ]; + cldr.on( "get", validateRequiredCldr ); pattern = dateExpandPattern( options, cldr ); - validatePattern( pattern, options ); + validateOptionsSkeleton( pattern, options ); properties = dateFormatProperties( pattern, cldr ); cldr.off( "get", validateRequiredCldr ); @@ -102,13 +128,14 @@ Globalize.prototype.dateParser = function( options ) { cldr = this.cldr; options = options || { skeleton: "yMd" }; - args = [ options ]; - + validateOptionsPreset( options ); validateDefaultLocale( cldr ); + args = [ options ]; + cldr.on( "get", validateRequiredCldr ); pattern = dateExpandPattern( options, cldr ); - validatePattern( pattern, options ); + validateOptionsSkeleton( pattern, options ); tokenizerProperties = dateTokenizerProperties( pattern, cldr ); parseProperties = dateParseProperties( cldr ); cldr.off( "get", validateRequiredCldr ); diff --git a/test/functional/date/date-formatter.js b/test/functional/date/date-formatter.js index 7419df05a..b71c0fe68 100644 --- a/test/functional/date/date-formatter.js +++ b/test/functional/date/date-formatter.js @@ -43,9 +43,21 @@ QUnit.test( "should validate parameters", function( assert ) { }; }); + assert.throws(function() { + Globalize.dateFormatter({ date: "invalid-stuff" }); + }, /E_INVALID_OPTIONS.*date.*invalid-stuff/ ); + + assert.throws(function() { + Globalize.dateFormatter({ time: "invalid-stuff" }); + }, /E_INVALID_OPTIONS.*time.*invalid-stuff/ ); + + assert.throws(function() { + Globalize.dateFormatter({ datetime: "invalid-stuff" }); + }, /E_INVALID_OPTIONS.*datetime.*invalid-stuff/ ); + assert.throws(function() { Globalize.dateFormatter({ skeleton: "invalid-stuff" }); - }, "E_INVALID_PATTERN: Invalid `{\"skeleton\":\"invalid-stuff\"}` based on provided CLDR." ); + }, /E_INVALID_OPTIONS.*skeleton.*invalid-stuff/ ); }); QUnit.test( "should validate CLDR content", function( assert ) { diff --git a/test/functional/date/date-parser.js b/test/functional/date/date-parser.js index 44e721cf3..fb354e3a7 100644 --- a/test/functional/date/date-parser.js +++ b/test/functional/date/date-parser.js @@ -50,9 +50,21 @@ QUnit.test( "should validate parameters", function( assert ) { }; }); + assert.throws(function() { + Globalize.dateParser({ date: "invalid-stuff" }); + }, /E_INVALID_OPTIONS.*date.*invalid-stuff/ ); + + assert.throws(function() { + Globalize.dateParser({ time: "invalid-stuff" }); + }, /E_INVALID_OPTIONS.*time.*invalid-stuff/ ); + + assert.throws(function() { + Globalize.dateParser({ datetime: "invalid-stuff" }); + }, /E_INVALID_OPTIONS.*datetime.*invalid-stuff/ ); + assert.throws(function() { Globalize.dateParser({ skeleton: "invalid-stuff" }); - }, "E_INVALID_PATTERN: Invalid `{\"skeleton\":\"invalid-stuff\"}` based on provided CLDR." ); + }, /E_INVALID_OPTIONS.*skeleton.*invalid-stuff/ ); }); QUnit.test( "should validate CLDR content", function( assert ) { diff --git a/test/functional/date/format-date.js b/test/functional/date/format-date.js index f74634f6f..84d0c1505 100644 --- a/test/functional/date/format-date.js +++ b/test/functional/date/format-date.js @@ -67,9 +67,21 @@ QUnit.test( "should validate parameters", function( assert ) { }; }); + assert.throws(function() { + Globalize.formatDate(date, { date: "invalid-stuff" }); + }, /E_INVALID_OPTIONS.*date.*invalid-stuff/ ); + + assert.throws(function() { + Globalize.formatDate(date, { time: "invalid-stuff" }); + }, /E_INVALID_OPTIONS.*time.*invalid-stuff/ ); + + assert.throws(function() { + Globalize.formatDate(date, { datetime: "invalid-stuff" }); + }, /E_INVALID_OPTIONS.*datetime.*invalid-stuff/ ); + assert.throws(function() { Globalize.formatDate(date, { skeleton: "invalid-stuff" }); - }, "E_INVALID_PATTERN: Invalid `{\"skeleton\":\"invalid-stuff\"}` based on provided CLDR." ); + }, /E_INVALID_OPTIONS.*skeleton.*invalid-stuff/ ); }); QUnit.test( "should validate CLDR content", function( assert ) { diff --git a/test/functional/date/parse-date.js b/test/functional/date/parse-date.js index a09710c2f..65af57310 100644 --- a/test/functional/date/parse-date.js +++ b/test/functional/date/parse-date.js @@ -71,9 +71,21 @@ QUnit.test( "should validate parameters", function( assert ) { }; }); + assert.throws(function() { + Globalize.parseDate( "15", { date: "invalid-stuff" }); + }, /E_INVALID_OPTIONS.*date.*invalid-stuff/ ); + + assert.throws(function() { + Globalize.parseDate( "15", { time: "invalid-stuff" }); + }, /E_INVALID_OPTIONS.*time.*invalid-stuff/ ); + + assert.throws(function() { + Globalize.parseDate( "15", { datetime: "invalid-stuff" }); + }, /E_INVALID_OPTIONS.*datetime.*invalid-stuff/ ); + assert.throws(function() { Globalize.parseDate( "15", { skeleton: "invalid-stuff" }); - }, "E_INVALID_PATTERN: Invalid `{\"skeleton\":\"invalid-stuff\"}` based on provided CLDR." ); + }, /E_INVALID_OPTIONS.*skeleton.*invalid-stuff/ ); }); QUnit.test( "should validate CLDR content", function( assert ) {