tokens = config._f.match(formattingTokens),
match, i, tokenIndex;
- // 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) {
tokenRegexp = getParseRegexForToken(token).toString();
// Do not remember groups
tokenRegexp = tokenRegexp.replace(/\(/g, '(?:');
- // this is a real token
// regexp-escape strings in-between tokens
if (offset > non_token_start) {
}
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);
match = config._i.match(regexp);
- console.log(match);
if (match === null) {
+ config._isValid = false;
return null;
}
}
}
- console.log(config._a);
dateFromArray(config);
}
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;
}
},
"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.expect(4);
+ test.equal(moment("2012-05", "YYYY-MM", true).format("YYYY-MM"), "2012-05", "parse correct string");
+ test.equal(moment(" 2012-05", "YYYY-MM", true).isValid(), false, "fail on extra whitespace");
+ test.equal(moment("foo 2012-05", "[foo] YYYY-MM", true).format('YYYY-MM'), "2012-05", "handle fixed text");
+ test.equal(moment("2012 05", "YYYY-MM", true).isValid(), false, "fail on different separator");
test.done();
},