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

Refactor parseDate #487

Merged
merged 3 commits into from
Feb 18, 2019
Merged
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
22 changes: 9 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,17 @@ Utils.isDayjs = isDayjs
Utils.wrapper = wrapper

const parseDate = (date) => {
let reg
if (date === null) return new Date(NaN) // Treat null as an invalid date
if (Utils.isUndefined(date)) return new Date()
if (date === null) return new Date(NaN) // null is invalid
if (Utils.isUndefined(date)) return new Date() // today
if (date instanceof Date) return date
// eslint-disable-next-line no-cond-assign
if ((typeof date === 'string')
&& (/.*[^Z]$/i.test(date)) // looking for a better way
&& (reg = date.match(C.REGEX_PARSE))) {
// 2018-08-08 or 20180808
return new Date(
reg[1], reg[2] - 1, reg[3] || 1,
reg[4] || 0, reg[5] || 0, reg[6] || 0, reg[7] || 0
)
if (typeof date === 'string' && !/Z$/i.test(date)) {
const d = date.match(C.REGEX_PARSE)
if (d) {
return new Date(d[1], d[2] - 1, d[3] || 1, d[4] || 0, d[5] || 0, d[6] || 0, d[7] || 0)
}
}
return new Date(date) // timestamp

return new Date(date) // everything else
}

class Dayjs {
Expand Down
36 changes: 35 additions & 1 deletion test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Parse', () => {
expect(dayjs().valueOf()).toBe(moment().valueOf())
})

it('String 20130208', () => {
it('moment-js like formatted dates', () => {
global.console.warn = jest.genMockFunction()// moment.js '2018-4-1 1:1:1:22' will throw warn
let d = '20130108'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf())
Expand Down Expand Up @@ -46,6 +46,40 @@ describe('Parse', () => {
expect(dayjs(time).valueOf()).toBe(moment(time).valueOf())
})

it('String RFC 2822, time and zone', () => {
Copy link
Owner

Choose a reason for hiding this comment

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

The added test suit here seems just test Date object rather dayjs here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just wanted to make that functionality explicit, because right now it is neither documented nor explicitly handled in the tests. Whenever we change our internal implementation we now get feedback whether our assumptions are still valid, right? Will tell you about breaking changes then.

const time = 'Mon, 11 Feb 2019 09:46:50 GMT+1'
const expected = '2019-02-11T08:46:50.000Z'
const d = dayjs(time)
expect(d.toISOString()).toEqual(expected)
expect(d.valueOf()).toBe(moment(time).valueOf())
})

it('String ECMAScript, time and zone', () => {
// should parse dates formatted in ECMA script format
// see https://www.ecma-international.org/ecma-262/9.0/index.html#sec-date.prototype.tostring
const time = 'Mon Feb 11 2019 11:01:37 GMT+0100 (Mitteleuropäische Normalzeit)'
const expected = '2019-02-11T10:01:37.000Z'
const d = dayjs(time)
expect(d.toISOString()).toEqual(expected)
expect(d.valueOf()).toBe(moment(time).valueOf())
})

it('rejects invalid values', () => {
expect(dayjs({}).isValid()).toBe(false)
expect(dayjs(() => '2018-01-01').isValid()).toBe(false)
expect(dayjs(Infinity).isValid()).toBe(false)
expect(dayjs(NaN).isValid()).toBe(false)
expect(dayjs([2018, 5, 1, 13, 52, 44]).isValid()).toBe(false) // Arrays with time part
})

it('parses Arrays with date part', () => {
const dateParts = [2018, 5, 1]
const expected = '2018-05-01T00:00:00.000Z'
const d = dayjs(dateParts)
const normalized = d.add(d.utcOffset(), 'minutes') // make test run in every timezone
expect(normalized.toISOString()).toEqual(expected)
})

it('String Other, Null and isValid', () => {
global.console.warn = jest.genMockFunction()// moment.js otherString will throw warn
expect(dayjs('otherString').toString().toLowerCase()).toBe(moment('otherString').toString().toLowerCase())
Expand Down