-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathdate.ts
155 lines (125 loc) · 4.19 KB
/
date.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
import { indexAboveValue, isNegative, groupArrayByValueAtIndex } from './utils';
/**
* Takes an array of numeric dates and tries to understand if the days come
* before the month or the other way around by checking if numbers go above
* `12`.
*
* Output is `true` if days are first, `false` if they are second, or `null` if
* it failed to understand the order.
*/
function checkAbove12(numericDates: number[][]): boolean | null {
const daysFirst = numericDates.some(indexAboveValue(0, 12));
if (daysFirst) return true;
const daysSecond = numericDates.some(indexAboveValue(1, 12));
if (daysSecond) return false;
return null;
}
/**
* Takes an array of numeric dates and tries to understand if the days come
* before the month or the other way around by checking if a set of numbers
* during the same year decrease at some point.
*
* If it does it's probably the days since months can only increase in a given
* year.
*
* Output is `true` if days are first, `false` if they are second, or `null` if
* it failed to understand the order.
*/
function checkDecreasing(numericDates: number[][]): boolean | null {
const datesByYear = groupArrayByValueAtIndex(numericDates, 2);
const results = datesByYear.map(dates => {
const daysFirst = dates.slice(1).some((date, i) => {
const [first1] = dates[i];
const [first2] = date;
return isNegative(first2 - first1);
});
if (daysFirst) return true;
const daysSecond = dates.slice(1).some((date, i) => {
const [, second1] = dates[i];
const [, second2] = date;
return isNegative(second2 - second1);
});
if (daysSecond) return false;
return null;
});
const anyTrue = results.some(value => value === true);
if (anyTrue) return true;
const anyFalse = results.some(value => value === false);
if (anyFalse) return false;
return null;
}
/**
* Takes an array of numeric dates and tries to understand if the days come
* before the month or the other way around by looking at which number changes
* more frequently.
*
* Output is `true` if days are first, `false` if they are second, or `null` if
* it failed to understand the order.
*/
function changeFrequencyAnalysis(numericDates: number[][]): boolean | null {
const diffs = numericDates
.slice(1)
.map((date, i) => date.map((num, j) => Math.abs(numericDates[i][j] - num)));
const [first, second] = diffs.reduce(
(total, diff) => {
const [first1, second1] = total;
const [first2, second2] = diff;
return [first1 + first2, second1 + second2];
},
[0, 0],
);
if (first > second) return true;
if (first < second) return false;
return null;
}
/**
* Takes an array of numeric dates and tries to understand if the days come
* before the month or the other way around by running the dates through various
* checks.
*
* Output is `true` if days are first, `false` if they are second, or `null` if
* it failed to understand the order.
*/
function daysBeforeMonths(numericDates: number[][]): boolean | null {
const firstCheck = checkAbove12(numericDates);
if (firstCheck !== null) return firstCheck;
const secondCheck = checkDecreasing(numericDates);
if (secondCheck !== null) return secondCheck;
return changeFrequencyAnalysis(numericDates);
}
/**
* Takes `year`, `month` and `day` as strings and pads them to `4`, `2`, `2`
* digits respectively.
*/
function normalizeDate(
year: string,
month: string,
day: string,
): [string, string, string] {
return [
// 2 digit years are assumed to be in the 2000-2099 range
year.padStart(4, '2000'),
month.padStart(2, '0'),
day.padStart(2, '0'),
];
}
/**
* Pushes the longest number in a date to the end, if there is one. Necessary to
* ensure the year is the last number.
*/
function orderDateComponents(date: string): [string, string, string] {
const regexSplitDate = /[-/.] ?/;
const [a, b, c] = date.split(regexSplitDate);
const maxLength = Math.max(a.length, b.length, c.length);
if (c.length === maxLength) return [a, b, c];
if (b.length === maxLength) return [a, c, b];
return [b, c, a];
}
export {
checkAbove12,
checkDecreasing,
changeFrequencyAnalysis,
daysBeforeMonths,
normalizeDate,
orderDateComponents,
};