Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix lowercase in LocaleData.longDateFormat() #1101

Merged
merged 12 commits into from
Oct 4, 2020
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