Skip to content

Commit

Permalink
#41 Fixed a bug characters inside square brackets are not validated
Browse files Browse the repository at this point in the history
  • Loading branch information
knowledgecode committed Aug 23, 2020
1 parent 6f726d9 commit 9f7941c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
4 changes: 2 additions & 2 deletions date-and-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
date.preparse = function (dateString, arg) {
var pattern = typeof arg === 'string' ? date.compile(arg) : arg,
dt = { Y: 1970, M: 1, D: 1, H: 0, A: 0, h: 0, m: 0, s: 0, S: 0, Z: 0, _index: 0, _length: 0, _match: 0 },
parser = locales[lang].parser, offset = 0;
comment = /\[(.*)]/, parser = locales[lang].parser, offset = 0;

dateString = parser.pre(dateString);
for (var i = 1, len = pattern.length, token, result; i < len; i++) {
Expand All @@ -188,7 +188,7 @@
dt._match++;
} else if (token === dateString.charAt(offset) || token === ' ') {
offset++;
} else if (/\[.*]/.test(token)) {
} else if (comment.test(token) && !dateString.slice(offset).indexOf(comment.exec(token)[1])) {
offset += token.length - 2;
} else if (token === '...') {
offset = dateString.length;
Expand Down
18 changes: 9 additions & 9 deletions date-and-time.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -945,12 +945,24 @@
var dt = { Y: 2015, M: 1, D: 1, H: 0, A: 0, h: 0, m: 0, s: 0, S: 0, Z: 0, _index: 4, _length: 21, _match: 1 };
expect(date.preparse('2015-12-31 23:59:59.9', 'YYYY?M?D H?m?s?S')).to.eql(dt);
});
it('YYYY-MM-DD HH:mm:ssZ', function () {
var dt = { Y: 2015, M: 12, D: 31, H: 23, A: 0, h: 0, m: 59, s: 59, S: 0, Z: 0, _index: 19, _length: 20, _match: 6 };
expect(date.preparse('2015-12-31 23:59:59K', 'YYYY-MM-DD HH:mm:ss[Z]')).to.eql(dt);
});
it('[Y]YYYY[M]M[D]D[H]H[m]m[s]s[S]S', function () {
var dt = { Y: 2015, M: 12, D: 31, H: 23, A: 0, h: 0, m: 59, s: 59, S: 900, Z: 0, _index: 22, _length: 22, _match: 7 };
expect(date.preparse('Y2015M12D31H23m59s59S9', '[Y]YYYY[M]M[D]D[H]H[m]m[s]s[S]S')).to.eql(dt);
});
it('[Y]YYYY[M]M[D]D[H]H[m]m[s]s[S]S', function () {
var dt = { Y: 2015, M: 12, D: 31, H: 0, A: 0, h: 0, m: 0, s: 0, S: 0, Z: 0, _index: 22, _length: 22, _match: 3 };
expect(date.preparse('Y2015M12D31H23m59s59S9', '[Y]YYYY[M]M[D]D...')).to.eql(dt);
});
it('[[Y]YYYY[M]MM[D]DD[H]HH[m]mm[s]ss[S]S]', function () {
var dt = { Y: 1970, M: 1, D: 1, H: 0, A: 0, h: 0, m: 0, s: 0, S: 0, Z: 0, _index: 36, _length: 36, _match: 0 };
expect(date.preparse('[Y]YYYY[M]MM[D]DD[H]HH[m]mm[s]ss[S]S', '[[Y]YYYY[M]MM[D]DD[H]HH[m]mm[s]ss[S]S]')).to.eql(dt);
});
it('[[Y]YYYY[M]MM[D]DD[H]HH[m]mm[s]ss[S]S]', function () {
var dt = { Y: 1970, M: 1, D: 1, H: 0, A: 0, h: 0, m: 0, s: 0, S: 0, Z: 0, _index: 0, _length: 36, _match: 0 };
expect(date.preparse('[Y]2015[M]12[D]31[H]23[m]59[s]59[S]9', '[[Y]YYYY[M]MM[D]DD[H]HH[m]mm[s]ss[S]S]')).to.eql(dt);
});
it(' ', function () {
Expand Down

0 comments on commit 9f7941c

Please sign in to comment.