]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Added strict parsing
authorIskren Chernev <iskren.chernev@gmail.com>
Sat, 27 Apr 2013 06:54:15 +0000 (23:54 -0700)
committerIskren Chernev <iskren.chernev@gmail.com>
Tue, 17 Sep 2013 07:21:03 +0000 (00:21 -0700)
moment.js
test/moment/create.js

index 6fb36b02dca2b6e0210a392b9ec92cc757bb29af..d5efd545a185862c019871beb900bd6be9744464 100644 (file)
--- a/moment.js
+++ b/moment.js
 
     // date from string and format string
     function makeDateFromStringAndFormat(config) {
+        if (config._strict) {
+            return makeDateFromStringAndStrictFormat(config);
+        }
         // This array is used to make a Date, either with `new Date` or `Date.UTC`
         var lang = getLangDefinition(config._l),
             string = '' + config._i,
         dateFromArray(config);
     }
 
+    function makeDateFromStringAndStrictFormat(config) {
+        var regexp = '', non_token_start = 0;
+
+        // var s = config._f;
+        // for (var i = 0; i < s.length; ++i) {
+        //     console.log(i + ": " + s[i] + " (" + s.charCodeAt(i) + ")");
+        // }
+
+        // We're not interested in the result. Just the tokens and their
+        // starting positions.
+        config._f.replace(formattingTokens, function(token) {
+            var offset = arguments[arguments.length - 2],
+                tokenRegexp;
+
+            if (formatTokenFunctions[token]) {
+                tokenRegexp = getParseRegexForToken(token).toString();
+                // this is a real token
+
+                // regexp-escape strings in-between tokens
+                if (offset > non_token_start) {
+                    regexp += regexpEscape(unescapeFormat(config._f.substring(non_token_start, offset)));
+                }
+                non_token_start = offset + token.length;
+
+                console.log("adding " + tokenRegexp + "### " + tokenRegexp.substring(1, tokenRegexp.lastIndexOf('/')));
+                // add token regexp
+                regexp += tokenRegexp.substring(1, tokenRegexp.lastIndexOf('/'));
+            } else {
+                console.log("not a token " + token);
+            }
+
+            return 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)
+    }
+
+    function unescapeFormat(s) {
+        console.log("unescaping " + s);
+        return s.replace(/\\(\[)|\\(\])|\[([^\]\[]*)\]|\\(.)/g, function(matched, p1, p2, p3, p4) {
+            console.log(matched, p1, p2, p3, p4);
+            return p1 || p2 || p3 || p4;
+        });
+        // console.log("unescape _" + s + "_");
+        // for (var i = 0; i < s.length; ++i) {
+        //     console.log(i + ": " + s[i] + " (" + s.charCodeAt(i) + ")");
+        // }
+        // console.log(s[0] + "XX" + s[s.length-2]);
+        // var res = s;
+        // if (s[0] == '[' && s[s.length-1] == ']') {
+        //     res = s.substr(1, s.length - 2);
+        //     console.log("intermid " + res);
+        // }
+        // console.log("intermid " + res);
+        // res = res.replace(/\\./, '$&');
+        // console.log("unescape " + s + " ==> " + res);
+        // return res;
+    }
+
+    // Code from http://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript
+    function regexpEscape(s) {
+        var res = s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
+        console.log("regexp escape " + s + " ===> " + res);
+        return res;
+    }
+
     // date from string and array of format strings
     function makeDateFromStringAndArray(config) {
         var tempConfig,
         return new Moment(config);
     }
 
-    moment = function (input, format, lang) {
+    moment = function (input, format, lang, strict) {
+        if (lang == true) {
+            lang = undefined;
+            strict = true;
+        }
         return makeMoment({
             _i : input,
             _f : format,
             _l : lang,
+            _strict : strict,
             _isUTC : false
         });
     };
 
     // creating with utc
     moment.utc = function (input, format, lang) {
+        if (lang == true) {
+            lang = undefined;
+            strict = true;
+        }
         return makeMoment({
             _useUTC : true,
             _isUTC : true,
             _l : lang,
             _i : input,
-            _f : format
+            _f : format,
+            _strict : strict
         }).utc();
     };
 
index e376d33397d4bd5a7c35a08fc079e5539901cf2a..398a4b46f1d6cb70c30c7c37668afe044c351bcf 100644 (file)
@@ -491,6 +491,12 @@ exports.create = {
         test.done();
     },
 
+    "strict parsing" : function(test) {
+        test.equal(moment("ala [ ] bala 2012-05", "[ala] \\[ \\] \b\a\l\a YYYY-MM").format("YYYY-MM"), "2012-05");
+        test.equal(moment("ala [ bala 2012-05", "[ala] \\[ \\] \b\a\l\a YYYY-MM"), null);
+        test.done();
+    },
+
     "parsing into a language" : function (test) {
         test.expect(2);