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 73c8525 commit a4e2a42
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 15 deletions.
49 changes: 38 additions & 11 deletions src/date.js
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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, {
Expand All @@ -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 )
*
Expand All @@ -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 );

Expand Down Expand Up @@ -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 );
Expand Down
14 changes: 13 additions & 1 deletion test/functional/date/date-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
14 changes: 13 additions & 1 deletion test/functional/date/date-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
14 changes: 13 additions & 1 deletion test/functional/date/format-date.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
14 changes: 13 additions & 1 deletion test/functional/date/parse-date.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down

0 comments on commit a4e2a42

Please sign in to comment.