From 1840b6c6b57565a6f46c320a258a651215da1c3a Mon Sep 17 00:00:00 2001 From: iamkun Date: Fri, 2 Oct 2020 01:30:37 +0300 Subject: [PATCH] fix: fix objectSupport plugin bug in utc fix #1105 --- src/plugin/objectSupport/index.js | 10 +++++----- test/plugin/objectSupport.test.js | 11 ++++++++--- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/plugin/objectSupport/index.js b/src/plugin/objectSupport/index.js index 6d161b8de..33b5a62da 100644 --- a/src/plugin/objectSupport/index.js +++ b/src/plugin/objectSupport/index.js @@ -1,4 +1,4 @@ -export default (o, c) => { +export default (o, c, dayjs) => { const proto = c.prototype const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array) && obj instanceof Object const prettyUnit = (u) => { @@ -9,13 +9,13 @@ export default (o, c) => { const { date, utc } = cfg const $d = {} if (isObject(date)) { - const now = new Date() + const now = utc ? dayjs.utc() : dayjs() Object.keys(date).forEach((k) => { $d[prettyUnit(k)] = date[k] }) - const d = $d.day || ((!$d.year && !($d.month >= 0)) ? now.getDate() : 1) - const y = $d.year || now.getFullYear() - const M = $d.month >= 0 ? $d.month : ((!$d.year && !$d.day) ? now.getMonth() : 0)// eslint-disable-line no-nested-ternary,max-len + const d = $d.day || ((!$d.year && !($d.month >= 0)) ? now.date() : 1) + const y = $d.year || now.year() + const M = $d.month >= 0 ? $d.month : ((!$d.year && !$d.day) ? now.month() : 0)// eslint-disable-line no-nested-ternary,max-len const h = $d.hour || 0 const m = $d.minute || 0 const s = $d.second || 0 diff --git a/test/plugin/objectSupport.test.js b/test/plugin/objectSupport.test.js index 476b97f4c..392bdbf7c 100755 --- a/test/plugin/objectSupport.test.js +++ b/test/plugin/objectSupport.test.js @@ -21,6 +21,9 @@ const now = new Date() const currentYear = now.getFullYear() const currentMonth = utils.s(now.getMonth() + 1, 2, '0') const currentDate = utils.s(now.getDate(), 2, '0') +const currentUTCYear = now.getUTCFullYear() +const currentUTCMonth = utils.s(now.getUTCMonth() + 1, 2, '0') +const currentUTCDate = utils.s(now.getUTCDate(), 2, '0') const fmt = 'YYYY-MM-DD HH:mm:ss.SSS' const tests = [ [{ year: 2010 }, '2010-01-01 00:00:00.000'], @@ -31,7 +34,8 @@ const tests = [ { hour: 15, minute: 25, second: 50, millisecond: 125 }, - `${currentYear}-${currentMonth}-${currentDate} 15:25:50.125`], + `${currentYear}-${currentMonth}-${currentDate} 15:25:50.125`, + `${currentUTCYear}-${currentUTCMonth}-${currentUTCDate} 15:25:50.125`], [ { year: 2010, month: 1, day: 12, hours: 1 @@ -119,8 +123,9 @@ it('Constructor from Object', () => { it('Constructor from Object UTC', () => { for (let i = 0; i < tests.length; i += 1) { - expect(dayjs.utc(tests[i][0]).format(fmt)).toBe(tests[i][1]) - expect(moment.utc(tests[i][0]).format(fmt)).toBe(tests[i][1]) + const result = tests[i][2] || tests[i][1] + expect(dayjs.utc(tests[i][0]).format(fmt)).toBe(result) + expect(moment.utc(tests[i][0]).format(fmt)).toBe(result) } }) it('Set from Object', () => {