@louis.jln/extract-date 3.0.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.
- package/.flowconfig +3 -0
- package/LICENSE +24 -0
- package/README.md +184 -0
- package/bundle/extract-date.js +61276 -0
- package/dist/calculateSpecificity.js +23 -0
- package/dist/calculateSpecificity.js.flow +21 -0
- package/dist/calculateSpecificity.js.map +1 -0
- package/dist/createFormats.js +165 -0
- package/dist/createFormats.js.flow +373 -0
- package/dist/createFormats.js.map +1 -0
- package/dist/createMovingChunks.js +20 -0
- package/dist/createMovingChunks.js.flow +19 -0
- package/dist/createMovingChunks.js.map +1 -0
- package/dist/dictionary.json +3792 -0
- package/dist/extractDate.js +148 -0
- package/dist/extractDate.js.flow +214 -0
- package/dist/extractDate.js.map +1 -0
- package/dist/extractRelativeDate.js +32 -0
- package/dist/extractRelativeDate.js.flow +34 -0
- package/dist/extractRelativeDate.js.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.js.flow +6 -0
- package/dist/index.js.map +1 -0
- package/dist/normalizeInput.js +22 -0
- package/dist/normalizeInput.js.flow +26 -0
- package/dist/normalizeInput.js.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.flow +23 -0
- package/dist/types.js.map +1 -0
- package/package.json +96 -0
- package/src/calculateSpecificity.js +21 -0
- package/src/createFormats.js +373 -0
- package/src/createMovingChunks.js +19 -0
- package/src/dictionary.json +3792 -0
- package/src/extractDate.js +214 -0
- package/src/extractRelativeDate.js +34 -0
- package/src/index.js +6 -0
- package/src/normalizeInput.js +26 -0
- package/src/types.js +23 -0
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
/* eslint-disable no-continue, no-negated-condition, import/no-namespace */
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
format as formatDate,
|
|
7
|
+
parse as parseDate,
|
|
8
|
+
isValid as isValidDate,
|
|
9
|
+
} from 'date-fns';
|
|
10
|
+
import * as locales from 'date-fns/locale';
|
|
11
|
+
import moment from 'moment-timezone';
|
|
12
|
+
import dictionary from 'relative-date-names';
|
|
13
|
+
import createMovingChunks from './createMovingChunks';
|
|
14
|
+
import extractRelativeDate from './extractRelativeDate';
|
|
15
|
+
import createFormats from './createFormats';
|
|
16
|
+
import normalizeInput from './normalizeInput';
|
|
17
|
+
import type {
|
|
18
|
+
ConfigurationType,
|
|
19
|
+
DateMatchType,
|
|
20
|
+
UserConfigurationType,
|
|
21
|
+
} from './types';
|
|
22
|
+
|
|
23
|
+
const defaultConfiguration = {
|
|
24
|
+
maximumAge: Infinity,
|
|
25
|
+
minimumAge: Infinity,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const formats = createFormats();
|
|
29
|
+
|
|
30
|
+
const dateFnsLocaleMap = {
|
|
31
|
+
en: 'enUS',
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// eslint-disable-next-line complexity
|
|
35
|
+
export default (input: string, userConfiguration: UserConfigurationType = defaultConfiguration): $ReadOnlyArray<DateMatchType> => {
|
|
36
|
+
const normalizedInput = normalizeInput(input);
|
|
37
|
+
|
|
38
|
+
const configuration: ConfigurationType = {
|
|
39
|
+
...defaultConfiguration,
|
|
40
|
+
...userConfiguration,
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const locale = configuration.locale || 'en';
|
|
44
|
+
|
|
45
|
+
const dateFnsLocale = locales[dateFnsLocaleMap[locale] || locale];
|
|
46
|
+
|
|
47
|
+
if (!dateFnsLocale) {
|
|
48
|
+
throw new Error('No translation available for the target locale (date-fns).');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!dictionary[locale]) {
|
|
52
|
+
throw new Error('No translation available for the target locale (relative dates).');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (configuration.timezone && !moment.tz.zone(configuration.timezone)) {
|
|
56
|
+
throw new Error('Unrecognized timezone.');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (configuration.maximumAge && configuration.maximumAge < 0) {
|
|
60
|
+
throw new Error('`maximumAge` must be a positive number.');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (configuration.minimumAge && configuration.minimumAge < 0) {
|
|
64
|
+
throw new Error('`minimumAge` must be a positive number.');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let words = normalizedInput.split(' ');
|
|
68
|
+
|
|
69
|
+
const matches = [];
|
|
70
|
+
|
|
71
|
+
const baseDate = parseDate('12:00', 'HH:mm', new Date());
|
|
72
|
+
|
|
73
|
+
for (const format of formats) {
|
|
74
|
+
const movingChunks = createMovingChunks(words, format.wordCount);
|
|
75
|
+
|
|
76
|
+
let chunkIndex = 0;
|
|
77
|
+
|
|
78
|
+
for (const movingChunk of movingChunks) {
|
|
79
|
+
const wordOffset = ++chunkIndex * format.wordCount;
|
|
80
|
+
|
|
81
|
+
const subject = movingChunk.join(' ');
|
|
82
|
+
|
|
83
|
+
if (format.dateFnsFormat === 'R') {
|
|
84
|
+
if (!configuration.locale) {
|
|
85
|
+
} else if (!configuration.timezone) {
|
|
86
|
+
} else {
|
|
87
|
+
const maybeDate = extractRelativeDate(subject, configuration.locale, configuration.timezone);
|
|
88
|
+
|
|
89
|
+
if (maybeDate) {
|
|
90
|
+
words = words.slice(wordOffset);
|
|
91
|
+
|
|
92
|
+
matches.push({
|
|
93
|
+
date: maybeDate,
|
|
94
|
+
originalText: subject,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
} else if (format.dateFnsFormat === 'EEE' || format.dateFnsFormat === 'EEEE') {
|
|
99
|
+
const date = parseDate(
|
|
100
|
+
subject,
|
|
101
|
+
format.dateFnsFormat,
|
|
102
|
+
baseDate,
|
|
103
|
+
{
|
|
104
|
+
locale: dateFnsLocale,
|
|
105
|
+
},
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
if (isValidDate(date)) {
|
|
109
|
+
words = words.slice(wordOffset);
|
|
110
|
+
|
|
111
|
+
matches.push({
|
|
112
|
+
date: formatDate(date, 'yyyy-MM-dd'),
|
|
113
|
+
originalText: subject,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
const yearIsExplicit = typeof format.yearIsExplicit === 'boolean' ? format.yearIsExplicit : true;
|
|
118
|
+
|
|
119
|
+
if (yearIsExplicit) {
|
|
120
|
+
const date = parseDate(
|
|
121
|
+
subject,
|
|
122
|
+
format.dateFnsFormat,
|
|
123
|
+
baseDate,
|
|
124
|
+
{
|
|
125
|
+
locale: dateFnsLocale,
|
|
126
|
+
},
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
if (!isValidDate(date)) {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const formatDirection = format.direction;
|
|
134
|
+
const configurationDirection = configuration.direction;
|
|
135
|
+
|
|
136
|
+
if (formatDirection && configurationDirection && format.dateFnsFormat.includes('yyyy') && formatDirection.replace('Y', '') === configurationDirection.replace('Y', '')) {
|
|
137
|
+
} else if (format.direction && format.direction !== configuration.direction) {
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (format.direction && !configuration.direction) {
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
words = words.slice(wordOffset);
|
|
146
|
+
|
|
147
|
+
matches.push({
|
|
148
|
+
date: formatDate(date, 'yyyy-MM-dd'),
|
|
149
|
+
originalText: subject,
|
|
150
|
+
});
|
|
151
|
+
} else {
|
|
152
|
+
const date = parseDate(
|
|
153
|
+
subject,
|
|
154
|
+
format.dateFnsFormat,
|
|
155
|
+
baseDate,
|
|
156
|
+
{
|
|
157
|
+
locale: dateFnsLocale,
|
|
158
|
+
},
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
if (!isValidDate(date)) {
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const currentYear = parseInt(formatDate(baseDate, 'yyyy'), 10);
|
|
166
|
+
|
|
167
|
+
const currentMonth = parseInt(formatDate(baseDate, 'M'), 10) + currentYear * 12;
|
|
168
|
+
const parsedMonth = parseInt(formatDate(date, 'M'), 10) + parseInt(formatDate(date, 'yyyy'), 10) * 12;
|
|
169
|
+
const difference = parsedMonth - currentMonth;
|
|
170
|
+
|
|
171
|
+
let useYear;
|
|
172
|
+
|
|
173
|
+
if (difference >= configuration.maximumAge) {
|
|
174
|
+
useYear = currentYear - 1;
|
|
175
|
+
} else if (difference < 0 && Math.abs(difference) >= configuration.minimumAge) {
|
|
176
|
+
useYear = currentYear + 1;
|
|
177
|
+
} else {
|
|
178
|
+
useYear = currentYear;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const maybeDate = parseDate(
|
|
182
|
+
useYear + '-' + formatDate(date, 'MM-dd'),
|
|
183
|
+
'yyyy-MM-dd',
|
|
184
|
+
baseDate,
|
|
185
|
+
{
|
|
186
|
+
locale: dateFnsLocale,
|
|
187
|
+
},
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
if (!isValidDate(maybeDate)) {
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (format.direction && format.direction !== configuration.direction) {
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (format.direction && !configuration.direction) {
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
words = words.slice(wordOffset);
|
|
203
|
+
|
|
204
|
+
matches.push({
|
|
205
|
+
date: formatDate(maybeDate, 'yyyy-MM-dd'),
|
|
206
|
+
originalText: subject,
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
return matches;
|
|
214
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
import moment from 'moment-timezone';
|
|
4
|
+
import dictionary from 'relative-date-names';
|
|
5
|
+
|
|
6
|
+
export default (subject: string, locale: string, timezone: string): ?string => {
|
|
7
|
+
const translation = dictionary[locale];
|
|
8
|
+
|
|
9
|
+
if (!translation) {
|
|
10
|
+
throw new Error('No translation available for the target locale.');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (!moment.tz.zone(timezone)) {
|
|
14
|
+
throw new Error('Unrecognized timezone.');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const normalizedSubject = subject.toLowerCase();
|
|
18
|
+
|
|
19
|
+
const now = moment();
|
|
20
|
+
|
|
21
|
+
if (normalizedSubject === translation.day.relative.yesterday) {
|
|
22
|
+
return now.subtract(1, 'day').format('YYYY-MM-DD');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (normalizedSubject === translation.day.relative.today) {
|
|
26
|
+
return now.format('YYYY-MM-DD');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (normalizedSubject === translation.day.relative.tomorrow) {
|
|
30
|
+
return now.add(1, 'day').format('YYYY-MM-DD');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return null;
|
|
34
|
+
};
|
package/src/index.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
export default (input: string): string => {
|
|
4
|
+
let lastInput = input;
|
|
5
|
+
|
|
6
|
+
while (true) {
|
|
7
|
+
const result = lastInput
|
|
8
|
+
|
|
9
|
+
// 2019-02-12T00:00:00
|
|
10
|
+
.replace(/(\d+)T(\d+)/, '$1 $2')
|
|
11
|
+
|
|
12
|
+
.replace(/(\d+)\s\/\s(\d+)/, '$1/$2')
|
|
13
|
+
|
|
14
|
+
.replace(/[.:;] /g, ' ')
|
|
15
|
+
.trim()
|
|
16
|
+
.replace(/[.!#?]+$/g, '');
|
|
17
|
+
|
|
18
|
+
if (result === lastInput) {
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
lastInput = result;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
throw new Error('Unexpected state.');
|
|
26
|
+
};
|
package/src/types.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
3
|
+
export type DateMatchType = {|
|
|
4
|
+
+date: string,
|
|
5
|
+
|};
|
|
6
|
+
|
|
7
|
+
export type DirectionType = 'DM' | 'DMY' | 'DYM' | 'MD' | 'YDM' | 'YMD' | 'MDY';
|
|
8
|
+
|
|
9
|
+
export type UserConfigurationType = {|
|
|
10
|
+
+direction?: DirectionType,
|
|
11
|
+
+locale?: string,
|
|
12
|
+
+maximumAge?: number,
|
|
13
|
+
+minimumAge?: number,
|
|
14
|
+
+timezone?: string,
|
|
15
|
+
|};
|
|
16
|
+
|
|
17
|
+
export type ConfigurationType = {|
|
|
18
|
+
+direction?: DirectionType,
|
|
19
|
+
+locale?: string,
|
|
20
|
+
+maximumAge: number,
|
|
21
|
+
+minimumAge: number,
|
|
22
|
+
+timezone?: string,
|
|
23
|
+
|};
|