Skip to content

Commit 7937ccd

Browse files
authored
fix: Fix LocaleData plugin longDateFormat lowercase error (#1101)
1 parent b1a0294 commit 7937ccd

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

src/plugin/localeData/index.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { t } from '../localizedFormat'
2+
13
export default (o, c, dayjs) => { // locale needed later
24
const proto = c.prototype
35
const getLocalePart = part => (part && (part.indexOf ? part : part.s))
@@ -11,6 +13,9 @@ export default (o, c, dayjs) => { // locale needed later
1113
return result.map((_, index) => (result[(index + (weekStart || 0)) % 7]))
1214
}
1315
const getDayjsLocaleObject = () => dayjs.Ls[dayjs.locale()]
16+
const getLongDateFormat = (l, format) =>
17+
l.formats[format] || t(l.formats[format.toUpperCase()])
18+
1419
const localeData = function () {
1520
return {
1621
months: instance =>
@@ -23,7 +28,8 @@ export default (o, c, dayjs) => { // locale needed later
2328
(instance ? instance.format('dd') : getShort(this, 'weekdaysMin', 'weekdays', 2)),
2429
weekdaysShort: instance =>
2530
(instance ? instance.format('ddd') : getShort(this, 'weekdaysShort', 'weekdays', 3)),
26-
longDateFormat: format => this.$locale().formats[format]
31+
longDateFormat: format => getLongDateFormat(this.$locale(), format)
32+
2733
}
2834
}
2935
proto.localeData = function () {
@@ -39,7 +45,7 @@ export default (o, c, dayjs) => { // locale needed later
3945
weekdaysMin: () => dayjs.weekdaysMin(),
4046
months: () => dayjs.months(),
4147
monthsShort: () => dayjs.monthsShort(),
42-
longDateFormat: format => localeObject.formats[format]
48+
longDateFormat: format => getLongDateFormat(localeObject, format)
4349
}
4450
}
4551

src/plugin/localizedFormat/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { FORMAT_DEFAULT } from '../../constant'
22

3+
export const t = format =>
4+
format.replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, (_, a, b) => a || b.slice(1))
5+
36
export default (o, c, d) => {
47
const proto = c.prototype
58
const oldFormat = proto.format
@@ -12,7 +15,6 @@ export default (o, c, d) => {
1215
LLLL: 'dddd, MMMM D, YYYY h:mm A'
1316
}
1417
d.en.formats = englishFormats
15-
const t = format => format.replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, (_, a, b) => a || b.slice(1))
1618
proto.format = function (formatStr = FORMAT_DEFAULT) {
1719
const { formats = {} } = this.$locale()
1820
const result = formatStr.replace(/(\[[^\]]+])|(LTS?|l{1,4}|L{1,4})/g, (_, a, b) => {

test/plugin/localeData.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('Instance localeData', () => {
3838
expect(dayjsLocaleData.weekdaysMin()).toEqual(momentLocaleData.weekdaysMin())
3939
expect(dayjsLocaleData.weekdaysShort(d)).toBe(momentLocaleData.weekdaysShort(m))
4040
expect(dayjsLocaleData.weekdaysShort()).toEqual(momentLocaleData.weekdaysShort())
41-
const longDateFormats = ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL']
41+
const longDateFormats = ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL', 'l', 'll', 'lll', 'llll']
4242
longDateFormats.forEach((f) => {
4343
expect(dayjsLocaleData.longDateFormat(f)).toEqual(momentLocaleData.longDateFormat(f))
4444
})
@@ -61,7 +61,7 @@ it('Global localeData', () => {
6161
expect(dayjsLocaleData.weekdays()).toEqual(momentLocaleData.weekdays())
6262
expect(dayjsLocaleData.weekdaysShort()).toEqual(momentLocaleData.weekdaysShort())
6363
expect(dayjsLocaleData.weekdaysMin()).toEqual(momentLocaleData.weekdaysMin())
64-
const longDateFormats = ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL']
64+
const longDateFormats = ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL', 'l', 'll', 'lll', 'llll']
6565
longDateFormats.forEach((f) => {
6666
expect(dayjsLocaleData.longDateFormat(f)).toEqual(momentLocaleData.longDateFormat(f))
6767
})

0 commit comments

Comments
 (0)