forked from CS599-MEDLINE/pubminer-web-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryHelper.js
More file actions
54 lines (44 loc) · 1.32 KB
/
QueryHelper.js
File metadata and controls
54 lines (44 loc) · 1.32 KB
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
"use strict";
class QueryHelper {
static isEmptyTerm(term) {
return (
typeof term === "undefined" ||
typeof term !== "string" ||
term.trim().length === 0);
}
static getDateFilter(num) {
switch (num) {
case "1":
case "2":
case "3":
return `"last ${num} years"[PDat]`;
default:
return "";
}
}
/**
* Joins all terms using a conjunction. Empty and undefined terms
* are removed.
* @param terms the individual terms to be combined.
* @return atomized and joined terms such as (term1) AND (term2)
* when passed [term1, term2]
*/
static combineSearchTerms(terms) {
if (!Array.isArray(terms)) {
return terms;
}
const validTerms = terms.filter(x => !QueryHelper.isEmptyTerm(x));
//
if (validTerms.length < 2) {
return validTerms.reduce((acc, x) => x, '');
}
return validTerms
.slice(1)
.reduce((acc, term) => `${acc} AND (${term})`, `(${validTerms[0]})`);
}
static mergeQueryOptions(optionsArray) {
return (optionsArray || [])
.reduce((acc, opt) => Object.assign(acc, opt), {});
}
}
module.exports = QueryHelper;