@navikt/ds-react 2.0.17 → 2.1.0

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.
@@ -1,4 +1,6 @@
1
1
  import parse from "date-fns/parse";
2
+ import isBefore from "date-fns/isBefore";
3
+ import sub from "date-fns/sub";
2
4
  import { isValidDate } from ".";
3
5
 
4
6
  export const INPUT_DATE_STRING_FORMAT_DATE = "dd.MM.yyyy";
@@ -22,7 +24,60 @@ const ALLOWED_INPUT_FORMATS_MONTH = [
22
24
  ...ALLOWED_INPUT_FORMATS_DATE,
23
25
  ];
24
26
 
25
- const isTwoDigitYear = (dateString, today, locale, formats) => {
27
+ export const parseDate = (
28
+ date: string,
29
+ today: Date,
30
+ locale: Locale,
31
+ type: "date" | "month",
32
+ allowTwoDigitYear: boolean
33
+ ): Date => {
34
+ let parsed;
35
+ const ALLOWED_FORMATS =
36
+ type === "date" ? ALLOWED_INPUT_FORMATS_DATE : ALLOWED_INPUT_FORMATS_MONTH;
37
+
38
+ if (allowTwoDigitYear) {
39
+ for (const format of ALLOWED_FORMATS) {
40
+ parsed = parse(date, format, today, { locale });
41
+ if (
42
+ isValidDate(parsed) &&
43
+ !isTwoDigitYear(date, today, locale, ALLOWED_FORMATS)
44
+ ) {
45
+ return parsed;
46
+ }
47
+ }
48
+ for (const format of [
49
+ ...ALLOWED_FORMATS.map((x) => x.replace("yyyy", "yy")),
50
+ ]) {
51
+ parsed = parse(date, format, today, { locale });
52
+ if (
53
+ isValidDate(parsed) &&
54
+ isTwoDigitYear(date, today, locale, ALLOWED_FORMATS)
55
+ ) {
56
+ const convertedDate = assignCenturyToDate(date, format, today, locale);
57
+
58
+ if (isValidDate(new Date(convertedDate))) {
59
+ return new Date(convertedDate);
60
+ } else {
61
+ return new Date("Invalid date");
62
+ }
63
+ }
64
+ }
65
+ return new Date("Invalid date");
66
+ } else {
67
+ for (const format of ALLOWED_FORMATS) {
68
+ parsed = parse(date, format, today, { locale });
69
+ if (
70
+ isValidDate(parsed) &&
71
+ !isTwoDigitYear(date, today, locale, ALLOWED_FORMATS)
72
+ ) {
73
+ return parsed;
74
+ }
75
+ }
76
+ return new Date("Invalid date");
77
+ }
78
+ };
79
+
80
+ function isTwoDigitYear(dateString, today, locale, formats) {
26
81
  let parsed;
27
82
  const newFormat = formats.map((x) => x.replace("yyyy", "yy"));
28
83
  for (const format of newFormat) {
@@ -32,25 +87,49 @@ const isTwoDigitYear = (dateString, today, locale, formats) => {
32
87
  }
33
88
  }
34
89
  return false;
35
- };
90
+ }
36
91
 
37
- export const parseDate = (
38
- date: string,
92
+ function assignCenturyToDate(
93
+ dateStr: string,
94
+ format: string,
39
95
  today: Date,
40
- locale: Locale,
41
- type: "date" | "month"
42
- ): Date => {
43
- let parsed;
44
- const ALLOWED_FORMATS =
45
- type === "date" ? ALLOWED_INPUT_FORMATS_DATE : ALLOWED_INPUT_FORMATS_MONTH;
46
- for (const format of ALLOWED_FORMATS) {
47
- parsed = parse(date, format, today, { locale });
48
- if (
49
- isValidDate(parsed) &&
50
- !isTwoDigitYear(date, today, locale, ALLOWED_FORMATS)
51
- ) {
52
- return parsed;
53
- }
96
+ locale: Locale
97
+ ) {
98
+ const date20Century = parse(
99
+ appendCenturyToTwoYearDigitDateString(dateStr, "19"),
100
+ format.replace("yy", "yyyy"),
101
+ today,
102
+ { locale }
103
+ );
104
+
105
+ const date21Century = parse(
106
+ appendCenturyToTwoYearDigitDateString(dateStr, "20"),
107
+ format.replace("yy", "yyyy"),
108
+ today,
109
+ { locale }
110
+ );
111
+
112
+ if (isValidDate(date20Century) && isValidDate(date21Century)) {
113
+ return isBefore(
114
+ date20Century,
115
+ sub(new Date(), {
116
+ years: 80,
117
+ })
118
+ )
119
+ ? date21Century
120
+ : date20Century;
54
121
  }
122
+
55
123
  return new Date("Invalid date");
56
- };
124
+ }
125
+
126
+ function appendCenturyToTwoYearDigitDateString(
127
+ dateString: string,
128
+ century: "19" | "20"
129
+ ) {
130
+ const twoDigitYear = dateString.slice(-2);
131
+ return `${dateString.slice(
132
+ 0,
133
+ dateString.length - 2
134
+ )}${century}${twoDigitYear}`;
135
+ }