Skip to content

Commit

Permalink
fix: skip square brackets in buddhistEra, advancedFormat plugins (#556)
Browse files Browse the repository at this point in the history
fixes #554
  • Loading branch information
mariomc authored and iamkun committed Apr 26, 2019
1 parent 4bdc8f9 commit 9279718
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
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')
})

0 comments on commit 9279718

Please sign in to comment.