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

fix: skip interpolation for strings inside square brackets #556

Merged
merged 1 commit into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions src/plugin/advancedFormat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default (o, c, d) => { // locale needed later
const locale = this.$locale()
const utils = this.$utils()
const str = formatStr || FORMAT_DEFAULT
const result = str.replace(/Q|wo|gggg|Do|X|x|k{1,2}|S/g, (match) => {
const result = str.replace(/\[([^\]]+)]|Q|wo|gggg|Do|X|x|k{1,2}|S/g, (match) => {
switch (match) {
case 'Q':
return Math.ceil((this.$M + 1) / 3)
Expand All @@ -28,8 +28,10 @@ export default (o, c, d) => { // locale needed later
return utils.s(String(this.$H === 0 ? 24 : this.$H), match === 'k' ? 1 : 2, '0')
case 'X':
return Math.floor(this.$d.getTime() / 1000)
default: // 'x'
case 'x':
return this.$d.getTime()
default:
return match
}
})
return oldFormat.bind(this)(result)
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/buddhistEra/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export default (o, c) => { // locale needed later
proto.format = function (formatStr) {
const yearBias = 543
const str = formatStr || FORMAT_DEFAULT
const result = str.replace(/BBBB|BB/g, (match) => {
const result = str.replace(/(\[[^\]]+])|BBBB|BB/g, (match, a) => {
const year = String(this.$y + yearBias)
const args = match === 'BB' ? [year.slice(-2), 2] : [year, 4]
return this.$utils().s(...args, '0')
return a || this.$utils().s(...args, '0')
})
return oldFormat.bind(this)(result)
}
Expand Down
11 changes: 11 additions & 0 deletions test/plugin/advancedFormat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,14 @@ it('Format Week Year gggg', () => {
const d = '2018-12-31'
expect(dayjs(d).format('gggg')).toBe(moment(d).format('gggg'))
})

it('Skips format strings inside brackets', () => {
expect(dayjs().format('[Q]')).toBe('Q')
expect(dayjs().format('[Do]')).toBe('Do')
expect(dayjs().format('[gggg]')).toBe('gggg')
expect(dayjs().format('[wo]')).toBe('wo')
expect(dayjs().format('[k]')).toBe('k')
expect(dayjs().format('[kk]')).toBe('kk')
expect(dayjs().format('[X]')).toBe('X')
expect(dayjs().format('[x]')).toBe('x')
})
5 changes: 5 additions & 0 deletions test/plugin/buddhistEra.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ it('Format Buddhist Era 4 digit with other format', () => {
const momentDate = today.format(format).replace('BBBB', today.year() + 543)
expect(dayjs().format(format)).toBe(momentDate)
})

it('Skips format strings inside brackets', () => {
expect(dayjs().format('[BBBB]')).toBe('BBBB')
expect(dayjs().format('[BB]')).toBe('BB')
})