Skip to content

Commit

Permalink
Merge aa19bbc into 528ac2b
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Wang committed Jun 11, 2020
2 parents 528ac2b + aa19bbc commit 8c00287
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 @@ -17,6 +17,8 @@ var match1 = /\d/, // 0 - 9
// any word (or two) characters or numbers including two/three word month in arabic.
// 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 @@ -37,6 +39,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
Expand Up @@ -2,7 +2,12 @@ import { makeGetSet } from '../moment/get-set';
import { addFormatToken } from '../format/format';
import { addUnitAlias } from './aliases';
import { addUnitPriority } from './priorities';
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 @@ -20,7 +25,7 @@ addUnitPriority('date', 9);

// 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 @@ -8,6 +8,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 @@ -83,9 +85,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
Expand Up @@ -2,7 +2,12 @@ import { makeGetSet } from '../moment/get-set';
import { addFormatToken } from '../format/format';
import { addUnitAlias } from './aliases';
import { addUnitPriority } from './priorities';
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 @@ -20,7 +25,7 @@ addUnitPriority('minute', 14);

// 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 @@ -9,6 +9,7 @@ import {
match2,
matchWord,
regexEscape,
match1to2NoLeadingZero,
} from '../parse/regex';
import { addParseToken } from '../parse/token';
import { hooks } from '../utils/hooks';
Expand Down Expand Up @@ -59,7 +60,7 @@ addUnitPriority('month', 8);

// 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
Expand Up @@ -2,7 +2,12 @@ import { makeGetSet } from '../moment/get-set';
import { addFormatToken } from '../format/format';
import { addUnitAlias } from './aliases';
import { addUnitPriority } from './priorities';
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 @@ -20,7 +25,7 @@ addUnitPriority('second', 15);

// 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,7 +1,12 @@
import { addFormatToken } from '../format/format';
import { addUnitAlias } from './aliases';
import { addUnitPriority } from './priorities';
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 @@ -23,9 +28,9 @@ addUnitPriority('isoWeek', 5);

// PARSING

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

addWeekParseToken(['w', 'ww', 'W', 'WW'], function (
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 @@ -483,7 +483,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 @@ -2199,8 +2199,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 @@ -2220,8 +2220,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 @@ -2257,6 +2257,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 8c00287

Please sign in to comment.