Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:add plugin objectSupport #887

Merged
merged 14 commits into from
Apr 30, 2020
63 changes: 63 additions & 0 deletions src/plugin/objectSupport/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
export default (o, c) => {
const proto = c.prototype
const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array) && obj instanceof Object
const prettyUnit = (u) => {
const unit = proto.$utils().p(u)
return unit === 'date' ? 'day' : unit
}
const parseDate = (cfg) => {
const { date, utc } = cfg
const $d = {}
if (isObject(date)) {
Object.keys(date).forEach((k) => {
$d[prettyUnit(k)] = date[k]
})
const y = $d.year || 1970
const M = $d.month - 1 || 0
const d = $d.day || 1
const h = $d.hour || 0
const m = $d.minute || 0
const s = $d.second || 0
const ms = $d.millisecond || 0
if (utc) {
return new Date(Date.UTC(y, M, d, h, m, s, ms))
}
return new Date(y, M, d, h, m, s, ms)
}
return date
}

const oldParse = proto.parse
proto.parse = function (cfg) {
cfg.date = parseDate.bind(this)(cfg)
oldParse.bind(this)(cfg)
}

const oldSet = function (int, string) {
return this.clone().$set(string, int)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why a copy-paste instead of proto.set

Copy link
Contributor Author

@zerooverture zerooverture Apr 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here I've changed the set to have the same parameter location as add, otherwise you have to judge in callObject, which is inefficient and increases the package size.
or set and add can't share a callObject.

also, my attempt to use proto.set in oldSet will cause a dead loop because proto.set is redirected.
Using 'proto.$set' has the problem of the 'this' scope.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, why useing return proto.set will cause a dead loop?

Copy link
Contributor Author

@zerooverture zerooverture Apr 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const oldSet = function (int, string) {
    return proto.set(string, int)
  }
...
proto.set = function (string, int) {
...
}

his code causes oldSet to enter the newly defined proto.set each time, So it creates a dead loop.

In the new code, I direct used a callback to solve this pointing problem.

}
const oldAdd = proto.add

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)
}

proto.set = function (string, int) {
int = int === undefined ? string : int
return callObject.bind(this)(oldSet, int, string)
}
proto.add = function (number, string) {
return callObject.bind(this)(oldAdd, number, string)
}
proto.subtract = function (number, string) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

substract could be implemented as negative add, subtract { day: 1 } => add { day: 1 * -1 }, so that we could only care add and set logic

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the three methods have been merged into one.

return callObject.bind(this)(oldAdd, number, string, -1)
}
}
Loading