Skip to content

Commit

Permalink
Merge pull request #1316 from iamkun/dev
Browse files Browse the repository at this point in the history
D2M
  • Loading branch information
iamkun authored Jan 5, 2021
2 parents 873b749 + b0dda31 commit 5716088
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"description": "2KB immutable date time library alternative to Moment.js with the same modern API ",
"main": "dayjs.min.js",
"types": "index.d.ts",
"module": "esm/index.js",
"scripts": {
"test": "TZ=Pacific/Auckland npm run test-tz && TZ=Europe/London npm run test-tz && TZ=America/Whitehorse npm run test-tz && npm run test-tz && jest",
"test-tz": "date && jest test/timezone.test --coverage=false",
Expand Down
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 FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ssZ'
export const INVALID_DATE_STRING = 'Invalid Date'

// regex
export const REGEX_PARSE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?\.?(\d+)?$/
export const REGEX_PARSE = /^(\d{4})[-/]?(\d{1,2})?[-/]?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?[.:]?(\d+)?$/
export const REGEX_FORMAT = /\[([^\]]+)]|Y{1,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
3 changes: 3 additions & 0 deletions src/plugin/devHelper/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export default (o, c, d) => {
if (typeof date === 'string' && date.length === 13) {
console.warn(`To parse a Unix timestamp like ${date}, you should pass it as a Number. https://day.js.org/docs/en/parse/unix-timestamp-milliseconds`)
}
if (typeof date === 'number' && String(date).length === 4) {
console.warn(`Guessing you may want to parse the Year ${date}, you should pass it as a String ${date}, not a Number. Otherwise, ${date} will be treated as a Unix timestamp`)
}
if (cfg.args.length >= 2 && !d.p.customParseFormat) {
console.warn(`To parse a date-time string like ${date} using the given format, you should enable customParseFormat plugin first. https://day.js.org/docs/en/parse/string-format`)
}
Expand Down
8 changes: 8 additions & 0 deletions test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ describe('REGEX_PARSE', () => {
expect(d).toBe(null)
})

// format used in timezone plugin utcString
it('2021-1-4 0:42:53:000', () => {
const date = '2021-1-4 0:42:53:000'
const d = date.match(REGEX_PARSE)
expect(dayjs(date).valueOf()).toBe(moment(date).valueOf())
expect(d.join('-')).toBe('2021-1-4 0:42:53:000-2021-1-4-0-42-53-000')
})

it('2020-12-31T18:00:00-05:00 (no regex match)', () => {
const date = '2020-12-31T18:00:00-05:00'
const d = date.match(REGEX_PARSE)
Expand Down
34 changes: 34 additions & 0 deletions test/plugin/devHelper.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import MockDate from 'mockdate'
import dayjs from '../../src'
import devHelper from '../../src/plugin/devHelper'

dayjs.extend(devHelper)

beforeEach(() => {
MockDate.set(new Date())
})

afterEach(() => {
MockDate.reset()
})

global.console.warn = jest.genMockFunction()


it('Warning: passing Year as a Number will be parsed as a Unix timestamp', () => {
const consoleSpy = jest.spyOn(console, 'warn')
dayjs(2020)
expect(consoleSpy).toHaveBeenCalledWith('Guessing you may want to parse the Year 2020, you should pass it as a String 2020, not a Number. Otherwise, 2020 will be treated as a Unix timestamp')
})

it('Warning Passing Unix timestamp as a String not Number', () => {
const consoleSpy = jest.spyOn(console, 'warn')
dayjs('1231231231231')
expect(consoleSpy).toHaveBeenCalledWith('To parse a Unix timestamp like 1231231231231, you should pass it as a Number. https://day.js.org/docs/en/parse/unix-timestamp-milliseconds')
})

it('Warning Enable customParseFormat plugin while passing the second format parameter', () => {
const consoleSpy = jest.spyOn(console, 'warn')
dayjs('2020', 'YYYY')
expect(consoleSpy).toHaveBeenCalledWith('To parse a date-time string like 2020 using the given format, you should enable customParseFormat plugin first. https://day.js.org/docs/en/parse/string-format')
})

0 comments on commit 5716088

Please sign in to comment.