-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Changes from 10 commits
954db86
89596ca
5963241
9e8fef1
1487db5
fe7fb05
7d7db50
60f59be
460e312
fa8528d
b9f7f46
46ecb61
3c51c1c
83f3206
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
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) { | ||
// console.log(cfg) | ||
cfg.date = parseDate.bind(this)(cfg) | ||
oldParse.bind(this)(cfg) | ||
} | ||
|
||
const setObject = function (argument) { | ||
const keys = Object.keys(argument) | ||
let chain = this.clone() | ||
keys.forEach((key) => { | ||
chain = chain.$set(key, argument[key]) | ||
}) | ||
return chain | ||
} | ||
const addObject = function (argument) { | ||
const keys = Object.keys(argument) | ||
let chain = this | ||
keys.forEach((key) => { | ||
chain = chain.add(argument[key], key) | ||
}) | ||
return chain | ||
} | ||
|
||
const subtractObject = function (argument) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these three functions could be combined into one function with different arguments |
||
const keys = Object.keys(argument) | ||
let chain = this | ||
keys.forEach((key) => { | ||
chain = chain.subtract(argument[key], key) | ||
}) | ||
return chain | ||
} | ||
|
||
const oldSet = proto.set | ||
proto.set = function (string, int) { | ||
if (string instanceof Object) { | ||
return setObject.bind(this)(string) | ||
} | ||
return oldSet.bind(this)(string, int) | ||
} | ||
const oldAdd = proto.add | ||
proto.add = function (number, string) { | ||
if (number instanceof Object) { | ||
return addObject.bind(this)(number) | ||
} | ||
return oldAdd.bind(this)(number, string) | ||
} | ||
const oldSubtract = proto.subtract | ||
proto.subtract = function (number, string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the three methods have been merged into one. |
||
if (number instanceof Object) { | ||
return subtractObject.bind(this)(number) | ||
} | ||
return oldSubtract.bind(this)(number, string) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no test code, pls.