@simplybusiness/mobius 5.26.3 → 5.27.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,82 @@
1
+ import { convertToDateFormat, isValidDate } from "./validation";
2
+
3
+ describe("convertToDateFormat", () => {
4
+ it("converts a date from yyyy-mm-dd to yyyy-mm-dd format when no custom format is supplied", () => {
5
+ expect(convertToDateFormat("2022-01-25")).toBe("2022-01-25");
6
+ });
7
+ it("converts a date from yyyy-mm-dd to yyyy-mm-dd format", () => {
8
+ expect(convertToDateFormat("2022-01-25", "yyyy-mm-dd")).toBe("2022-01-25");
9
+ });
10
+
11
+ it("converts a date from dd/mm/yyyy to yyyy-mm-dd format", () => {
12
+ expect(convertToDateFormat("25/01/2022", "dd/mm/yyyy")).toBe("2022-01-25");
13
+ });
14
+
15
+ it("converts a date from mm/dd/yyyy to yyyy-mm-dd format", () => {
16
+ expect(convertToDateFormat("01/25/2022", "mm/dd/yyyy")).toBe("2022-01-25");
17
+ });
18
+
19
+ it("handles single digit days and months with padding", () => {
20
+ expect(convertToDateFormat("5/6/2022", "dd/mm/yyyy")).toBe("2022-06-05");
21
+ expect(convertToDateFormat("6/5/2022", "mm/dd/yyyy")).toBe("2022-06-05");
22
+ });
23
+
24
+ it("handles missing parts by using default values", () => {
25
+ expect(convertToDateFormat("2022-01", "yyyy-mm")).toBe("2022-01-00");
26
+ expect(convertToDateFormat("01/2022", "mm/yyyy")).toBe("2022-01-00");
27
+ });
28
+
29
+ it("handles empty string by using default values", () => {
30
+ expect(convertToDateFormat("", "yyyy-mm-dd")).toBe("0-00-00");
31
+ });
32
+ });
33
+
34
+ describe("isValidDate", () => {
35
+ it("returns true for undefined date", () => {
36
+ expect(isValidDate(undefined)).toBe(true);
37
+ });
38
+
39
+ it("returns true for valid date in default format yyyy-mm-dd", () => {
40
+ expect(isValidDate("2022-01-25")).toBe(true);
41
+ });
42
+
43
+ it("returns false when the format parts length does not match date parts", () => {
44
+ expect(isValidDate("2022-01")).toBe(false);
45
+ });
46
+
47
+ it("returns false for invalid month value", () => {
48
+ expect(isValidDate("2022-13-01")).toBe(false);
49
+ expect(isValidDate("2022-00-01")).toBe(false);
50
+ });
51
+
52
+ it("returns false for invalid day value", () => {
53
+ expect(isValidDate("2022-01-32")).toBe(false);
54
+ expect(isValidDate("2022-01-00")).toBe(false);
55
+ });
56
+
57
+ it("validates February days correctly in leap year", () => {
58
+ expect(isValidDate("2020-02-29")).toBe(true);
59
+ expect(isValidDate("2020-02-30")).toBe(false);
60
+ });
61
+
62
+ it("validates February days correctly in non-leap year", () => {
63
+ expect(isValidDate("2021-02-28")).toBe(true);
64
+ expect(isValidDate("2021-02-29")).toBe(false);
65
+ });
66
+
67
+ it("validates different months with different days", () => {
68
+ expect(isValidDate("2022-04-30")).toBe(true);
69
+ expect(isValidDate("2022-04-31")).toBe(false);
70
+ expect(isValidDate("2022-05-31")).toBe(true);
71
+ });
72
+
73
+ it("handles custom format dd/mm/yyyy", () => {
74
+ expect(isValidDate("25/01/2022", "dd/mm/yyyy")).toBe(true);
75
+ expect(isValidDate("32/01/2022", "dd/mm/yyyy")).toBe(false);
76
+ });
77
+
78
+ it("handles custom format mm/dd/yyyy", () => {
79
+ expect(isValidDate("01/25/2022", "mm/dd/yyyy")).toBe(true);
80
+ expect(isValidDate("13/25/2022", "mm/dd/yyyy")).toBe(false);
81
+ });
82
+ });
@@ -0,0 +1,43 @@
1
+ // Convert a custom format date string to the format "yyyy-mm-dd".
2
+ export const convertToDateFormat = (date: string, format?: string): string => {
3
+ const dateParts = date.split(/[-/]/);
4
+ const formatParts = format ? format.split(/[-/]/) : ["yyyy", "mm", "dd"];
5
+ const dateObj: { [key: string]: number } = {};
6
+ const formattedDate: string[] = [];
7
+
8
+ for (let i = 0; i < formatParts.length; i++) {
9
+ dateObj[formatParts[i]] = dateParts[i] ? parseInt(dateParts[i], 10) : 0;
10
+ }
11
+
12
+ formattedDate.push(dateObj["yyyy"]?.toString() || "0000");
13
+ formattedDate.push(
14
+ (dateObj["mm"] < 10 ? "0" : "") + (dateObj["mm"]?.toString() || "00"),
15
+ );
16
+ formattedDate.push(
17
+ (dateObj["dd"] < 10 ? "0" : "") + (dateObj["dd"]?.toString() || "00"),
18
+ );
19
+
20
+ return formattedDate.join("-");
21
+ };
22
+
23
+ export const isValidDate = (date?: string, format = "yyyy-mm-dd"): boolean => {
24
+ if (!date) return true;
25
+
26
+ // Convert date to standard format
27
+ const standardDate = convertToDateFormat(date, format);
28
+
29
+ // Parse the standardized date
30
+ const [yearStr, monthStr, dayStr] = standardDate.split("-");
31
+ const year = parseInt(yearStr, 10);
32
+ const month = parseInt(monthStr, 10);
33
+ const day = parseInt(dayStr, 10);
34
+
35
+ // Validate month range
36
+ if (month < 1 || month > 12) {
37
+ return false;
38
+ }
39
+
40
+ // Check if day is valid for the month
41
+ const daysInMonth = new Date(year, month, 0).getDate();
42
+ return day > 0 && day <= daysInMonth;
43
+ };
@@ -7,6 +7,7 @@ export * from "./Button";
7
7
  export * from "./Checkbox";
8
8
  export * from "./Combobox";
9
9
  export * from "./Container";
10
+ export * from "./DateField";
10
11
  export * from "./Divider";
11
12
  export * from "./Drawer";
12
13
  export * from "./DropdownMenu";