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

Parse month from string in customParseFormat #457

Merged
merged 17 commits into from
Feb 23, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
40 changes: 20 additions & 20 deletions .babelrc → babel.config.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"env": {
"test": {
"presets": [
"@babel/preset-env"
]
},
"build": {
"presets": [
[
"@babel/preset-env",
{
"modules": false,
"loose": true
}
]
]
}
}
}
module.exports = {
Kreozot marked this conversation as resolved.
Show resolved Hide resolved
"env": {
"test": {
"presets": [
"@babel/preset-env"
]
},
"build": {
"presets": [
[
"@babel/preset-env",
{
"modules": false,
"loose": true
}
]
]
}
}
};
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,16 @@ const parseDate = (date) => {

class Dayjs {
constructor(cfg) {
this.$L = this.$L || parseLocale(cfg.locale, null, true) || L
Copy link
Contributor Author

Choose a reason for hiding this comment

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

to make locale available in parse() function

this.parse(cfg) // for plugin
}

parse(cfg) {
this.$d = parseDate(cfg.date)
this.init(cfg)
this.init()
}

init(cfg) {
init() {
const { $d } = this
this.$y = $d.getFullYear()
this.$M = $d.getMonth()
Expand All @@ -82,7 +83,6 @@ class Dayjs {
this.$m = $d.getMinutes()
this.$s = $d.getSeconds()
this.$ms = $d.getMilliseconds()
this.$L = this.$L || parseLocale(cfg.locale, null, true) || L
}

// eslint-disable-next-line class-methods-use-this
Expand Down
97 changes: 54 additions & 43 deletions src/plugin/customParseFormat/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const formattingTokens = /(\[[^[]*\])|([-:/.()\s]+)|(A|a|YYYY|YY?|MM?|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g
const formattingTokens = /(\[[^[]*\])|([-:/.()\s]+)|(A|a|YYYY|YY?|MM?M?M?|DD?|hh?|HH?|mm?|ss?|S{1,3}|z|ZZ?)/g

const match1 = /\d/ // 0 - 9
const match2 = /\d\d/ // 00 - 99
Expand All @@ -9,6 +9,7 @@ const matchUpperCaseAMPM = /[AP]M/
const matchLowerCaseAMPM = /[ap]m/
const matchSigned = /[+-]?\d+/ // -inf - inf
const matchOffset = /[+-]\d\d:?\d\d/ // +00:00 -00:00 +0000 or -0000
const matchWord = /[A-Za-z]+/ // Word

Kreozot marked this conversation as resolved.
Show resolved Hide resolved
function offsetFromString(string) {
const parts = string.match(/([+-]|\d\d)/g)
Expand All @@ -27,44 +28,6 @@ const zoneExpressions = [matchOffset, function (input) {
zone.offset = offsetFromString(input)
}]

const expressions = {
A: [matchUpperCaseAMPM, function (input) {
this.afternoon = input === 'PM'
}],
a: [matchLowerCaseAMPM, function (input) {
this.afternoon = input === 'pm'
}],
S: [match1, function (input) {
this.milliseconds = +input * 100
}],
SS: [match2, function (input) {
this.milliseconds = +input * 10
}],
SSS: [match3, function (input) {
this.milliseconds = +input
}],
s: [match1to2, addInput('seconds')],
ss: [match2, addInput('seconds')],
m: [match1to2, addInput('minutes')],
mm: [match2, addInput('minutes')],
H: [match1to2, addInput('hours')],
h: [match1to2, addInput('hours')],
HH: [match2, addInput('hours')],
hh: [match2, addInput('hours')],
D: [match1to2, addInput('day')],
DD: [match2, addInput('day')],
M: [match1to2, addInput('month')],
MM: [match2, addInput('month')],
Y: [matchSigned, addInput('year')],
YY: [match2, function (input) {
input = +input
this.year = input + (input > 68 ? 1900 : 2000)
}],
YYYY: [match4, addInput('year')],
Z: zoneExpressions,
ZZ: zoneExpressions
}

function correctHours(time) {
const { afternoon } = time
if (afternoon !== undefined) {
Expand All @@ -80,7 +43,55 @@ function correctHours(time) {
}
}

function makeParser(format) {
function makeParser(format, instance) {
const expressions = {
Kreozot marked this conversation as resolved.
Show resolved Hide resolved
A: [matchUpperCaseAMPM, function (input) {
this.afternoon = input === 'PM'
}],
a: [matchLowerCaseAMPM, function (input) {
this.afternoon = input === 'pm'
}],
S: [match1, function (input) {
this.milliseconds = +input * 100
}],
SS: [match2, function (input) {
this.milliseconds = +input * 10
}],
SSS: [match3, function (input) {
this.milliseconds = +input
}],
s: [match1to2, addInput('seconds')],
ss: [match2, addInput('seconds')],
m: [match1to2, addInput('minutes')],
mm: [match2, addInput('minutes')],
H: [match1to2, addInput('hours')],
h: [match1to2, addInput('hours')],
HH: [match2, addInput('hours')],
hh: [match2, addInput('hours')],
D: [match1to2, addInput('day')],
DD: [match2, addInput('day')],
M: [match1to2, addInput('month')],
MM: [match2, addInput('month')],
MMM: [matchWord, function (input) {
const locale = instance.$locale()
const { months } = locale
Copy link
Owner

Choose a reason for hiding this comment

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

also consider monthsShort in some locales

Copy link
Owner

Choose a reason for hiding this comment

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

why change locale before date parsing?

this.month = months.findIndex(month => month.substr(0, 3) === input) + 1
}],
MMMM: [matchWord, function (input) {
const locale = instance.$locale()
const { months } = locale
this.month = months.indexOf(input) + 1
}],
Y: [matchSigned, addInput('year')],
YY: [match2, function (input) {
input = +input
this.year = input + (input > 68 ? 1900 : 2000)
}],
YYYY: [match4, addInput('year')],
Z: zoneExpressions,
ZZ: zoneExpressions
}

const array = format.match(formattingTokens)
const { length } = array
for (let i = 0; i < length; i += 1) {
Expand Down Expand Up @@ -114,9 +125,9 @@ function makeParser(format) {
}
}

const parseFormattedInput = (input, format) => {
const parseFormattedInput = (input, format, instance) => {
try {
const parser = makeParser(format)
const parser = makeParser(format, instance)
Kreozot marked this conversation as resolved.
Show resolved Hide resolved
const {
year, month, day, hours, minutes, seconds, milliseconds, zone
} = parser(input)
Expand All @@ -143,7 +154,7 @@ export default (o, C) => {
proto.parse = function (cfg) {
const { date: input, format } = cfg
if (format) {
this.$d = parseFormattedInput(input, format)
this.$d = parseFormattedInput(input, format, this)
this.init(cfg)
} else {
oldParse.call(this, cfg)
Expand Down
12 changes: 12 additions & 0 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,15 @@ it('fails with an invalid format', () => {
expect(dayjs(input, format).format().toLowerCase())
.toBe(moment(input, format).format().toLowerCase())
})

it('parse month from string', () => {
const input = '2018 February 03'
const format = 'YYYY MMMM DD'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})

it('parse month from short string', () => {
const input = '2018 Feb 03'
const format = 'YYYY MMM DD'
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})