From 329e9a4fd35f30032f1461475e5404a1ae3e71cd Mon Sep 17 00:00:00 2001 From: Iskren Chernev Date: Tue, 29 Sep 2015 01:11:40 -0700 Subject: [PATCH] Use different regexp for extended and basic iso --- src/lib/create/from-string.js | 58 +++++++++++++++-------------------- src/test/moment/create.js | 12 ++++---- 2 files changed, 31 insertions(+), 39 deletions(-) diff --git a/src/lib/create/from-string.js b/src/lib/create/from-string.js index 38bdca17e..6476457e8 100644 --- a/src/lib/create/from-string.js +++ b/src/lib/create/from-string.js @@ -5,36 +5,37 @@ import getParsingFlags from './parsing-flags'; // iso 8601 regex // 0000-00-00 0000-W00 or 0000-W00-0 + T + 00 or 00:00 or 00:00:00 or 00:00:00.000 + +00:00 or +0000 or +00) -var isoRegex = /^\s*((?:[+-]\d{6}|\d{4})-?(?:\d\d-?\d\d|W\d\d-?\d|W\d\d|\d\d\d|\d\d))(?:(T| )(\d\d(?::?\d\d(?::?\d\d(?:[.,]\d+)?)?)?)([\+\-]\d\d(?::?\d\d)?|\s*Z)?)?/; +var extendedIsoRegex = /^\s*((?:[+-]\d{6}|\d{4})-(?:\d\d-\d\d|W\d\d-\d|W\d\d|\d\d\d|\d\d))(?:(T| )(\d\d(?::\d\d(?::\d\d(?:[.,]\d+)?)?)?)([\+\-]\d\d(?::?\d\d)?|\s*Z)?)?/; +var basicIsoRegex = /^\s*((?:[+-]\d{6}|\d{4})(?:\d\d\d\d|W\d\d\d|W\d\d|\d\d\d|\d\d))(?:(T| )(\d\d(?:\d\d(?:\d\d(?:[.,]\d+)?)?)?)([\+\-]\d\d(?::?\d\d)?|\s*Z)?)?/; var tzRegex = /Z|[+-]\d\d(?::?\d\d)?/; var isoDates = [ - ['YYYYYY-MM-DD', /[+-]\d{6}-\d\d-\d\d/, true], - ['YYYY-MM-DD', /\d{4}-\d\d-\d\d/, true], - ['GGGG-[W]WW-E', /\d{4}-W\d\d-\d/, true], - ['GGGG-[W]WW', /\d{4}-W\d\d/, true, false], - ['YYYY-DDD', /\d{4}-\d{3}/, true], - ['YYYY-MM', /\d{4}-\d\d/, true, false], - ['YYYYYYMMDD', /[+-]\d{10}/, false], - ['YYYYMMDD', /\d{8}/, false], + ['YYYYYY-MM-DD', /[+-]\d{6}-\d\d-\d\d/], + ['YYYY-MM-DD', /\d{4}-\d\d-\d\d/], + ['GGGG-[W]WW-E', /\d{4}-W\d\d-\d/], + ['GGGG-[W]WW', /\d{4}-W\d\d/, false], + ['YYYY-DDD', /\d{4}-\d{3}/], + ['YYYY-MM', /\d{4}-\d\d/, false], + ['YYYYYYMMDD', /[+-]\d{10}/], + ['YYYYMMDD', /\d{8}/], // YYYYMM is NOT allowed by the standard - ['GGGG[W]WWE', /\d{4}W\d{3}/, false], - ['GGGG[W]WW', /\d{4}W\d{2}/, false, false], - ['YYYYDDD', /\d{7}/, false] + ['GGGG[W]WWE', /\d{4}W\d{3}/], + ['GGGG[W]WW', /\d{4}W\d{2}/, false], + ['YYYYDDD', /\d{7}/] ]; // iso time formats and regexes var isoTimes = [ - ['HH:mm:ss.SSSS', /\d\d:\d\d:\d\d\.\d+/, true], - ['HH:mm:ss,SSSS', /\d\d:\d\d:\d\d,\d+/, true], - ['HH:mm:ss', /\d\d:\d\d:\d\d/, true], - ['HH:mm', /\d\d:\d\d/, true], - ['HHmmss.SSSS', /\d\d\d\d\d\d\.\d+/, false], - ['HHmmss,SSSS', /\d\d\d\d\d\d,\d+/, false], - ['HHmmss', /\d\d\d\d\d\d/, false], - ['HHmm', /\d\d\d\d/, false], - ['HH', /\d\d/, null] + ['HH:mm:ss.SSSS', /\d\d:\d\d:\d\d\.\d+/], + ['HH:mm:ss,SSSS', /\d\d:\d\d:\d\d,\d+/], + ['HH:mm:ss', /\d\d:\d\d:\d\d/], + ['HH:mm', /\d\d:\d\d/], + ['HHmmss.SSSS', /\d\d\d\d\d\d\.\d+/], + ['HHmmss,SSSS', /\d\d\d\d\d\d,\d+/], + ['HHmmss', /\d\d\d\d\d\d/], + ['HHmm', /\d\d\d\d/], + ['HH', /\d\d/] ]; var aspNetJsonRegex = /^\/?Date\((\-?\d+)/i; @@ -43,8 +44,8 @@ var aspNetJsonRegex = /^\/?Date\((\-?\d+)/i; export function configFromISO(config) { var i, l, string = config._i, - match = isoRegex.exec(string), - extendedDate, extendedTime, allowTime, dateFormat, timeFormat, tzFormat; + match = extendedIsoRegex.exec(string) || basicIsoRegex.exec(string), + allowTime, dateFormat, timeFormat, tzFormat; if (match) { getParsingFlags(config).iso = true; @@ -52,8 +53,7 @@ export function configFromISO(config) { for (i = 0, l = isoDates.length; i < l; i++) { if (isoDates[i][1].exec(match[1])) { dateFormat = isoDates[i][0]; - extendedDate = isoDates[i][2]; - allowTime = isoDates[i][3] !== false; + allowTime = isoDates[i][2] !== false; break; } } @@ -66,7 +66,6 @@ export function configFromISO(config) { if (isoTimes[i][1].exec(match[3])) { // match[2] should be 'T' or space timeFormat = (match[2] || ' ') + isoTimes[i][0]; - extendedTime = isoTimes[i][2]; break; } } @@ -79,13 +78,6 @@ export function configFromISO(config) { config._isValid = false; return; } - if (extendedDate != null && - extendedTime != null && - extendedDate !== extendedTime) { - // extended and basic formats for date and time can NOT be mixed - config._isValid = false; - return; - } if (match[4] != null) { if (tzRegex.exec(match[4])) { tzFormat = 'Z'; diff --git a/src/test/moment/create.js b/src/test/moment/create.js index ad9757d48..ac48dba31 100644 --- a/src/test/moment/create.js +++ b/src/test/moment/create.js @@ -583,12 +583,12 @@ test('parsing iso', function (assert) { }); test('non iso 8601 strings', function (assert) { - assert.ok(!moment('2015-10T10:15', moment.ISO_8601).isValid(), 'incomplete date with time'); - assert.ok(!moment('2015-W10T10:15', moment.ISO_8601).isValid(), 'incomplete week date with time'); - assert.ok(!moment('201510', moment.ISO_8601).isValid(), 'basic YYYYMM is not allowed'); - assert.ok(!moment('2015W10T1015', moment.ISO_8601).isValid(), 'incomplete week date with time (basic)'); - assert.ok(!moment('2015-10-08T1015', moment.ISO_8601).isValid(), 'mixing extended and basic format'); - assert.ok(!moment('20151008T10:15', moment.ISO_8601).isValid(), 'mixing basic and extended format'); + assert.ok(!moment('2015-10T10:15', moment.ISO_8601, true).isValid(), 'incomplete date with time'); + assert.ok(!moment('2015-W10T10:15', moment.ISO_8601, true).isValid(), 'incomplete week date with time'); + assert.ok(!moment('201510', moment.ISO_8601, true).isValid(), 'basic YYYYMM is not allowed'); + assert.ok(!moment('2015W10T1015', moment.ISO_8601, true).isValid(), 'incomplete week date with time (basic)'); + assert.ok(!moment('2015-10-08T1015', moment.ISO_8601, true).isValid(), 'mixing extended and basic format'); + assert.ok(!moment('20151008T10:15', moment.ISO_8601, true).isValid(), 'mixing basic and extended format'); }); test('parsing iso week year/week/weekday', function (assert) { -- 2.47.2