]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Handle empty object and empty array for creation as now
authorIskren Chernev <iskren.chernev@gmail.com>
Tue, 14 Jun 2016 08:48:01 +0000 (01:48 -0700)
committerIskren Chernev <iskren.chernev@gmail.com>
Wed, 15 Jun 2016 03:40:15 +0000 (20:40 -0700)
Redo #3111, fix #3085

src/lib/create/from-anything.js
src/lib/utils/is-object-empty.js [new file with mode: 0644]
src/test/moment/now.js

index 190a4f6326c277e039446cd0338a37f05e439331..4615090c960bf8dbddab94ac7d450bdc158128f2 100644 (file)
@@ -1,4 +1,6 @@
 import isArray from '../utils/is-array';
+import isObject from '../utils/is-object';
+import isObjectEmpty from '../utils/is-object-empty';
 import isDate from '../utils/is-date';
 import map from '../utils/map';
 import { createInvalid } from './valid';
@@ -88,6 +90,11 @@ export function createLocalOrUTC (input, format, locale, strict, isUTC) {
         strict = locale;
         locale = undefined;
     }
+
+    if ((isObject(input) && isObjectEmpty(input)) ||
+            (isArray(input) && input.length === 0)) {
+        input = undefined;
+    }
     // object construction must be done this way.
     // https://github.com/moment/moment/issues/1423
     c._isAMomentObject = true;
diff --git a/src/lib/utils/is-object-empty.js b/src/lib/utils/is-object-empty.js
new file mode 100644 (file)
index 0000000..1f2d939
--- /dev/null
@@ -0,0 +1,8 @@
+export default function isObjectEmpty(obj) {
+    var k;
+    for (k in obj) {
+        // even if its not own property I'd still call it non-empty
+        return false;
+    }
+    return true;
+}
index eada0c0cdddf5038f0030fd2cc9a770cae3fd48c..58eb080f5c0e463a03444bc970a845995329047f 100644 (file)
@@ -47,10 +47,24 @@ test('now - custom value', function (assert) {
     };
 
     try {
-        assert.ok(moment().toISOString() === customTimeStr, 'moment() constructor should use the function defined by moment.now, but it did not');
-        assert.ok(moment.utc().toISOString() === customTimeStr, 'moment() constructor should use the function defined by moment.now, but it did not');
-        assert.ok(moment.utc([]).toISOString() === '2015-01-01T00:00:00.000Z', 'moment() constructor should fall back to the date defined by moment.now when an empty array is given, but it did not');
+        assert.equal(moment().toISOString(), customTimeStr, 'moment() constructor should use the function defined by moment.now, but it did not');
+        assert.equal(moment.utc().toISOString(), customTimeStr, 'moment() constructor should use the function defined by moment.now, but it did not');
     } finally {
         moment.now = oldFn;
     }
 });
+
+test('empty object, empty array', function (assert) {
+    function assertIsNow(gen, msg) {
+        var before = +(new Date),
+            mid = gen(),
+            after = +(new Date);
+        assert.ok(before <= +mid && +mid <= after, 'should be now : ' + msg);
+    }
+    assertIsNow(function () { return moment(); }, 'moment()');
+    assertIsNow(function () { return moment([]); }, 'moment([])');
+    assertIsNow(function () { return moment({}); }, 'moment({})');
+    assertIsNow(function () { return moment.utc(); }, 'moment.utc()');
+    assertIsNow(function () { return moment.utc([]); }, 'moment.utc([])');
+    assertIsNow(function () { return moment.utc({}); }, 'moment.utc({})');
+});