Skip to content

Commit

Permalink
Merge pull request #5592 from Alanscut:issue_5314
Browse files Browse the repository at this point in the history
[bugfix] Fix the issue of  `isValid()` in strict mode
  • Loading branch information
ichernev committed Dec 23, 2023
2 parents a3babc5 + 6367947 commit cbcd0c5
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 18 deletions.
4 changes: 4 additions & 0 deletions src/lib/parse/regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ var match1 = /\d/, // 0 - 9
// includes scottish gaelic two word and hyphenated months
matchWord =
/[0-9]{0,256}['a-z\u00A0-\u05FF\u0700-\uD7FF\uF900-\uFDCF\uFDF0-\uFF07\uFF10-\uFFEF]{1,256}|[\u0600-\u06FF\/]{1,256}(\s*?[\u0600-\u06FF]{1,256}){1,2}/i,
match1to2NoLeadingZero = /^[1-9]\d?/, // 1-99
match1to2HasZero = /^([1-9]\d|\d)/, // 0-99
regexes;

export {
Expand All @@ -38,6 +40,8 @@ export {
matchShortOffset,
matchTimestamp,
matchWord,
match1to2NoLeadingZero,
match1to2HasZero,
};

import hasOwnProp from '../utils/has-own-prop';
Expand Down
9 changes: 7 additions & 2 deletions src/lib/units/day-of-month.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { makeGetSet } from '../moment/get-set';
import { addFormatToken } from '../format/format';
import { addRegexToken, match1to2, match2 } from '../parse/regex';
import {
addRegexToken,
match1to2,
match2,
match1to2NoLeadingZero,
} from '../parse/regex';
import { addParseToken } from '../parse/token';
import { DATE } from './constants';
import toInt from '../utils/to-int';
Expand All @@ -11,7 +16,7 @@ addFormatToken('D', ['DD', 2], 'Do', 'date');

// PARSING

addRegexToken('D', match1to2);
addRegexToken('D', match1to2, match1to2NoLeadingZero);
addRegexToken('DD', match1to2, match2);
addRegexToken('Do', function (isStrict, locale) {
// TODO: Remove "ordinalParse" fallback in next major release.
Expand Down
8 changes: 5 additions & 3 deletions src/lib/units/hour.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
match2,
match3to4,
match5to6,
match1to2NoLeadingZero,
match1to2HasZero,
} from '../parse/regex';
import { addParseToken } from '../parse/token';
import { HOUR, MINUTE, SECOND } from './constants';
Expand Down Expand Up @@ -74,9 +76,9 @@ function matchMeridiem(isStrict, locale) {

addRegexToken('a', matchMeridiem);
addRegexToken('A', matchMeridiem);
addRegexToken('H', match1to2);
addRegexToken('h', match1to2);
addRegexToken('k', match1to2);
addRegexToken('H', match1to2, match1to2HasZero);
addRegexToken('h', match1to2, match1to2NoLeadingZero);
addRegexToken('k', match1to2, match1to2NoLeadingZero);
addRegexToken('HH', match1to2, match2);
addRegexToken('hh', match1to2, match2);
addRegexToken('kk', match1to2, match2);
Expand Down
9 changes: 7 additions & 2 deletions src/lib/units/minute.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { makeGetSet } from '../moment/get-set';
import { addFormatToken } from '../format/format';
import { addRegexToken, match1to2, match2 } from '../parse/regex';
import {
addRegexToken,
match1to2,
match2,
match1to2HasZero,
} from '../parse/regex';
import { addParseToken } from '../parse/token';
import { MINUTE } from './constants';

Expand All @@ -10,7 +15,7 @@ addFormatToken('m', ['mm', 2], 0, 'minute');

// PARSING

addRegexToken('m', match1to2);
addRegexToken('m', match1to2, match1to2HasZero);
addRegexToken('mm', match1to2, match2);
addParseToken(['m', 'mm'], MINUTE);

Expand Down
3 changes: 2 additions & 1 deletion src/lib/units/month.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
match2,
matchWord,
regexEscape,
match1to2NoLeadingZero,
} from '../parse/regex';
import { addParseToken } from '../parse/token';
import { hooks } from '../utils/hooks';
Expand Down Expand Up @@ -49,7 +50,7 @@ addFormatToken('MMMM', 0, 0, function (format) {

// PARSING

addRegexToken('M', match1to2);
addRegexToken('M', match1to2, match1to2NoLeadingZero);
addRegexToken('MM', match1to2, match2);
addRegexToken('MMM', function (isStrict, locale) {
return locale.monthsShortRegex(isStrict);
Expand Down
9 changes: 7 additions & 2 deletions src/lib/units/second.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { makeGetSet } from '../moment/get-set';
import { addFormatToken } from '../format/format';
import { addRegexToken, match1to2, match2 } from '../parse/regex';
import {
addRegexToken,
match1to2,
match2,
match1to2HasZero,
} from '../parse/regex';
import { addParseToken } from '../parse/token';
import { SECOND } from './constants';

Expand All @@ -10,7 +15,7 @@ addFormatToken('s', ['ss', 2], 0, 'second');

// PARSING

addRegexToken('s', match1to2);
addRegexToken('s', match1to2, match1to2HasZero);
addRegexToken('ss', match1to2, match2);
addParseToken(['s', 'ss'], SECOND);

Expand Down
11 changes: 8 additions & 3 deletions src/lib/units/week.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { addFormatToken } from '../format/format';
import { addRegexToken, match1to2, match2 } from '../parse/regex';
import {
addRegexToken,
match1to2,
match2,
match1to2NoLeadingZero,
} from '../parse/regex';
import { addWeekParseToken } from '../parse/token';
import toInt from '../utils/to-int';
import { weekOfYear } from './week-calendar-utils';
Expand All @@ -11,9 +16,9 @@ addFormatToken('W', ['WW', 2], 'Wo', 'isoWeek');

// PARSING

addRegexToken('w', match1to2);
addRegexToken('w', match1to2, match1to2NoLeadingZero);
addRegexToken('ww', match1to2, match2);
addRegexToken('W', match1to2);
addRegexToken('W', match1to2, match1to2NoLeadingZero);
addRegexToken('WW', match1to2, match2);

addWeekParseToken(
Expand Down
2 changes: 1 addition & 1 deletion src/test/locale/es-us.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ test('test short months proper', function (assert) {

test('test lenient month parsing', function (assert) {
assert.ok(
moment('nov 01, 2015', 'MMM D, YYYY', true).isValid(),
moment('nov 01, 2015', 'MMM DD, YYYY', true).isValid(),
'nov 01, 2015 should parse correctly'
);
});
116 changes: 112 additions & 4 deletions src/test/moment/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -2192,8 +2192,8 @@ test('strict parsing', function (assert) {
);
assert.equal(
moment('2012-05-25', 'YYYY-M-DD', true).isValid(),
true,
'valid one-digit month'
false,
'isValid one-digit month'
);
assert.equal(
moment('2012-05-25', 'YYYY-MM-DD', true).isValid(),
Expand All @@ -2213,8 +2213,8 @@ test('strict parsing', function (assert) {
);
assert.equal(
moment('2012-05-02', 'YYYY-MM-D', true).isValid(),
true,
'valid two-digit day'
false,
'isValid two-digit day'
);
assert.equal(
moment('2012-05-02', 'YYYY-MM-DD', true).isValid(),
Expand Down Expand Up @@ -2250,6 +2250,114 @@ test('strict parsing', function (assert) {
'invalid three-digit milisecond'
);

assert.equal(
moment('2002-05-02 09:30:26', 'YYYY-MM-DD H:mm:ss', true).isValid(),
false,
'invalid two-digit hour'
);

assert.equal(
moment('2002-05-02 11:30:26', 'YYYY-MM-DD H:mm:ss', true).isValid(),
true,
'valid two-digit hour'
);

assert.equal(
moment('2002-05-02 09:30:26', 'YYYY-MM-DD h:mm:ss', true).isValid(),
false,
'invalid two-digit hour'
);

assert.equal(
moment('2002-05-02 11:30:26', 'YYYY-MM-DD h:mm:ss', true).isValid(),
true,
'valid two-digit hour'
);

assert.equal(
moment('2002-05-02 09:30:26', 'YYYY-MM-DD k:mm:ss', true).isValid(),
false,
'invalid two-digit hour'
);

assert.equal(
moment('2002-05-02 11:30:26', 'YYYY-MM-DD k:mm:ss', true).isValid(),
true,
'valid two-digit hour'
);

assert.equal(
moment('2002-05-02 11:09:26', 'YYYY-MM-DD hh:m:ss', true).isValid(),
false,
'invalid two-digit minute'
);

assert.equal(
moment('2002-05-02 11:12:26', 'YYYY-MM-DD hh:m:ss', true).isValid(),
true,
'valid two-digit minute'
);

assert.equal(
moment('2002-05-02 11:09:06', 'YYYY-MM-DD hh:mm:s', true).isValid(),
false,
'invalid two-digit second'
);

assert.equal(
moment('2002-05-02 11:09:16', 'YYYY-MM-DD hh:mm:s', true).isValid(),
true,
'valid two-digit second'
);

assert.equal(
moment('2012-W07', 'YYYY-[W]W', true).isValid(),
false,
'invalid two-digit week'
);

assert.equal(
moment('2012-W17', 'YYYY-[W]W', true).isValid(),
true,
'valid two-digit week'
);

assert.equal(
moment('2012-W07', 'YYYY-[W]w', true).isValid(),
false,
'invalid two-digit week'
);

assert.equal(
moment('2012-W17', 'YYYY-[W]w', true).isValid(),
true,
'valid two-digit week'
);

assert.equal(
moment('08 June 2012', ['D MMMM YYYY'], true).isValid(),
false,
'invalid two-digit day'
);

assert.equal(
moment('18 June 2012', ['D MMMM YYYY'], true).isValid(),
true,
'valid two-digit day'
);

assert.equal(
moment('2012-05-02', 'YYYY-M-DD', true).isValid(),
false,
'invalid two-digit month'
);

assert.equal(
moment('2012-11-02', 'YYYY-M-DD', true).isValid(),
true,
'valid two-digit month'
);

assert.equal(
moment('1', 'SS', true).isValid(),
false,
Expand Down

0 comments on commit cbcd0c5

Please sign in to comment.