-
-
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 1 commit
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,93 @@ | ||
export default (o, c) => { | ||
const proto = c.prototype | ||
|
||
/** | ||
* Array to date | ||
* @param d | ||
* @param utc | ||
* @returns {Date} | ||
*/ | ||
const parseArrayArgument = (d, utc) => { | ||
if (utc) { | ||
return new Date(Date.UTC(d[1], d[2] - 1, d[3] | ||
|| 1, d[4] || 0, d[5] || 0, d[6] || 0, d[7] || 0)) | ||
} | ||
return new Date(d[1], d[2] - 1, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, d[7] || 0) | ||
} | ||
|
||
/** | ||
* Converts the object to a date array | ||
* @param d Object { years, months, date, hours, minutes, seconds, milliseconds} | ||
* @param utc | ||
*/ | ||
const parseObjectArgument = (d, utc) => parseArrayArgument([ | ||
0, | ||
d.y || d.year || d.years || 1970, | ||
d.M || d.month || d.months || 1, | ||
d.d || d.day || d.days || d.date || 1, | ||
d.h || d.hour || d.hours || 0, | ||
d.m || d.minute || d.minutes || 0, | ||
d.s || d.second || d.seconds || 0, | ||
d.ms || d.millisecond || d.milliseconds || 0 | ||
], utc) | ||
|
||
const isObject = obj => !(obj instanceof Date) && !(obj instanceof Array) && obj instanceof Object | ||
const parseDate = (cfg) => { | ||
const { date, utc } = cfg | ||
if (isObject(date)) return parseObjectArgument(date, utc) | ||
return date | ||
} | ||
|
||
const oldParse = proto.parse | ||
proto.parse = function (cfg) { | ||
cfg.date = parseDate(cfg) | ||
oldParse.bind(this)(cfg) | ||
} | ||
|
||
proto.setObject = 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. seems no need to put it on the 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. you are right. |
||
const keys = Object.keys(argument) | ||
let chain = this.clone() | ||
keys.forEach((key) => { | ||
chain = chain.$set(key, argument[key]) | ||
}) | ||
return chain | ||
} | ||
const oldSet = proto.set | ||
proto.set = function (string, int) { | ||
if (string instanceof Object) { | ||
return this.setObject(string) | ||
} | ||
return oldSet.bind(this)(string, int) | ||
} | ||
|
||
proto.addObject = function (argument) { | ||
const keys = Object.keys(argument) | ||
let chain = this | ||
keys.forEach((key) => { | ||
chain = chain.add(argument[key], key) | ||
}) | ||
return chain | ||
} | ||
const oldAdd = proto.add | ||
proto.add = function (number, string) { | ||
if (number instanceof Object) { | ||
return this.addObject(number) | ||
} | ||
return oldAdd.bind(this)(number, string) | ||
} | ||
proto.subtractObject = function (argument) { | ||
const keys = Object.keys(argument) | ||
let chain = this | ||
keys.forEach((key) => { | ||
chain = chain.subtract(argument[key], key) | ||
}) | ||
return chain | ||
} | ||
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 this.subtractObject(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.
check our
this.$utils().p()
prettyUnit to better handle this.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.
Their role is different,
d
is input by the userd => { years: 2010, months: 1, days: 14, hours: 15, minutes: 25, seconds: 50, milliseconds: 125 }
or
d => { y: 2010, M: 1, d: 14, h: 15, m: 25, s: 50, ms: 125 }
I don't have to judge units, just need to get the value of some units, just like the REGEX_PARSE in constant.js.
This code is similar to the part in index.js where parseDate handles strings.
const d = date.match(C.REGEX_PARSE)
In the index.js 60 linesThere 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.
https://github.com/iamkun/dayjs/blob/dev/src/plugin/duration/index.js#L42
Something like this to process all kinds of user input.
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.
Is that right? But it doesn't support date->
dayjs({date:1})
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.
1 you can get
prettyUnit
fromthis.$utils().p
instead of important it directlly.2 you may have to extend a new
prettyUnit
function to treatday and date key both mean day-of-the-month.
like https://github.com/iamkun/dayjs/blob/dev/src/plugin/duration/index.js#L26There 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.
Is that all right?
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.
cool
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.
Thank you, seniors, let me learn a lot.
This is my first PR. Thanks♪(・ω・)ノ
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.
You are most welcome, cheers.