From d40930420ab4293a4f597d13862705530dce44ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1rio=20Gon=C3=A7alves?= Date: Fri, 26 Apr 2019 15:49:22 +0100 Subject: [PATCH] fix: Update LocalizedFormat plugin lowercase formats logic (#557) --- src/plugin/localizedFormat/index.js | 19 ++++++-------- test/plugin/localizedFormat.test.js | 40 ++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/plugin/localizedFormat/index.js b/src/plugin/localizedFormat/index.js index e3412dcfa..b72e635b0 100644 --- a/src/plugin/localizedFormat/index.js +++ b/src/plugin/localizedFormat/index.js @@ -9,19 +9,16 @@ export default (o, c, d) => { L: 'MM/DD/YYYY', LL: 'MMMM D, YYYY', LLL: 'MMMM D, YYYY h:mm A', - LLLL: 'dddd, MMMM D, YYYY h:mm A', - l: 'M/D/YYYY', - ll: 'MMM D, YYYY', - lll: 'MMM D, YYYY h:mm A', - llll: 'ddd, MMM D, YYYY h:mm A' + LLLL: 'dddd, MMMM D, YYYY h:mm A' } d.en.formats = englishFormats - proto.format = function (formatStr) { - const locale = this.$locale() - const formats = locale.formats || {} - const str = formatStr || FORMAT_DEFAULT - const result = str.replace(/(\[[^\]]+])|(LTS?|l{1,4}|L{1,4})/g, (_, a, b) => - a || formats[b] || englishFormats[b]) + 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) => { + const B = b && b.toUpperCase() + return a || formats[b] || englishFormats[b] || t(formats[B]) + }) return oldFormat.call(this, result) } } diff --git a/test/plugin/localizedFormat.test.js b/test/plugin/localizedFormat.test.js index 24dd8df04..1c7a89e14 100644 --- a/test/plugin/localizedFormat.test.js +++ b/test/plugin/localizedFormat.test.js @@ -2,6 +2,7 @@ import MockDate from 'mockdate' import moment from 'moment' import dayjs from '../../src' import es from '../../src/locale/es' +import znCn from '../../src/locale/zh-cn' import localizedFormat from '../../src/plugin/localizedFormat' dayjs.extend(localizedFormat) @@ -17,7 +18,7 @@ afterEach(() => { it('Declares English localized formats', () => { expect(dayjs.en).toBeDefined() expect(dayjs.en.formats).toBeDefined(); - ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL', 'l', 'll', 'lll', 'llll'].forEach(option => + ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'].forEach(option => expect(dayjs.en.formats[option]).toBeDefined()) }) @@ -30,12 +31,27 @@ it('Should not interpolate characters inside square brackets', () => { expect(actualDate.format('YYYY [l] YYYY')).toBe('1970 l 1970') expect(actualDate.format('l [l] l')).toBe('1/1/1970 l 1/1/1970') expect(actualDate.format('[L LL LLL LLLL]')).toBe(expectedDate.format('[L LL LLL LLLL]')) + + + const localeFormats = { + L: '[MMMM MM DD dddd]' + } + const mockedDayJsLocale = { + ...es, + name: 'fake-locale', + formats: { + ...localeFormats + } + } + const fakeDate = dayjs(date, { locale: mockedDayJsLocale }) + + expect(fakeDate.locale('fake-locale').format('l')).toEqual('MMMM MM DD dddd') }) it('Recognizes localized format options', () => { const { formats } = dayjs.en const date = dayjs(); - ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL', 'l', 'll', 'lll', 'llll'].forEach(option => + ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'].forEach(option => expect(date.format(option)).toBe(date.format(formats[option]))) }) @@ -43,7 +59,7 @@ it('Uses correct English formats', () => { const date = new Date() const actualDate = dayjs(date) const expectedDate = moment(date); - ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL', 'l', 'll', 'lll', 'llll'].forEach(option => + ['LT', 'LTS', 'L', 'LL', 'LLL', 'LLLL'].forEach(option => expect(actualDate.format(option)).toBe(expectedDate.format(option))) }) @@ -72,3 +88,21 @@ it('Uses the locale of the dayjs instance', () => { const spanishDate = dayjs(date, { locale: es }) expect(englishDate.format('L LTS')).not.toBe(spanishDate.format('L LTS')) }) + + +it('Uses the localized lowercase formats if defined', () => { + const date = new Date() + const znDate = dayjs(date, { locale: znCn }); + ['l', 'll', 'lll', 'llll'].forEach(option => expect(znDate.format(option)).toBe(znDate.format(znCn.formats[option]))) +}) + +it('Uses the localized uppercase formats as a base for lowercase formats, if not defined', () => { + const date = new Date() + const spanishDate = dayjs(date, { locale: es }); + + ['l', 'll', 'lll', 'llll'].forEach((option) => { + const upperCaseFormat = es.formats[option.toUpperCase()] + const adaptedFormat = upperCaseFormat.replace(/(\[[^\]]+])|(MMMM|MM|DD|dddd)/g, (_, a, b) => a || b.slice(1)) + expect(spanishDate.format(option)).toBe(spanishDate.format(adaptedFormat)) + }) +})