-
Notifications
You must be signed in to change notification settings - Fork 33
/
toString.js
104 lines (84 loc) · 1.81 KB
/
toString.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
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
'use strict';
var implicit = '<implicit>';
module.exports = function toString(ast) {
if (!ast) {
return '';
}
var result = '';
if (ast.start != null) {
result += (ast.parenthesized ? '(' : '') + ast.start + ' ';
}
if (ast.field && ast.field !== implicit) {
result += ast.field + ':';
}
if (ast.left) {
if (ast.parenthesized && !ast.start) {
result += '(';
}
result += toString(ast.left);
if (ast.parenthesized && !ast.right) {
result += ')';
}
}
if (ast.operator) {
if (ast.left) {
result += ' ';
}
if (ast.operator !== implicit) {
result += ast.operator;
}
}
if (ast.right) {
if (ast.operator && ast.operator !== implicit) {
result += ' ';
}
result += toString(ast.right);
if (ast.parenthesized) {
result += ')';
}
}
if (ast.term || (ast.term === '' && ast.quoted)) {
if (ast.prefix) {
result += ast.prefix;
}
if (ast.quoted) {
result += '"';
result += ast.term;
result += '"';
} else if (ast.regex) {
result += '/';
result += ast.term;
result += '/';
} else {
result += ast.term;
}
if (ast.proximity != null) {
result += '~' + ast.proximity;
}
if (ast.boost != null) {
result += '^' + ast.boost;
}
}
if (ast.term_min) {
if (ast.inclusive === 'both' || ast.inclusive === 'left') {
result += '[';
} else {
result += '{';
}
result += ast.term_min;
result += ' TO ';
result += ast.term_max;
if (ast.inclusive === 'both' || ast.inclusive === 'right') {
result += ']';
} else {
result += '}';
}
}
if (ast.similarity) {
result += '~';
if (ast.similarity !== 0.5) {
result += ast.similarity;
}
}
return result;
};