From: Iskren Chernev Date: Tue, 14 Jun 2016 08:48:01 +0000 (-0700) Subject: Handle empty object and empty array for creation as now X-Git-Tag: 2.14.0~17^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=76fea370307d7091b06e8a34d4a6c219fdb87dae;p=thirdparty%2Fmoment.git Handle empty object and empty array for creation as now Redo #3111, fix #3085 --- diff --git a/src/lib/create/from-anything.js b/src/lib/create/from-anything.js index 190a4f632..4615090c9 100644 --- a/src/lib/create/from-anything.js +++ b/src/lib/create/from-anything.js @@ -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 index 000000000..1f2d939a5 --- /dev/null +++ b/src/lib/utils/is-object-empty.js @@ -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; +} diff --git a/src/test/moment/now.js b/src/test/moment/now.js index eada0c0cd..58eb080f5 100644 --- a/src/test/moment/now.js +++ b/src/test/moment/now.js @@ -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({})'); +});