Skip to content

Commit

Permalink
fix: add devHelper plugin (#1163)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun authored Oct 23, 2020
1 parent 368108b commit de49dc8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,5 +401,5 @@ dayjs.unix = timestamp => (

dayjs.en = Ls[L]
dayjs.Ls = Ls

dayjs.p = {}
export default dayjs
1 change: 1 addition & 0 deletions src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ const parseFormattedInput = (input, format, utc) => {


export default (o, C, d) => {
d.p.customParseFormat = true
const proto = C.prototype
const oldParse = proto.parse
proto.parse = function (cfg) {
Expand Down
18 changes: 18 additions & 0 deletions src/plugin/devHelper/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-disable no-console */
export default (o, c, d) => {
if (process.env.NODE_ENV !== 'production') {
const proto = c.prototype
const oldParse = proto.parse
proto.parse = function (cfg) {
const { date } = cfg
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 (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`)
}
return oldParse.bind(this)(cfg)
}
}
}

4 changes: 4 additions & 0 deletions types/plugin/devHelper.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PluginFunc } from 'dayjs'

declare const plugin: PluginFunc
export = plugin

0 comments on commit de49dc8

Please sign in to comment.