Skip to content

Commit 9279718

Browse files
mariomciamkun
authored andcommitted
fix: skip square brackets in buddhistEra, advancedFormat plugins (#556)
fixes #554
1 parent 4bdc8f9 commit 9279718

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

src/plugin/advancedFormat/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default (o, c, d) => { // locale needed later
1313
const locale = this.$locale()
1414
const utils = this.$utils()
1515
const str = formatStr || FORMAT_DEFAULT
16-
const result = str.replace(/Q|wo|gggg|Do|X|x|k{1,2}|S/g, (match) => {
16+
const result = str.replace(/\[([^\]]+)]|Q|wo|gggg|Do|X|x|k{1,2}|S/g, (match) => {
1717
switch (match) {
1818
case 'Q':
1919
return Math.ceil((this.$M + 1) / 3)
@@ -28,8 +28,10 @@ export default (o, c, d) => { // locale needed later
2828
return utils.s(String(this.$H === 0 ? 24 : this.$H), match === 'k' ? 1 : 2, '0')
2929
case 'X':
3030
return Math.floor(this.$d.getTime() / 1000)
31-
default: // 'x'
31+
case 'x':
3232
return this.$d.getTime()
33+
default:
34+
return match
3335
}
3436
})
3537
return oldFormat.bind(this)(result)

src/plugin/buddhistEra/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ export default (o, c) => { // locale needed later
77
proto.format = function (formatStr) {
88
const yearBias = 543
99
const str = formatStr || FORMAT_DEFAULT
10-
const result = str.replace(/BBBB|BB/g, (match) => {
10+
const result = str.replace(/(\[[^\]]+])|BBBB|BB/g, (match, a) => {
1111
const year = String(this.$y + yearBias)
1212
const args = match === 'BB' ? [year.slice(-2), 2] : [year, 4]
13-
return this.$utils().s(...args, '0')
13+
return a || this.$utils().s(...args, '0')
1414
})
1515
return oldFormat.bind(this)(result)
1616
}

test/plugin/advancedFormat.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,14 @@ it('Format Week Year gggg', () => {
8282
const d = '2018-12-31'
8383
expect(dayjs(d).format('gggg')).toBe(moment(d).format('gggg'))
8484
})
85+
86+
it('Skips format strings inside brackets', () => {
87+
expect(dayjs().format('[Q]')).toBe('Q')
88+
expect(dayjs().format('[Do]')).toBe('Do')
89+
expect(dayjs().format('[gggg]')).toBe('gggg')
90+
expect(dayjs().format('[wo]')).toBe('wo')
91+
expect(dayjs().format('[k]')).toBe('k')
92+
expect(dayjs().format('[kk]')).toBe('kk')
93+
expect(dayjs().format('[X]')).toBe('X')
94+
expect(dayjs().format('[x]')).toBe('x')
95+
})

test/plugin/buddhistEra.test.js

+5
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ it('Format Buddhist Era 4 digit with other format', () => {
3131
const momentDate = today.format(format).replace('BBBB', today.year() + 543)
3232
expect(dayjs().format(format)).toBe(momentDate)
3333
})
34+
35+
it('Skips format strings inside brackets', () => {
36+
expect(dayjs().format('[BBBB]')).toBe('BBBB')
37+
expect(dayjs().format('[BB]')).toBe('BB')
38+
})

0 commit comments

Comments
 (0)