diff --git a/src/plugin/localeData/index.js b/src/plugin/localeData/index.js index b753c3d6..503db0de 100644 --- a/src/plugin/localeData/index.js +++ b/src/plugin/localeData/index.js @@ -1,3 +1,5 @@ +import { t } from '../localizedFormat' + export default (o, c, dayjs) => { // locale needed later const proto = c.prototype const getLocalePart = part => (part && (part.indexOf ? part : part.s)) @@ -11,6 +13,9 @@ export default (o, c, dayjs) => { // locale needed later return result.map((_, index) => (result[(index + (weekStart || 0)) % 7])) } const getDayjsLocaleObject = () => dayjs.Ls[dayjs.locale()] + const getLongDateFormat = (l, format) => + l.formats[format] || t(l.formats[format.toUpperCase()]) + const localeData = function () { return { months: instance => @@ -23,7 +28,8 @@ export default (o, c, dayjs) => { // locale needed later (instance ? instance.format('dd') : getShort(this, 'weekdaysMin', 'weekdays', 2)), weekdaysShort: instance => (instance ? instance.format('ddd') : getShort(this, 'weekdaysShort', 'weekdays', 3)), - longDateFormat: format => this.$locale().formats[format] + longDateFormat: format => getLongDateFormat(this.$locale(), format) + } } proto.localeData = function () { @@ -39,7 +45,7 @@ export default (o, c, dayjs) => { // locale needed later weekdaysMin: () => dayjs.weekdaysMin(), months: () => dayjs.months(), monthsShort: () => dayjs.monthsShort(), - longDateFormat: format => localeObject.formats[format] + longDateFormat: format => getLongDateFormat(localeObject, format) } } diff --git a/src/plugin/localizedFormat/index.js b/src/plugin/localizedFormat/index.js index b72e635b..b56d2252 100644 --- a/src/plugin/localizedFormat/index.js +++ b/src/plugin/localizedFormat/index.js @@ -1,5 +1,8 @@ import { FORMAT_DEFAULT } from '../../constant' +export const t = format => + format.replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, (_, a, b) => a || b.slice(1)) + export default (o, c, d) => { const proto = c.prototype const oldFormat = proto.format @@ -12,7 +15,6 @@ export default (o, c, d) => { LLLL: 'dddd, MMMM D, YYYY h:mm A' } d.en.formats = englishFormats - const t = format => format.replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, (_, a, b) => a || b.slice(1)) proto.format = function (formatStr = FORMAT_DEFAULT) { const { formats = {} } = this.$locale() const result = formatStr.replace(/(\[[^\]]+])|(LTS?|l{1,4}|L{1,4})/g, (_, a, b) => { diff --git a/test/plugin/localeData.test.js b/test/plugin/localeData.test.js index a2e017e9..82985640 100644 --- a/test/plugin/localeData.test.js +++ b/test/plugin/localeData.test.js @@ -38,7 +38,7 @@ describe('Instance localeData', () => { expect(dayjsLocaleData.weekdaysMin()).toEqual(momentLocaleData.weekdaysMin()) expect(dayjsLocaleData.weekdaysShort(d)).toBe(momentLocaleData.weekdaysShort(m)) expect(dayjsLocaleData.weekdaysShort()).toEqual(momentLocaleData.weekdaysShort()) - const longDateFormats = ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'] + const longDateFormats = ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL', 'l', 'll', 'lll', 'llll'] longDateFormats.forEach((f) => { expect(dayjsLocaleData.longDateFormat(f)).toEqual(momentLocaleData.longDateFormat(f)) }) @@ -61,7 +61,7 @@ it('Global localeData', () => { expect(dayjsLocaleData.weekdays()).toEqual(momentLocaleData.weekdays()) expect(dayjsLocaleData.weekdaysShort()).toEqual(momentLocaleData.weekdaysShort()) expect(dayjsLocaleData.weekdaysMin()).toEqual(momentLocaleData.weekdaysMin()) - const longDateFormats = ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'] + const longDateFormats = ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL', 'l', 'll', 'lll', 'llll'] longDateFormats.forEach((f) => { expect(dayjsLocaleData.longDateFormat(f)).toEqual(momentLocaleData.longDateFormat(f)) })