From 87f841c47cf737e8f66b7119274a79d7c3214136 Mon Sep 17 00:00:00 2001 From: Simon Bernier St-Pierre Date: Thu, 5 Jun 2014 10:38:22 -0400 Subject: [PATCH] Implemented suggested changes `moment.js` now has a separate parseISO function which only handles the parsing. `makeDateFromString` keeps the same behavior and uses parseISO. The tests have also been refactored and moved into their own function. --- moment.js | 24 +++++++++++++----------- test/moment/create.js | 21 ++++++++++----------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/moment.js b/moment.js index 8364399c4..07525236d 100644 --- a/moment.js +++ b/moment.js @@ -1345,7 +1345,7 @@ function makeDateFromStringAndFormat(config) { if (config._f === moment.ISO_8601) { - makeDateFromString(config, false); + parseISO(config); return; } @@ -1461,13 +1461,11 @@ } // date from iso format - function makeDateFromString(config, useFallback) { + function parseISO(config) { var i, l, string = config._i, match = isoRegex.exec(string); - useFallback = typeof useFallback === 'undefined' ? true : useFallback; - if (match) { config._pf.iso = true; for (i = 0, l = isoDates.length; i < l; i++) { @@ -1487,13 +1485,17 @@ config._f += "Z"; } makeDateFromStringAndFormat(config); + } else { + config._isValid = false; } - else { - if (useFallback) { - moment.createFromInputFallback(config); - } else { - config._isValid = false; - } + } + + // date from iso format or fallback + function makeDateFromString(config) { + parseISO(config); + if (config._isValid === false) { + delete config._isValid; + moment.createFromInputFallback(config); } } @@ -1834,7 +1836,7 @@ moment.defaultFormat = isoFormat; // constant that refers to the ISO standard - moment.ISO_8601 = 'ISO 8601'; + moment.ISO_8601 = function () {}; // Plugins that add properties should also add the key here (null value), // so we can properly clone ourselves. diff --git a/test/moment/create.js b/test/moment/create.js index e81eedb23..ca33d4e12 100644 --- a/test/moment/create.js +++ b/test/moment/create.js @@ -395,18 +395,17 @@ exports.create = { test.equal(moment('01', ["MM", "DD"])._f, "MM", "Should use first valid format"); - // pass an ISO date in the array of formats - function parseISO(string) { - return moment(string, [moment.ISO_8601, 'MM', 'HH:mm', 'YYYY']); - } - test.equal(parseISO('1994')._f, 'YYYY', 'iso: test parse YYYY'); - test.equal(parseISO('17:15')._f, 'HH:mm', 'iso: test parse HH:mm'); - test.equal(parseISO('06')._f, 'MM', 'iso: test parse MM'); - test.equal(parseISO('2012-06-01')._pf.iso, true, 'iso: test parse iso'); - - test.equal(moment('2014-05-05', [moment.ISO_8601, 'YYYY-MM-DD'])._pf.iso, true, 'iso: edge case array precedence iso'); - test.equal(moment('2014-05-05', ['YYYY-MM-DD', moment.ISO_8601])._pf.iso, false, 'iso: edge case array precedence not iso'); + test.done(); + }, + "string with array of formats + ISO": function (test) { + test.equal(moment('1994', [moment.ISO_8601, 'MM', 'HH:mm', 'YYYY']).year(), 1994, 'iso: test parse YYYY'); + test.equal(moment('17:15', [moment.ISO_8601, 'MM', 'HH:mm', 'YYYY']).hour(), 17, 'iso: test parse HH:mm (1)'); + test.equal(moment('17:15', [moment.ISO_8601, 'MM', 'HH:mm', 'YYYY']).minutes(), 15, 'iso: test parse HH:mm (2)'); + test.equal(moment('06', [moment.ISO_8601, 'MM', 'HH:mm', 'YYYY']).month(), 6-1, 'iso: test parse MM'); + test.equal(moment('2012-06-01', [moment.ISO_8601, 'MM', 'HH:mm', 'YYYY']).parsingFlags().iso, true, 'iso: test parse iso'); + test.equal(moment('2014-05-05', [moment.ISO_8601, 'YYYY-MM-DD']).parsingFlags().iso, true, 'iso: edge case array precedence iso'); + test.equal(moment('2014-05-05', ['YYYY-MM-DD', moment.ISO_8601]).parsingFlags().iso, false, 'iso: edge case array precedence not iso'); test.done(); }, -- 2.47.2