]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fixing thrown error when parsing an empty string with multiple formats
authorTim Wood <washwithcare@gmail.com>
Thu, 3 May 2012 16:15:48 +0000 (09:15 -0700)
committerTim Wood <washwithcare@gmail.com>
Thu, 3 May 2012 16:15:48 +0000 (09:15 -0700)
#301

moment.js
test/moment/create.js

index 87862ff21dddedb0a7ef4ed09ba9c8973bfd1468..08b3f49aba7314873178775dd4f92f3c870037cf 100644 (file)
--- a/moment.js
+++ b/moment.js
         // MONTH
         case 'M' : // fall through to MM
         case 'MM' :
-            datePartArray[1] = ~~input - 1;
+            datePartArray[1] = (input == null) ? 0 : ~~input - 1;
             break;
         case 'MMM' : // fall through to MMMM
         case 'MMMM' :
             i, parsedInput;
 
         for (i = 0; i < tokens.length; i++) {
-            parsedInput = (getParseRegexForToken(tokens[i]).exec(string) || [0])[0];
+            parsedInput = (getParseRegexForToken(tokens[i]).exec(string) || [])[0];
             string = string.replace(getParseRegexForToken(tokens[i]), '');
             addTimeToArrayFromToken(tokens[i], parsedInput, datePartArray, config);
         }
     // date from string and array of format strings
     function makeDateFromStringAndArray(string, formats) {
         var output,
-            inputParts = string.match(parseMultipleFormatChunker),
+            inputParts = string.match(parseMultipleFormatChunker) || [],
             formattedInputParts,
             scoreToBeat = 99,
             i,
             currentScore;
         for (i = 0; i < formats.length; i++) {
             currentDate = makeDateFromStringAndFormat(string, formats[i]);
-            formattedInputParts = formatMoment(new Moment(currentDate), formats[i]).match(parseMultipleFormatChunker);
+            formattedInputParts = formatMoment(new Moment(currentDate), formats[i]).match(parseMultipleFormatChunker) || [];
             currentScore = compareArrays(inputParts, formattedInputParts);
             if (currentScore < scoreToBeat) {
                 scoreToBeat = currentScore;
index 05ddf93e5a97c6aac05b1df6c623eebe75449f98..66be79671d51a86ba611cfad9588ed0556d6223e 100644 (file)
@@ -75,6 +75,16 @@ exports.create = {
         test.done();
     },
 
+    "empty string with formats" : function(test) {
+        test.expect(3);
+        
+        test.equal(moment(' ', 'MM').format('YYYY-MM-DD HH:mm:ss'), '1900-01-01 00:00:00', 'should not break if input is an empty string');
+        test.equal(moment(' ', 'DD').format('YYYY-MM-DD HH:mm:ss'), '1900-01-01 00:00:00', 'should not break if input is an empty string');
+        test.equal(moment(' ', ['MM', "DD"]).format('YYYY-MM-DD HH:mm:ss'), '1900-01-01 00:00:00', 'should not break if input is an empty string');
+        
+        test.done();
+    },
+
     "string with format" : function(test) {
         moment.lang('en');
         var a = [