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/set/subtract support Object parameter #886

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 69 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,47 @@ Utils.l = parseLocale
Utils.i = isDayjs
Utils.w = wrapper

/**
* 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.years,
d.months,
d.date,
d.hours,
d.minutes,
d.seconds,
d.milliseconds
], utc)

const parseDate = (cfg) => {
const { date, utc } = cfg
if (date === null) return new Date(NaN) // null is invalid
if (Utils.u(date)) return new Date() // today
if (date instanceof Date) return new Date(date)
if (Array.isArray(date)) return parseArrayArgument([0, ...date], utc)
if (date instanceof Object) return parseObjectArgument(date, utc)
if (typeof date === 'string' && !/Z$/i.test(date)) {
const d = date.match(C.REGEX_PARSE)
if (d) {
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)
return parseArrayArgument(d, utc)
}
}

Expand Down Expand Up @@ -237,14 +265,29 @@ class Dayjs {
}

set(string, int) {
if (string instanceof Object) {
return this.setObject(string)
}
return this.clone().$set(string, int)
}

setObject(argument) {
const keys = Object.keys(argument)
let chain = this.clone()
keys.forEach((key) => {
chain = chain.$set(key, argument[key])
})
return chain
}

get(unit) {
return this[Utils.p(unit)]()
}

add(number, units) {
if (number instanceof Object) {
return this.addObject(number)
}
number = Number(number) // eslint-disable-line no-param-reassign
const unit = Utils.p(units)
const instanceFactorySet = (n) => {
Expand Down Expand Up @@ -273,10 +316,31 @@ class Dayjs {
return Utils.w(nextTimeStamp, this)
}

addObject(argument) {
const keys = Object.keys(argument)
let chain = this
keys.forEach((key) => {
chain = chain.add(argument[key], key)
})
return chain
}

subtract(number, string) {
if (number instanceof Object) {
return this.subtractObject(number)
}
return this.add(number * -1, string)
}

subtractObject(argument) {
const keys = Object.keys(argument)
let chain = this
keys.forEach((key) => {
chain = chain.subtract(argument[key], key)
})
return chain
}

format(formatStr) {
if (!this.isValid()) return C.INVALID_DATE_STRING

Expand Down