From 153e70475fa09ed3c3a353fa6e1ed53b17ec14ed Mon Sep 17 00:00:00 2001 From: Bernhard Pottler Date: Wed, 24 Aug 2022 08:00:20 +0200 Subject: [PATCH] fix: make objectSupport ignore other object types (issue #2027) while fixing, an issue wit the handling of subtract appeared and was fixed too. --- src/plugin/objectSupport/index.js | 46 ++++++++++++++++++------------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/src/plugin/objectSupport/index.js b/src/plugin/objectSupport/index.js index 4344dc402..650166606 100644 --- a/src/plugin/objectSupport/index.js +++ b/src/plugin/objectSupport/index.js @@ -1,6 +1,7 @@ export default (o, c, dayjs) => { const proto = c.prototype - const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array) && obj instanceof Object + const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array) + && !proto.$utils().u(obj) && (obj.constructor.name === 'Object') const prettyUnit = (u) => { const unit = proto.$utils().p(u) return unit === 'date' ? 'day' : unit @@ -39,29 +40,36 @@ export default (o, c, dayjs) => { const oldSet = proto.set const oldAdd = proto.add + const oldSubtract = proto.subtract const callObject = function (call, argument, string, offset = 1) { - if (argument instanceof Object) { - const keys = Object.keys(argument) - let chain = this - keys.forEach((key) => { - chain = call.bind(chain)(argument[key] * offset, key) - }) - return chain - } - return call.bind(this)(argument * offset, string) + const keys = Object.keys(argument) + let chain = this + keys.forEach((key) => { + chain = call.bind(chain)(argument[key] * offset, key) + }) + return chain } - proto.set = function (string, int) { - int = int === undefined ? string : int - return callObject.bind(this)(function (i, s) { - return oldSet.bind(this)(s, i) - }, int, string) + proto.set = function (unit, value) { + value = value === undefined ? unit : value + if (unit.constructor.name === 'Object') { + return callObject.bind(this)(function (i, s) { + return oldSet.bind(this)(s, i) + }, value, unit) + } + return oldSet.bind(this)(unit, value) } - proto.add = function (number, string) { - return callObject.bind(this)(oldAdd, number, string) + proto.add = function (value, unit) { + if (value.constructor.name === 'Object') { + return callObject.bind(this)(oldAdd, value, unit) + } + return oldAdd.bind(this)(value, unit) } - proto.subtract = function (number, string) { - return callObject.bind(this)(oldAdd, number, string, -1) + proto.subtract = function (value, unit) { + if (value.constructor.name === 'Object') { + return callObject.bind(this)(oldAdd, value, unit, -1) + } + return oldSubtract.bind(this)(value, unit) } }