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 11 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
31 changes: 25 additions & 6 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 = /[^\d\s]+/ // Word

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

const expressions = {
const baseExpressions = {
A: [matchUpperCaseAMPM, function (input) {
this.afternoon = input === 'PM'
}],
Expand Down Expand Up @@ -80,7 +81,25 @@ function correctHours(time) {
}
}

function makeParser(format) {
function makeParser(format, instance) {
const expressions = {
Kreozot marked this conversation as resolved.
Show resolved Hide resolved
...baseExpressions,
MMM: [matchWord, function (input) {
const locale = instance.$locale()
const { months, monthsShort } = locale
Kreozot marked this conversation as resolved.
Show resolved Hide resolved
const matchIndex = monthsShort
? monthsShort.findIndex(month => month === input)
: months.findIndex(month => month.substr(0, 3) === input)
this.month = matchIndex >= 0 ? matchIndex + 1 : NaN
}],
MMMM: [matchWord, function (input) {
const locale = instance.$locale()
const { months } = locale
const matchIndex = months.indexOf(input)
this.month = matchIndex >= 0 ? matchIndex + 1 : NaN
}]
}

const array = format.match(formattingTokens)
const { length } = array
for (let i = 0; i < length; i += 1) {
Expand Down Expand Up @@ -114,9 +133,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 +162,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
47 changes: 47 additions & 0 deletions test/plugin/customParseFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import MockDate from 'mockdate'
import moment from 'moment'
import dayjs from '../../src'
import customParseFormat from '../../src/plugin/customParseFormat'
import uk from '../../src/locale/uk'

dayjs.extend(customParseFormat)
const oldLocale = dayjs().$locale()

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

afterEach(() => {
MockDate.reset()
dayjs.locale(oldLocale)
})

it('does not break the built-in parsing', () => {
Expand Down Expand Up @@ -72,3 +75,47 @@ 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())
})

it('parse month from short string in other locales', () => {
const input = '2018 May 03'
const inputUk = '2018 трав 03'
const format = 'YYYY MMM DD'
dayjs.locale(uk)
expect(dayjs(inputUk, format).valueOf()).toBe(moment(input, format).valueOf())
Copy link
Owner

Choose a reason for hiding this comment

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

this is not a good test .

we should compare with

moment.locale('uk')

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.
I also write test with passing locale in config (btw, it is undocumented for now)

})

it('return Invalid Date when parse corrupt string', () => {
const input = '2018 Februaru 03'
Copy link
Owner

Choose a reason for hiding this comment

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

I don't know why moment('2018 Februaru 03', 'YYYY MMMM DD') returns a valid date.

But we might should change Februaru to another error string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

const format = 'YYYY MMMM DD'
expect(dayjs(input, format).format()).toBe('Invalid Date')
Kreozot marked this conversation as resolved.
Show resolved Hide resolved
})

it('return Invalid Date when parse corrupt short string', () => {
const input = '2018 Fev 03'
const format = 'YYYY MMM DD'
expect(dayjs(input, format).format()).toBe('Invalid Date')
})

it('correctly parse month from string after changing locale', () => {
const input = '2018 February 03'
const inputUk = '2018 лютий 03'
const format = 'YYYY MMMM DD'

dayjs.locale(uk)
expect(dayjs(inputUk, format).valueOf()).toBe(moment(input, format).valueOf())

dayjs.locale(oldLocale)
expect(dayjs(input, format).valueOf()).toBe(moment(input, format).valueOf())
})