diff --git a/src/plugin/advancedFormat/index.js b/src/plugin/advancedFormat/index.js index 1b5288c0..117831b8 100644 --- a/src/plugin/advancedFormat/index.js +++ b/src/plugin/advancedFormat/index.js @@ -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) @@ -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) diff --git a/src/plugin/buddhistEra/index.js b/src/plugin/buddhistEra/index.js index 3ee0dd52..ff048143 100644 --- a/src/plugin/buddhistEra/index.js +++ b/src/plugin/buddhistEra/index.js @@ -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) } diff --git a/test/plugin/advancedFormat.test.js b/test/plugin/advancedFormat.test.js index 74de4eb9..e3ccaddc 100644 --- a/test/plugin/advancedFormat.test.js +++ b/test/plugin/advancedFormat.test.js @@ -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') +}) diff --git a/test/plugin/buddhistEra.test.js b/test/plugin/buddhistEra.test.js index 2482702f..626df54c 100644 --- a/test/plugin/buddhistEra.test.js +++ b/test/plugin/buddhistEra.test.js @@ -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') +})