Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Support for strict formatless parsing #4611

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 22 additions & 7 deletions moment.d.ts
@@ -1,4 +1,19 @@
/**
* @param strict Strict parsing disables the deprecated fallback to the native Date constructor when
* parsing a string.
*/
declare function moment(inp?: moment.MomentInput, strict?: boolean): moment.Moment;
/**
* @param strict Strict parsing requires that the format and input match exactly, including delimeters.
* Strict parsing is frequently the best parsing option. For more information about choosing strict vs
* forgiving parsing, see the [parsing guide](https://momentjs.com/guides/#/parsing/).
*/
declare function moment(inp?: moment.MomentInput, format?: moment.MomentFormatSpecification, strict?: boolean): moment.Moment;
/**
* @param strict Strict parsing requires that the format and input match exactly, including delimeters.
* Strict parsing is frequently the best parsing option. For more information about choosing strict vs
* forgiving parsing, see the [parsing guide](https://momentjs.com/guides/#/parsing/).
*/
declare function moment(inp?: moment.MomentInput, format?: moment.MomentFormatSpecification, language?: string, strict?: boolean): moment.Moment;

declare namespace moment {
Expand Down Expand Up @@ -716,16 +731,16 @@ declare namespace moment {

export var defaultFormat: string;
export var defaultFormatUtc: string;
export var HTML5_FMT: {

export var HTML5_FMT: {
DATETIME_LOCAL: string,
DATETIME_LOCAL_SECONDS: string,
DATETIME_LOCAL_MS: string,
DATE: string,
TIME: string,
TIME_SECONDS: string,
TIME_MS: string,
WEEK: string,
DATE: string,
TIME: string,
TIME_SECONDS: string,
TIME_MS: string,
WEEK: string,
MONTH: string
};

Expand Down
5 changes: 5 additions & 0 deletions src/lib/create/from-anything.js
Expand Up @@ -88,6 +88,11 @@ function configFromInput(config) {
export function createLocalOrUTC (input, format, locale, strict, isUTC) {
var c = {};

if (format === true || format === false) {
strict = format;
format = undefined;
}

if (locale === true || locale === false) {
strict = locale;
locale = undefined;
Expand Down
11 changes: 7 additions & 4 deletions src/lib/create/from-string.js
Expand Up @@ -192,10 +192,9 @@ export function configFromRFC2822(config) {
}
}

// date from iso format or fallback
// date from 1) ASP.NET, 2) ISO, 3) RFC 2822 formats, or 4) optional fallback if parsing isn't strict
export function configFromString(config) {
var matched = aspNetJsonRegex.exec(config._i);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pr adds moment support ASP.NET dates.
The variable is setup on line 45

var aspNetJsonRegex = /^/?Date((-?\d+)/i;
It is basically a regex that matches a string like this (random number):
/Date(9897979

Not a maintainer, just adding to the conversation.


if (matched !== null) {
config._d = new Date(+matched[1]);
return;
Expand All @@ -215,8 +214,12 @@ export function configFromString(config) {
return;
}

// Final attempt, use Input Fallback
hooks.createFromInputFallback(config);
if (config._strict) {
config._isValid = false;
} else {
// Final attempt, use Input Fallback
hooks.createFromInputFallback(config);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you enforce _strict for the InputFallback?
Will this cause breaking changes for other users. Who might not be using strict for their moment usage.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have strict, then you'd have a format, and then you wouldn't do the fallback. So it is not breaking for existing users.

}

hooks.createFromInputFallback = deprecate(
Expand Down
8 changes: 8 additions & 0 deletions src/test/moment/create.js
Expand Up @@ -152,6 +152,14 @@ test('string without format - json', function (assert) {
assert.equal(moment('/Date(1325132654000-0700)/').valueOf(), 1325132654000, '/Date(1325132654000-0700)/');
});

test('string without format - strict parsing', function (assert) {
assert.equal(moment('Date(1325132654000)', false).valueOf(), 1325132654000, 'Date(1325132654000)');
assert.equal(moment('Date(1325132654000)', true).valueOf(), 1325132654000, 'Date(1325132654000)');
assert.equal(moment('/Date(1325132654000)/', true).valueOf(), 1325132654000, '/Date(1325132654000)/');
assert.equal(moment('1/1/2001', true).isValid(), false, '1/1/2001');
assert.equal(moment.utc('1/1/2001', true).isValid(), false, '1/1/2001 utc');
});

test('string with format dropped am/pm bug', function (assert) {
moment.locale('en');

Expand Down
2 changes: 2 additions & 0 deletions typing-tests/moment-tests.ts
Expand Up @@ -23,6 +23,8 @@ var day10 = moment([2010, 6, 10]);
var array = [2010, 1, 14, 15, 25, 50, 125];
var day11 = moment(Date.UTC.apply({}, array));
var day12 = moment.unix(1318781876);
var day13 = moment("/Date(1198908717056-0700)/", true);
var day14 = moment("foobazbar", 'L', true);

// TODO: reenable in 2.0
// moment(null);
Expand Down