]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
[feature] Support for `moment(String, true)`
authorGeorgii Dolzhykov <thorn.mailbox@gmail.com>
Fri, 4 May 2018 16:16:21 +0000 (19:16 +0300)
committerIskren Chernev <iskren.chernev@gmail.com>
Mon, 27 Apr 2020 00:27:37 +0000 (03:27 +0300)
Closes #2469

moment.d.ts
src/lib/create/from-anything.js
src/lib/create/from-string.js
src/test/moment/create.js
typing-tests/moment-tests.ts

index 78a76b2adcf63d8e7dacd2269f933ea4d0734cdf..c676fad37bf904e3f44358aa3aca12107a954248 100644 (file)
@@ -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 {
index f501de6c55d4e4935f3c403d8478ddd129f80b66..0c5d772364e1e46d36ff4302691270f6120d35a3 100644 (file)
@@ -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;
index 99a7fe0aa0421f0058d6e8074eaeeca20e700908..dd1ff9d9f88f5c643caedacdd86cc1c529fc7ba8 100644 (file)
@@ -214,10 +214,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);
-
     if (matched !== null) {
         config._d = new Date(+matched[1]);
         return;
@@ -237,8 +236,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);
+    }
 }
 
 hooks.createFromInputFallback = deprecate(
index 9be49fef06642c8876a37ed1d141206c60c52a82..d2966323b2de22efb6d30f73d0cd3cee1418aa06 100644 (file)
@@ -347,6 +347,26 @@ test('string without format - json', function (assert) {
     );
 });
 
+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');
 
index 6d4d1635f55be833140738b3862aefb993d7ba6f..41cff85cf61ec205d304c48b158be54ce0cd5f04 100644 (file)
@@ -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);