Skip to content

Commit

Permalink
fix: Fix LocaleData plugin longDateFormat lowercase error (#1101)
Browse files Browse the repository at this point in the history
  • Loading branch information
xvaara authored Oct 4, 2020
1 parent b1a0294 commit 7937ccd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
10 changes: 8 additions & 2 deletions src/plugin/localeData/index.js
Original file line number Diff line number Diff line change
@@ -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))
Expand All @@ -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 =>
Expand All @@ -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 () {
Expand All @@ -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)
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/plugin/localizedFormat/index.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions test/plugin/localeData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand All @@ -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))
})
Expand Down

0 comments on commit 7937ccd

Please sign in to comment.