From 2413795de19fdc6cb10001b6202bfd735ae41634 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20SIMOND?= Date: Wed, 10 Jan 2024 15:11:01 +0100 Subject: [PATCH] breaking(deps): bump date-fns to v3 (#656) BREAKING CHANGE: updates date-fns peer version to v3 --- README.md | 2 +- __tests__/date-fns-jalali.test.ts | 2 +- __tests__/formats.test.ts | 4 +- __tests__/localization.test.ts | 4 +- packages/date-fns/package.json | 4 +- packages/date-fns/src/date-fns-utils.ts | 120 ++++++++++++------------ 6 files changed, 67 insertions(+), 69 deletions(-) diff --git a/README.md b/README.md index 15712709..f5a14160 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Abstraction over common JavaScript date management libraries. [![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg)](https://lerna.js.org/) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier) -The project exposes an abstraction interface over [luxon](https://moment.github.io/luxon/), [date-fns v2](https://github.com/date-fns/date-fns), [dayjs](https://github.com/iamkun/dayjs) and [moment](https://momentjs.com/). +The project exposes an abstraction interface over [luxon](https://moment.github.io/luxon/), [date-fns v3](https://github.com/date-fns/date-fns), [dayjs](https://github.com/iamkun/dayjs) and [moment](https://momentjs.com/). It allows you to build any UI date or time components while utilizing the same date management library in use within your user's project. It simplifies timezone management, allows your code to return the exact same type that your user expects and works with specific calendar systems (e.g. [Jalali calendar](https://en.wikipedia.org/wiki/Jalali_calendar)) diff --git a/__tests__/date-fns-jalali.test.ts b/__tests__/date-fns-jalali.test.ts index f60ad2dc..0336aabd 100644 --- a/__tests__/date-fns-jalali.test.ts +++ b/__tests__/date-fns-jalali.test.ts @@ -1,6 +1,6 @@ import DateFnsJalaliUtils from "../packages/date-fns-jalali/src"; import { TEST_TIMESTAMP } from "./test-utils"; -import ruLocale from "date-fns/locale/ru"; +import { ru as ruLocale } from "date-fns/locale/ru"; const date = new Date(TEST_TIMESTAMP); /* 1397/08/08 سه‌شنبه*/ const dateTimeFormat = "yyyy/MM/dd HH:mm"; diff --git a/__tests__/formats.test.ts b/__tests__/formats.test.ts index 4645ac11..0aeb6c70 100644 --- a/__tests__/formats.test.ts +++ b/__tests__/formats.test.ts @@ -1,4 +1,4 @@ -import ruDateFnsLocale from "date-fns/locale/ru"; +import { ru as ruDateFnsLocale } from "date-fns/locale/ru"; import LuxonUtils from "../packages/luxon/src"; import DateFnsUtils from "../packages/date-fns/src"; import MomentUtils from "../packages/moment/src"; @@ -85,7 +85,7 @@ describe("Localized formats", () => { test.each` format | expectedWithEn | expectedWithRu ${"fullDate"} | ${"Feb 1, 2020"} | ${"1 фев. 2020 г."} - ${"fullDateWithWeekday"} | ${"Saturday, February 1st, 2020"} | ${"суббота, 1-е февраля 2020 г."} + ${"fullDateWithWeekday"} | ${"Saturday, February 1st, 2020"} | ${"суббота, 1 февраля 2020 г."} ${"fullDateTime"} | ${"Feb 1, 2020 11:44 PM"} | ${"1 фев. 2020 г. 23:44"} ${"fullDateTime12h"} | ${"Feb 1, 2020 11:44 PM"} | ${"1 фев. 2020 г. 11:44 ПП"} ${"fullDateTime24h"} | ${"Feb 1, 2020 23:44"} | ${"1 фев. 2020 г. 23:44"} diff --git a/__tests__/localization.test.ts b/__tests__/localization.test.ts index 98f096b0..8f46f65e 100644 --- a/__tests__/localization.test.ts +++ b/__tests__/localization.test.ts @@ -6,8 +6,8 @@ import advancedDayJsFormat from "dayjs/plugin/advancedFormat"; import LuxonUtils from "../packages/luxon/src"; import { TEST_TIMESTAMP, utilsTest } from "./test-utils"; import DayjsUtils from "../packages/dayjs/src"; -import ruLocale from "date-fns/locale/ru"; -import enAuLocale from "date-fns/locale/en-AU"; +import { ru as ruLocale } from "date-fns/locale/ru"; +import { enAU as enAuLocale } from "date-fns/locale/en-AU"; import DateFnsUtils from "../packages/date-fns/src"; import MomentUtils from "../packages/moment/src"; import JsJodaUtils from "../packages/js-joda/src"; diff --git a/packages/date-fns/package.json b/packages/date-fns/package.json index 54de1ae0..ff56d218 100644 --- a/packages/date-fns/package.json +++ b/packages/date-fns/package.json @@ -30,7 +30,7 @@ }, "license": "MIT", "peerDependencies": { - "date-fns": "^2.0.0" + "date-fns": "^3.2.0" }, "peerDependenciesMeta": { "date-fns": { @@ -41,7 +41,7 @@ "@date-io/core": "^2.17.0" }, "devDependencies": { - "date-fns": "2.16.1", + "date-fns": "^3.2.0", "rollup": "^2.0.2", "typescript": "^5.0.0" }, diff --git a/packages/date-fns/src/date-fns-utils.ts b/packages/date-fns/src/date-fns-utils.ts index c570dba3..92fcbff4 100644 --- a/packages/date-fns/src/date-fns-utils.ts +++ b/packages/date-fns/src/date-fns-utils.ts @@ -1,59 +1,57 @@ -import addDays from "date-fns/addDays"; -import addSeconds from "date-fns/addSeconds"; -import addMinutes from "date-fns/addMinutes"; -import addHours from "date-fns/addHours"; -import addWeeks from "date-fns/addWeeks"; -import addMonths from "date-fns/addMonths"; -import addYears from "date-fns/addYears"; -import differenceInYears from "date-fns/differenceInYears"; -import differenceInQuarters from "date-fns/differenceInQuarters"; -import differenceInMonths from "date-fns/differenceInMonths"; -import differenceInWeeks from "date-fns/differenceInWeeks"; -import differenceInDays from "date-fns/differenceInDays"; -import differenceInHours from "date-fns/differenceInHours"; -import differenceInMinutes from "date-fns/differenceInMinutes"; -import differenceInSeconds from "date-fns/differenceInSeconds"; -import differenceInMilliseconds from "date-fns/differenceInMilliseconds"; -import eachDayOfInterval from "date-fns/eachDayOfInterval"; -import endOfDay from "date-fns/endOfDay"; -import endOfWeek from "date-fns/endOfWeek"; -import endOfYear from "date-fns/endOfYear"; -import format from "date-fns/format"; -import getDate from "date-fns/getDate"; -import getDay from "date-fns/getDay"; -import getDaysInMonth from "date-fns/getDaysInMonth"; -import getHours from "date-fns/getHours"; -import getMinutes from "date-fns/getMinutes"; -import getMonth from "date-fns/getMonth"; -import getSeconds from "date-fns/getSeconds"; -import getYear from "date-fns/getYear"; -import isAfter from "date-fns/isAfter"; -import isBefore from "date-fns/isBefore"; -import isEqual from "date-fns/isEqual"; -import isSameDay from "date-fns/isSameDay"; -import isSameYear from "date-fns/isSameYear"; -import isSameMonth from "date-fns/isSameMonth"; -import isSameHour from "date-fns/isSameHour"; -import isValid from "date-fns/isValid"; -import dateFnsParse from "date-fns/parse"; -import setDate from "date-fns/setDate"; -import setHours from "date-fns/setHours"; -import setMinutes from "date-fns/setMinutes"; -import setMonth from "date-fns/setMonth"; -import setSeconds from "date-fns/setSeconds"; -import setYear from "date-fns/setYear"; -import startOfDay from "date-fns/startOfDay"; -import startOfMonth from "date-fns/startOfMonth"; -import endOfMonth from "date-fns/endOfMonth"; -import startOfWeek from "date-fns/startOfWeek"; -import startOfYear from "date-fns/startOfYear"; -import parseISO from "date-fns/parseISO"; -import formatISO from "date-fns/formatISO"; +import { addDays } from "date-fns/addDays"; +import { addSeconds } from "date-fns/addSeconds"; +import { addMinutes } from "date-fns/addMinutes"; +import { addHours } from "date-fns/addHours"; +import { addWeeks } from "date-fns/addWeeks"; +import { addMonths } from "date-fns/addMonths"; +import { addYears } from "date-fns/addYears"; +import { differenceInYears } from "date-fns/differenceInYears"; +import { differenceInQuarters } from "date-fns/differenceInQuarters"; +import { differenceInMonths } from "date-fns/differenceInMonths"; +import { differenceInWeeks } from "date-fns/differenceInWeeks"; +import { differenceInDays } from "date-fns/differenceInDays"; +import { differenceInHours } from "date-fns/differenceInHours"; +import { differenceInMinutes } from "date-fns/differenceInMinutes"; +import { differenceInSeconds } from "date-fns/differenceInSeconds"; +import { differenceInMilliseconds } from "date-fns/differenceInMilliseconds"; +import { eachDayOfInterval } from "date-fns/eachDayOfInterval"; +import { endOfDay } from "date-fns/endOfDay"; +import { endOfWeek } from "date-fns/endOfWeek"; +import { endOfYear } from "date-fns/endOfYear"; +import { format, longFormatters } from "date-fns/format"; +import { getDate } from "date-fns/getDate"; +import { getDay } from "date-fns/getDay"; +import { getDaysInMonth } from "date-fns/getDaysInMonth"; +import { getHours } from "date-fns/getHours"; +import { getMinutes } from "date-fns/getMinutes"; +import { getMonth } from "date-fns/getMonth"; +import { getSeconds } from "date-fns/getSeconds"; +import { getYear } from "date-fns/getYear"; +import { isAfter } from "date-fns/isAfter"; +import { isBefore } from "date-fns/isBefore"; +import { isEqual } from "date-fns/isEqual"; +import { isSameDay } from "date-fns/isSameDay"; +import { isSameYear } from "date-fns/isSameYear"; +import { isSameMonth } from "date-fns/isSameMonth"; +import { isSameHour } from "date-fns/isSameHour"; +import { isValid } from "date-fns/isValid"; +import { parse as dateFnsParse } from "date-fns/parse"; +import { setDate } from "date-fns/setDate"; +import { setHours } from "date-fns/setHours"; +import { setMinutes } from "date-fns/setMinutes"; +import { setMonth } from "date-fns/setMonth"; +import { setSeconds } from "date-fns/setSeconds"; +import { setYear } from "date-fns/setYear"; +import { startOfDay } from "date-fns/startOfDay"; +import { startOfMonth } from "date-fns/startOfMonth"; +import { endOfMonth } from "date-fns/endOfMonth"; +import { startOfWeek } from "date-fns/startOfWeek"; +import { startOfYear } from "date-fns/startOfYear"; +import { parseISO } from "date-fns/parseISO"; +import { formatISO } from "date-fns/formatISO"; import { IUtils, DateIOFormats, Unit } from "@date-io/core/IUtils"; -import isWithinInterval from "date-fns/isWithinInterval"; -// @ts-ignore -import longFormatters from "date-fns/_lib/format/longFormatters"; -import defaultLocale from "date-fns/locale/en-US"; +import { isWithinInterval } from "date-fns/isWithinInterval"; +import { enUS as defaultLocale } from "date-fns/locale/en-US"; type Locale = typeof defaultLocale; @@ -62,16 +60,16 @@ const defaultFormats: DateIOFormats = { fullDate: "PP", fullDateWithWeekday: "PPPP", fullDateTime: "PP p", - fullDateTime12h: "PP hh:mm aaa", + fullDateTime12h: "PP hh:mm aa", fullDateTime24h: "PP HH:mm", fullTime: "p", - fullTime12h: "hh:mm aaa", + fullTime12h: "hh:mm aa", fullTime24h: "HH:mm", hours12h: "hh", hours24h: "HH", keyboardDate: "P", keyboardDateTime: "P p", - keyboardDateTime12h: "P hh:mm aaa", + keyboardDateTime12h: "P hh:mm aa", keyboardDateTime24h: "P HH:mm", minutes: "mm", month: "LLLL", @@ -104,7 +102,7 @@ export default class DateFnsUtils implements IUtils { // strict signature and delegate to the more lenient signature. Otherwise, we have downstream type errors upon usage. public is12HourCycleInCurrentLocale = () => { if (this.locale) { - return /a/.test(this.locale.formatLong?.time()); + return /a/.test(this.locale.formatLong?.time({})); } // By default date-fns is using en-US locale with am/pm enabled @@ -123,7 +121,7 @@ export default class DateFnsUtils implements IUtils { const firstCharacter = token[0]; if (firstCharacter === "p" || firstCharacter === "P") { const longFormatter = longFormatters[firstCharacter]; - return longFormatter(token, locale.formatLong, {}); + return longFormatter(token, locale.formatLong); } return token; }) @@ -436,7 +434,7 @@ export default class DateFnsUtils implements IUtils { let count = 0; let current = start; const nestedWeeks: Date[][] = []; - let lastDay = null; + let lastDay = null as null | number; while (isBefore(current, end)) { const weekNumber = Math.floor(count / 7); nestedWeeks[weekNumber] = nestedWeeks[weekNumber] || [];