From 744596370b4f8d228b45cfa8aae2d7f671aec0eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=CC=81rio=20Gonc=CC=A7alves?= Date: Tue, 2 Apr 2019 19:34:02 +0100 Subject: [PATCH 1/2] feat: make lowercase localized formats fallback to slightly modified uppercase formats --- src/plugin/localizedFormat/index.js | 19 ++++++-------- test/plugin/localizableFormat.test.js | 38 +++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 13 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/localizableFormat.test.js b/test/plugin/localizableFormat.test.js index dc6348125..b3934b85c 100644 --- a/test/plugin/localizableFormat.test.js +++ b/test/plugin/localizableFormat.test.js @@ -3,6 +3,7 @@ import moment from 'moment' import dayjs from '../../src' import es from '../../src/locale/es' import ar from '../../src/locale/ar' +import znCn from '../../src/locale/zh-cn' import localizedFormat from '../../src/plugin/localizedFormat' dayjs.extend(localizedFormat) @@ -18,7 +19,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()) }) @@ -31,12 +32,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]))) }) @@ -69,3 +85,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)) + }) +}) From d4f79bbb5bcbc504deb8be5385911abb3d72d312 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=CC=81rio=20Gonc=CC=A7alves?= Date: Fri, 26 Apr 2019 10:24:37 +0100 Subject: [PATCH 2/2] Renamed file --- .../plugin/{localizableFormat.test.js => localizedFormat.test.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename test/plugin/{localizableFormat.test.js => localizedFormat.test.js} (100%) diff --git a/test/plugin/localizableFormat.test.js b/test/plugin/localizedFormat.test.js similarity index 100% rename from test/plugin/localizableFormat.test.js rename to test/plugin/localizedFormat.test.js