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
93 changes: 93 additions & 0 deletions src/plugin/objectSupport/index.js
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,
Copy link
Owner

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.

Copy link
Contributor Author

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 user
d => { 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 lines

Copy link
Owner

@iamkun iamkun Apr 29, 2020

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.

Copy link
Contributor Author

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

Copy link
Owner

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 from this.$utils().p instead of important it directlly.

2 you may have to extend a new prettyUnit function to treat day and date key both mean day-of-the-month. like https://github.com/iamkun/dayjs/blob/dev/src/plugin/duration/index.js#L26

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is that all right?

Copy link
Owner

Choose a reason for hiding this comment

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

cool

Copy link
Contributor Author

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♪(・ω・)ノ

Copy link
Owner

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.

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) {
Copy link
Owner

Choose a reason for hiding this comment

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

seems no need to put it on the proto, just a simple function will be better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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) {
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.

if (number instanceof Object) {
return this.subtractObject(number)
}
return oldSubtract.bind(this)(number, string)
}
}
Loading