Skip to content

Commit

Permalink
chore: replace date-fns with native implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandresoro committed Sep 22, 2024
1 parent 37fe138 commit dec46f1
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 18 deletions.
1 change: 0 additions & 1 deletion packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"bullmq": "5.13.0",
"content-disposition": "0.5.4",
"csv-parse": "5.5.6",
"date-fns": "3.6.0",
"escape-string-regexp": "5.0.0",
"exceljs": "4.4.0",
"fastify": "4.28.1",
Expand Down
20 changes: 11 additions & 9 deletions packages/backend/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { parse } from "date-fns";
import { fr as locale } from "date-fns/locale";

export const areSetsContainingSameValues = <T>(firstArray: Set<T>, secondArray: Set<T>): boolean => {
// biome-ignore lint/style/useBlockStatements: <explanation>
if (firstArray.size !== secondArray.size) return false;
Expand All @@ -14,17 +11,22 @@ export const isIdInListIds = <T>(ids: Set<T>, idToFind: T): boolean => {
};

export const getFormattedDate = (value: string): Date | null => {
const dateFormat = "dd/MM/yyyy";
// Supported format is: dd/MM/yyyy

const parsedDate = parse(value, dateFormat, new Date(), {
locale,
});
const parts = value.split("/");
if (parts.length !== 3) {
return null;
}
const year = Number.parseInt(parts[2]);
const month = Number.parseInt(parts[1]) - 1;
const day = Number.parseInt(parts[0]);

// As date-fns parsing is not exact and could provide surprises, we check that we indeed parsed a proper year
if (parsedDate.getFullYear() < 1000) {
if (year < 1000 || month < 0 || month > 11 || day < 1 || day > 31) {
return null;
}

const parsedDate = new Date(year, month, day);

// Invalid date is represented by NaN which is not === to itself
// biome-ignore lint/suspicious/noSelfCompare: <explanation>
if (parsedDate.getTime() === parsedDate.getTime()) {
Expand Down
8 changes: 0 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dec46f1

Please # to comment.