forked from WorldBrain/Memex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnlp-time-filter.js
70 lines (59 loc) · 1.77 KB
/
nlp-time-filter.js
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
import chrono from 'chrono-node'
const BEFORE_REGEX = /before:[''"](.+)['"]/i
const AFTER_REGEX = /after:['"](.+)['"]/i
/**
* @typedef QueryFilters
* @type {Object}
* @property {string} query The non-date-filter search terms.
* @property {number} [startDate] Number of ms representing the start of filter time.
* @property {number} [endDate] Number of ms representing the end of filter time.
*/
/**
* Takes in query as a string and extracts startDate, endDate, and query parts.
*
* @param {string} query The query string that user has entered.
* @returns {QueryFilters} The extracted query parameters.
*/
export default function extractTimeFiltersFromQuery(query) {
const matchedBefore = query.match(BEFORE_REGEX)
const matchedAfter = query.match(AFTER_REGEX)
let startDate
let endDate
if (matchedBefore) {
const parsedDate = chrono.parseDate(matchedBefore[1])
endDate = parsedDate && parsedDate.getTime()
}
if (matchedAfter) {
const parsedDate = chrono.parseDate(matchedAfter[1])
startDate = parsedDate && parsedDate.getTime()
}
const extractedQuery = query
.replace(BEFORE_REGEX, '')
.replace(AFTER_REGEX, '')
.trim()
return {
startDate,
endDate,
query: extractedQuery,
}
}
/**
* Utility function which runs on the output of `extractTimeFiltersFromQuery` and returns the values for
* display. Now only used for analytics.
*
* @param {QueryFilters} The extracted query parameters.
* @returns {string}
*/
export function queryFiltersDisplay({ startDate, endDate, query }) {
let val = ''
if (query && query.length) {
val += 'T'
}
if (startDate) {
val += ' SD'
}
if (endDate) {
val += ' ED'
}
return val
}