]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Strict parsing now works. Add tests!
authorIskren Chernev <iskren.chernev@gmail.com>
Mon, 24 Jun 2013 17:15:45 +0000 (10:15 -0700)
committerIskren Chernev <iskren.chernev@gmail.com>
Tue, 17 Sep 2013 07:21:03 +0000 (00:21 -0700)
moment.js

index d5efd545a185862c019871beb900bd6be9744464..743258739ff93a4d32a16731e02407254d019a8f 100644 (file)
--- a/moment.js
+++ b/moment.js
     }
 
     function makeDateFromStringAndStrictFormat(config) {
-        var regexp = '', non_token_start = 0;
+        var regexp = '', non_token_start = 0,
+            tokens = config._f.match(formattingTokens),
+            match, i, tokenIndex;
 
         // var s = config._f;
         // for (var i = 0; i < s.length; ++i) {
 
         // We're not interested in the result. Just the tokens and their
         // starting positions.
-        config._f.replace(formattingTokens, function(token) {
+        config._f.replace(formattingTokens, function (token) {
             var offset = arguments[arguments.length - 2],
                 tokenRegexp;
 
             if (formatTokenFunctions[token]) {
                 tokenRegexp = getParseRegexForToken(token).toString();
+                // Do not remember groups
+                tokenRegexp = tokenRegexp.replace(/\(/g, '(?:');
                 // this is a real token
 
                 // regexp-escape strings in-between tokens
 
                 console.log("adding " + tokenRegexp + "### " + tokenRegexp.substring(1, tokenRegexp.lastIndexOf('/')));
                 // add token regexp
-                regexp += tokenRegexp.substring(1, tokenRegexp.lastIndexOf('/'));
+                regexp += '(' + tokenRegexp.substring(1, tokenRegexp.lastIndexOf('/')) + ')';
             } else {
                 console.log("not a token " + token);
             }
         });
         regexp = new RegExp('^' + regexp + '$');
         console.log(regexp);
-        console.log(config._i.match(regexp));
-        // TODO: non-matching groups in upper regex, put match groups around
-        // regexes, get tokens, parse.
-        // new RegExp(regexp).match(config._i)
+        match = config._i.match(regexp);
+        console.log(match);
+        if (match === null) {
+            return null;
+        }
+
+        config._a = [];
+        for (i = tokenIndex = 0; i < tokens.length; ++i) {
+            if (formatTokenFunctions[tokens[i]]) {
+                addTimeToArrayFromToken(tokens[i], match[tokenIndex + 1], config);
+                ++tokenIndex;
+            }
+        }
+
+        console.log(config._a);
+        dateFromArray(config);
     }
 
     function unescapeFormat(s) {
         console.log("unescaping " + s);
-        return s.replace(/\\(\[)|\\(\])|\[([^\]\[]*)\]|\\(.)/g, function(matched, p1, p2, p3, p4) {
+        return s.replace(/\\(\[)|\\(\])|\[([^\]\[]*)\]|\\(.)/g, function (matched, p1, p2, p3, p4) {
             console.log(matched, p1, p2, p3, p4);
             return p1 || p2 || p3 || p4;
         });
     }
 
     moment = function (input, format, lang, strict) {
-        if (lang == true) {
+        if (lang === true) {
             lang = undefined;
             strict = true;
         }
     };
 
     // creating with utc
-    moment.utc = function (input, format, lang) {
-        if (lang == true) {
+    moment.utc = function (input, format, lang, strict) {
+        if (lang === true) {
             lang = undefined;
             strict = true;
         }