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

Fix: Error in parsing array format date #2639

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
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
30 changes: 22 additions & 8 deletions src/plugin/arraySupport/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
export default (o, c, dayjs) => {
export default (o, c) => {
const proto = c.prototype

const getStringDate = (date) => {
const makeTwoDigit = (value) => {
if ((value % 10) === 1) {
return `0${value}`
}
return value
}
// Date part YYYY, MM, DD
const dP = date.slice(0, 3).map(makeTwoDigit).join('-')
// Time part HH:MM:SS
const tP = date.length > 5 ? date.slice(3, date.length - 1).join(':') : date[3]
// Zone Part
const zP = date[7]
return `${dP}${tP ? `T${tP}` : ''}${zP ? `.${zP}Z` : ''}`
}

const parseDate = (cfg) => {
const { date, utc } = cfg
if (Array.isArray(date)) {
if (!date.length) {
return new Date()
}
if (utc) {
if (!date.length) {
return new Date()
}
return new Date(Date.UTC.apply(null, date))
}
if (date.length === 1) {
return dayjs(String(date[0])).toDate()
}
return new (Function.prototype.bind.apply(Date, [null].concat(date)))()
return getStringDate(date)
}
return date
}
Expand Down
26 changes: 20 additions & 6 deletions test/plugin/arraySupport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,26 @@ const testArrs = [
[2010, 6, 10]
]

describe('parse array local', () => {
testArrs.forEach((testArr) => {
it(testArr, () => {
expect(dayjs(testArr).format())
.toBe(moment(testArr).format())
Copy link
Owner

Choose a reason for hiding this comment

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

we should keep the same return with moment.js. otherwise, it will be a huge breaking change.

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 fact is the same bug is present in « moment ». JavaScript Date parses month number from zero to eleven. But most of the time if not all time, we use month from 1 to 12. Excepting the same result as moment will be impossible unless moment fix it. Another alternative is to update the docs and let users know that they need to maybe change their habits and consider zero as first month and 11 as the last month in order to properly use arraySupport plugin. What would you suggest?

})
describe('parse full array local', () => {
it('parse and format full date array', () => {
expect(dayjs([2010, 1, 14, 15, 25, 50, 125]).format())
.toBe('2010-01-14T15:25:50+00:00')
})

it('parse and format a date array that contain only the year', () => {
expect(dayjs([2010]).format())
.toBe('2010-01-01T00:00:00+00:00')
})

it('parse and format a date array that contain only the year and the month', () => {
expect(dayjs([2010, 6]).format())
.toBe('2010-06-01T00:00:00+00:00')
})


it('parse and format a date array that contain a date without a time', () => {
expect(dayjs([2010, 6, 10]).format())
.toBe('2010-06-10T00:00:00+00:00')
})
})

Expand Down
Loading