Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/generate/luxon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const normalizeFormat = (format: string): string =>
*/
const normalizeLocale = (locale: string): string => locale.replace(/_/g, '-');

const generateConfig: GenerateConfig<DateTime> = {
const getGenerateConfig = (useLocaleWeeks?: boolean): GenerateConfig<DateTime> => ({
// get
getNow: () => DateTime.local(),
getFixedDate: (string) => DateTime.fromFormat(string, 'yyyy-MM-dd'),
Expand Down Expand Up @@ -85,9 +85,11 @@ const generateConfig: GenerateConfig<DateTime> = {
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', {
Expand Down Expand Up @@ -126,6 +128,8 @@ const generateConfig: GenerateConfig<DateTime> = {
return null;
},
},
};
});

const generateConfig = getGenerateConfig();
export const generateConfigWithLocale = getGenerateConfig(true);
export default generateConfig;
33 changes: 17 additions & 16 deletions tests/generate.spec.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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 }) => {
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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);
Expand All @@ -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');
Expand Down Expand Up @@ -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',
Expand Down