From c9c55fd1673228796be692ecc9f4466312e3d83c Mon Sep 17 00:00:00 2001 From: ShtibsDev Date: Tue, 20 Feb 2024 03:44:40 +0200 Subject: [PATCH 1/2] feat: Add luxon configuration with locale weeks set to `true` Add configuration option for luxon with `useLocaleWeeks` for better flexebility.\nThe new configuration is exported seperetly in order to make an additional option and not cause breaking changes. --- src/generate/luxon.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/generate/luxon.ts b/src/generate/luxon.ts index cd1def454..2a31ecd67 100644 --- a/src/generate/luxon.ts +++ b/src/generate/luxon.ts @@ -54,7 +54,7 @@ const normalizeFormat = (format: string): string => */ const normalizeLocale = (locale: string): string => locale.replace(/_/g, '-'); -const generateConfig: GenerateConfig = { +const getGenerateConfig = (useLocaleWeeks?: boolean): GenerateConfig => ({ // get getNow: () => DateTime.local(), getFixedDate: (string) => DateTime.fromFormat(string, 'yyyy-MM-dd'), @@ -85,9 +85,11 @@ const generateConfig: GenerateConfig = { isValidate: (date) => date.isValid, locale: { - getWeekFirstDate: (locale, date) => date.setLocale(normalizeLocale(locale)).startOf('week'), + getWeekFirstDate: (locale, date) => + date.setLocale(normalizeLocale(locale)).startOf('week', { useLocaleWeeks }), getWeekFirstDay: (locale) => - DateTime.local().setLocale(normalizeLocale(locale)).startOf('week').weekday, + DateTime.local().setLocale(normalizeLocale(locale)).startOf('week', { useLocaleWeeks }) + .weekday, getWeek: (locale, date) => date.setLocale(normalizeLocale(locale)).weekNumber, getShortWeekDays: (locale) => { const weekdays = Info.weekdays(weekDayFormatMap[locale] || 'short', { @@ -126,6 +128,8 @@ const generateConfig: GenerateConfig = { return null; }, }, -}; +}); +const generateConfig = getGenerateConfig(); +export const generateConfigWithLocale = getGenerateConfig(true); export default generateConfig; From 4679b0705b9419b9e36be991906ce448c11c6ef5 Mon Sep 17 00:00:00 2001 From: ShtibsDev Date: Tue, 20 Feb 2024 03:46:25 +0200 Subject: [PATCH 2/2] test: Add tests for new luxon configuration --- tests/generate.spec.tsx | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/tests/generate.spec.tsx b/tests/generate.spec.tsx index 2f8879589..82dd17975 100644 --- a/tests/generate.spec.tsx +++ b/tests/generate.spec.tsx @@ -1,7 +1,9 @@ import MockDate from 'mockdate'; import dateFnsGenerateConfig from '../src/generate/dateFns'; import dayjsGenerateConfig from '../src/generate/dayjs'; -import luxonGenerateConfig from '../src/generate/luxon'; +import luxonGenerateConfig, { + generateConfigWithLocale as luxonGenerateConfigWithLocale, +} from '../src/generate/luxon'; import momentGenerateConfig from '../src/generate/moment'; import { getMoment } from './util/commonUtil'; @@ -23,6 +25,7 @@ describe('Picker.Generate', () => { { name: 'dayjs', generateConfig: dayjsGenerateConfig }, { name: 'date-fns', generateConfig: dateFnsGenerateConfig }, { name: 'luxon', generateConfig: luxonGenerateConfig }, + { name: 'luxon-with-locale', generateConfig: luxonGenerateConfigWithLocale }, ]; list.forEach(({ name, generateConfig }) => { @@ -102,7 +105,7 @@ describe('Picker.Generate', () => { }); it('week', () => { - if (!['date-fns', 'luxon'].includes(name)) { + if (!['date-fns', 'luxon', 'luxon-with-locale'].includes(name)) { expect( generateConfig.locale.format( 'en_US', @@ -141,7 +144,9 @@ describe('Picker.Generate', () => { }); it('getWeekFirstDay', () => { - const expectedUsFirstDay = name === 'luxon' ? 1 : 0; + let expectedUsFirstDay = 0; + if (name === 'luxon') expectedUsFirstDay = 1; + if (name === 'luxon-with-locale') expectedUsFirstDay = 7; expect(generateConfig.locale.getWeekFirstDay('en_US')).toEqual(expectedUsFirstDay); expect(generateConfig.locale.getWeekFirstDay('zh_CN')).toEqual(1); @@ -158,25 +163,21 @@ describe('Picker.Generate', () => { it('getWeekFirstDate', () => { const formatStr = name === 'date-fns' ? 'yyyy-MM-dd' : 'YYYY-MM-DD'; - const usDate = generateConfig.locale.getWeekFirstDate( - 'en_US', - generateConfig.locale.parse('en_US', '2020-12-30', [formatStr]), - ); - const cnDate = generateConfig.locale.getWeekFirstDate( - 'zh_CN', - generateConfig.locale.parse('zh_CN', '2020-12-30', [formatStr]), + const [usDate, cnDate] = ['en_US', 'zh_CN'].map((locale) => + generateConfig.locale.getWeekFirstDate( + locale, + generateConfig.locale.parse(locale, '2020-12-30', [formatStr]), + ), ); - const expectedUsFirstDate = name === 'luxon' ? '28' : '27'; + const expectedUsDate = name === 'luxon' ? '2020-12-28' : '2020-12-27'; - expect(generateConfig.locale.format('en_US', usDate, formatStr)).toEqual( - `2020-12-${expectedUsFirstDate}`, - ); + expect(generateConfig.locale.format('en_US', usDate, formatStr)).toEqual(expectedUsDate); expect(generateConfig.locale.format('zh_CN', cnDate, formatStr)).toEqual('2020-12-28'); }); it('Parse format Wo', () => { - if (!['date-fns', 'luxon'].includes(name)) { + if (!['date-fns', 'luxon', 'luxon-with-locale'].includes(name)) { expect( generateConfig.locale.parse('en_US', '2012-51st', ['YYYY-Wo']).format('Wo'), ).toEqual('51st'); @@ -265,7 +266,7 @@ describe('Picker.Generate', () => { ), ).toEqual(49); - const expectedUsWeek = name === 'luxon' ? 49 : 50; + const expectedUsWeek = ['luxon', 'luxon-with-locale'].includes(name) ? 49 : 50; expect( generateConfig.locale.getWeek( 'en_US',