|
1 | 1 | export default (o, c, dayjs) => { |
2 | 2 | const proto = c.prototype |
3 | | - const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array) && obj instanceof Object |
| 3 | + const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array) |
| 4 | + && !proto.$utils().u(obj) && (obj.constructor.name === 'Object') |
4 | 5 | const prettyUnit = (u) => { |
5 | 6 | const unit = proto.$utils().p(u) |
6 | 7 | return unit === 'date' ? 'day' : unit |
@@ -39,29 +40,36 @@ export default (o, c, dayjs) => { |
39 | 40 |
|
40 | 41 | const oldSet = proto.set |
41 | 42 | const oldAdd = proto.add |
| 43 | + const oldSubtract = proto.subtract |
42 | 44 |
|
43 | 45 | const callObject = function (call, argument, string, offset = 1) { |
44 | | - if (argument instanceof Object) { |
45 | | - const keys = Object.keys(argument) |
46 | | - let chain = this |
47 | | - keys.forEach((key) => { |
48 | | - chain = call.bind(chain)(argument[key] * offset, key) |
49 | | - }) |
50 | | - return chain |
51 | | - } |
52 | | - return call.bind(this)(argument * offset, string) |
| 46 | + const keys = Object.keys(argument) |
| 47 | + let chain = this |
| 48 | + keys.forEach((key) => { |
| 49 | + chain = call.bind(chain)(argument[key] * offset, key) |
| 50 | + }) |
| 51 | + return chain |
53 | 52 | } |
54 | 53 |
|
55 | | - proto.set = function (string, int) { |
56 | | - int = int === undefined ? string : int |
57 | | - return callObject.bind(this)(function (i, s) { |
58 | | - return oldSet.bind(this)(s, i) |
59 | | - }, int, string) |
| 54 | + proto.set = function (unit, value) { |
| 55 | + value = value === undefined ? unit : value |
| 56 | + if (unit.constructor.name === 'Object') { |
| 57 | + return callObject.bind(this)(function (i, s) { |
| 58 | + return oldSet.bind(this)(s, i) |
| 59 | + }, value, unit) |
| 60 | + } |
| 61 | + return oldSet.bind(this)(unit, value) |
60 | 62 | } |
61 | | - proto.add = function (number, string) { |
62 | | - return callObject.bind(this)(oldAdd, number, string) |
| 63 | + proto.add = function (value, unit) { |
| 64 | + if (value.constructor.name === 'Object') { |
| 65 | + return callObject.bind(this)(oldAdd, value, unit) |
| 66 | + } |
| 67 | + return oldAdd.bind(this)(value, unit) |
63 | 68 | } |
64 | | - proto.subtract = function (number, string) { |
65 | | - return callObject.bind(this)(oldAdd, number, string, -1) |
| 69 | + proto.subtract = function (value, unit) { |
| 70 | + if (value.constructor.name === 'Object') { |
| 71 | + return callObject.bind(this)(oldAdd, value, unit, -1) |
| 72 | + } |
| 73 | + return oldSubtract.bind(this)(value, unit) |
66 | 74 | } |
67 | 75 | } |
0 commit comments