-
Notifications
You must be signed in to change notification settings - Fork 85
/
index.ts
169 lines (147 loc) · 3.72 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { Locale as DateFnsLocale } from "date-fns";
// The order of this array is important, when an input value matches more than one format the last one is used
const EU_FORMATS = [
"d M yyyy",
"dd M yyyy",
"d MM yyyy",
"dd MM yyyy",
"d M yy",
"dd M yy",
"d MM yy",
"dd MM yy",
"d",
"d M",
"dd",
"d MM",
"dd M",
"dd MM",
];
// The order of this array is important, when an input value matches more than one format the last one is used
const NA_FORMATS = [
"M",
"M d",
"MM",
"M dd",
"MM d",
"MM dd",
"M d yy",
"MM d yy",
"M dd yy",
"MM dd yy",
"M d yyyy",
"MM d yyyy",
"M dd yyyy",
"MM dd yyyy",
];
// The order of this array is important, when an input value matches more than one format the last one is used
const CN_FORMATS = [
"yyyy M",
"yyyy M d",
"yyyy MM d",
"yyyy M dd",
"yyyy MM dd",
"yy M",
"yy MM",
"yy M d",
"yy MM d",
"yy M dd",
"yy MM dd",
"M",
"M d",
"MM",
"M dd",
"MM d",
"MM dd",
];
const SEPARATORS = ["", ".", ",", "-", "/", ":"] as const;
const STANDARD_FORMAT_LENGTH = 10;
type Separator = typeof SEPARATORS[number];
const generateFormats = (
formatArray: string[],
separator: Separator,
trailingChar?: string
): string[] => {
const separators = SEPARATORS.includes(separator)
? SEPARATORS
: [...SEPARATORS, separator];
return formatArray.reduce((arr: string[], formatString) => {
const array = [...arr, formatString];
if (formatString.includes(" ")) {
separators.forEach((char) => {
if (separator === char && trailingChar) {
array.push(`${formatString.replace(/ /g, char)}${trailingChar}`);
}
array.push(formatString.replace(/ /g, char));
});
}
return array;
}, []);
};
const getOutputFormatForLocale = (localeCode: string) => {
const formatMap = {
day: "dd",
month: "MM",
year: "yyyy",
};
const formatter = new Intl.DateTimeFormat(localeCode);
let separator: Separator = "";
const format = formatter
.formatToParts(new Date())
.map(({ type, value }) => {
if (["day", "month", "year"].includes(type)) {
return formatMap[type as keyof typeof formatMap];
}
if (!separator) {
separator = value as Separator;
}
return value;
})
.join("");
if (localeCode.startsWith("bg")) {
// this locale adds an additional char that has no effect on the output formatting
return { format: format.substring(0, STANDARD_FORMAT_LENGTH), separator };
}
return { format, separator };
};
const getInputFormatsArrayForLocale = (format: string) => {
if (format.startsWith("y")) {
return CN_FORMATS;
}
if (format.startsWith("M")) {
return NA_FORMATS;
}
return EU_FORMATS;
};
// we need this to handle for formats that add extra chars at the end of the format
const getTrailingChar = (format: string) => {
const lastChar = format.split("").pop() as string;
return ["y", "M", "d"].includes(lastChar) ? "" : lastChar;
};
interface LocaleFormats {
formats: string[];
format: string;
}
const getFormatData = ({ code = "en-GB" }: DateFnsLocale): LocaleFormats => {
if (["en-CA", "en-US"].includes(code)) {
const format = "MM/dd/yyyy";
const formats = getInputFormatsArrayForLocale(format);
return {
format,
formats: generateFormats(formats, "/"),
};
}
const { format, separator } = getOutputFormatForLocale(code);
const outputFormat = ["fr-CA", "en-ZA", "ar-EG"].includes(code)
? "dd/MM/yyyy"
: format;
const formatsForLocale = getInputFormatsArrayForLocale(outputFormat);
return {
format: outputFormat,
formats: generateFormats(
formatsForLocale,
separator,
getTrailingChar(format)
),
};
};
export default getFormatData;