Skip to content

Commit

Permalink
Merge pull request #117 from xx45/feature/iamkun
Browse files Browse the repository at this point in the history
Bug fix and chore
  • Loading branch information
iamkun authored May 8, 2018
2 parents 6e299cc + 877b1c1 commit 986f117
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/constant.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ export const MONTHS = 'January.February.March.April.May.June.July.August.Septemb
export const FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ'

// regex
export const REGEX_PARSE = /^(\d{4})-?(\d{2})-?(\d{1,2})(.*(\d{2}):(\d{2}):(\d{2}))?.?(\d{3})?$/
export const REGEX_PARSE = /^(\d{4})-?(\d{1,2})-?(\d{1,2})(.*?(\d{1,2}):(\d{1,2}):(\d{1,2}))?.?(\d{1,3})?$/
export const REGEX_FORMAT = /\[.*?\]|Y{2,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a|A|m{1,2}|s{1,2}|Z{1,2}|SSS/g
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const parseConfig = (config) => {
if (Utils.isUndefined(config)) return new Date()
if (config instanceof Date) return config
// eslint-disable-next-line no-cond-assign
if (reg = String(config).match(C.REGEX_PARSE)) {
if ((typeof config === 'string') && (reg = config.match(C.REGEX_PARSE))) {
// 2018-08-08 or 20180808
return new Date(
reg[1], reg[2] - 1, reg[3],
Expand Down
28 changes: 17 additions & 11 deletions test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ describe('Parse', () => {

it('String 20130208', () => {
global.console.warn = jest.genMockFunction()// moment.js '2018-4-1 1:1:1:22' will throw warn
const timeArr = [
'20130108',
'2018-04-24',
'2018-05-02 11:12:13',
'2018-05-02 11:12:13.998',
'2018-4-1', // not recommend
'2018-4-1 1:1:1:22' // not recommend
]
timeArr.forEach((t) => {
expect(dayjs(t).valueOf()).toBe(moment(t).valueOf())
})
let d = '20130108'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf())
d = '2018-04-24'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf())
d = '2018-05-02 11:12:13'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf())
d = '2018-05-02 11:12:13.998'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf())
d = '2018-4-1'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend
d = '2018-4-1 1:1:1:223'
expect(dayjs(d).valueOf()).toBe(moment(d).valueOf()) // not recommend
})

it('String ISO 8601 date, time and zone', () => {
Expand All @@ -49,6 +50,11 @@ it('Unix Timestamp Number (milliseconds) 1523520536000', () => {
expect(dayjs(timestamp).valueOf()).toBe(moment(timestamp).valueOf())
})

it('String and Number 20180101', () => {
expect(dayjs(20180101).valueOf()).toBe(moment(20180101).valueOf())
expect(dayjs('20180101').valueOf()).toBe(moment('20180101').valueOf())
})

it('Number 0', () => {
expect(dayjs(0).valueOf()).toBe(moment(0).valueOf())
})
Expand Down

0 comments on commit 986f117

Please sign in to comment.