-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
253 lines (230 loc) · 6.66 KB
/
index.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* @typedef { import("./index").TsqueryOptions } TsqueryOptions
*/
const PRECEDENCES = {
'|': 0,
'&': 1,
'<': 2,
};
class Node {
constructor({ type, input, negated, left, right, value, prefix, quoted }) {
this.type = type; // '&'|'|'|'<->'|'<1>'|'<2>'.. or undefined if a word node (leaf node)
this.input = input; // remaining string to parse
this.negated = negated; // boolean
this.left = left; // Node
this.right = right; // Node
this.value = value; // word node string value
this.prefix = prefix; // word node prefix when using a :* operator
this.quoted = quoted; // whether the node is wrapped in quotes (aka websearch_to_tsquery)
}
toString() {
if (!this.type) {
if (!this.value) return ''; // avoid just ! (negated empty word, shouldn't happen with proper non-empty word regex tho)
const prefixed = `${this.value}${this.prefix ? ':*' : ''}`;
return this.negated && this.quoted
? `!(${prefixed})`
: `${this.negated ? '!' : ''}${prefixed}`;
}
let left = this.left;
let right = this.right;
if (left.type && PRECEDENCES[this.type[0]] > PRECEDENCES[left.type[0]] && !left.negated) {
// wrap left in parens
left = `(${left})`;
}
if (right.type && PRECEDENCES[this.type[0]] > PRECEDENCES[right.type[0]] && !right.negated) {
// wrap right in parens
right = `(${right})`;
}
const content = `${left}${this.type}${right}`;
return this.negated ? `!(${content})` : content;
}
}
class Tsquery {
/**
*
* @param {TsqueryOptions} opts
*/
constructor({
or = /^\s*(?:[|,]|or)/i,
and = /^(?!\s*(?:[|,]|or))(?:[\s&+:|,!-]|and)*/i, // /^\s*(?:[\s&+:|,!-]|and)*/i,
followedBy = /^\s*>/, // /^\s*<(?:(?:(\d+)|-)?>)?/,
word = /^[\s*&+<:,|]*(?<negated>[\s!-]*)[\s*&+<:,|]*(?:(?<quote>["'])(?<phrase>.*?)\k<quote>|(?<word>[^\s,|&+<>:*()[\]!]+))/,
quotedWordSep = /(?:[\s<()|&!]|:\*)+/, // those are mostly tsquery operator, not removing them would cause errors
parStart = /^\s*[!-]*[([]/,
parEnd = /^[)\]]/,
negated = /[!-]$/,
prefix = /^(\*|:\*)*/,
tailOp = '&',
singleQuoteReplacement = '',
} = {}) {
this.or = or;
this.and = and;
this.followedBy = followedBy;
this.word = word;
this.quotedWordSep = quotedWordSep;
this.parStart = parStart;
this.parEnd = parEnd;
this.negated = negated;
this.prefix = prefix;
this.tailOp = tailOp;
this.singleQuoteReplacement = singleQuoteReplacement;
}
parseAndStringify(str) {
return `${this.parse(str) || ''}`;
}
/**
* Parse string as a Node tree, invoke .toString() to get back a string value for thta tree
* @param {string} str
* @returns {Node|undefined}
*/
parse(str) {
let node = this.parseOr(str);
let tail = node && node.input;
while (tail && this.tailOp) {
tail = tail.slice(1);
const right = this.parseOr(tail);
if (right) {
node = new Node({
type: this.tailOp,
input: right.input,
negated: false,
left: node,
right,
value: undefined,
prefix: undefined,
quoted: undefined,
});
tail = node.input;
}
}
return node;
}
/**
*
* @returns {Node|undefined}
*/
parseOr(str) {
let node = this.parseAnd(str);
while (node && node.input && this.or) {
const m = node.input.match(this.or);
if (!m) return node;
const s = node.input.slice(m[0].length);
const right = this.parseAnd(s);
if (!right) return node;
right.negated = right.negated || this.negated.test(m[0]);
node = new Node({
type: '|',
input: right.input,
negated: false,
left: node,
right,
value: undefined,
prefix: undefined,
quoted: undefined,
});
}
return node;
}
/**
*
* @returns {Node|undefined}
*/
parseAnd(str) {
let node = this.parseFollowedBy(str);
while (node && node.input && this.and) {
const m = node.input.match(this.and);
if (!m) return node;
const s = node.input.slice(m[0].length);
const right = this.parseFollowedBy(s);
if (!right) return node;
right.negated = right.negated || this.negated.test(m[0]);
node = new Node({
type: '&',
input: right.input,
negated: false,
left: node,
right,
value: undefined,
prefix: undefined,
quoted: undefined,
});
}
return node;
}
/**
*
* @returns {Node|undefined}
*/
parseFollowedBy(str) {
let node = this.parseWord(str);
while (node && node.input && this.followedBy) {
const m = node.input.match(this.followedBy);
if (!m) return node;
const s = node.input.slice(m[0].length);
const right = this.parseWord(s);
if (!right) return node;
right.negated = right.negated || this.negated.test(m[0]);
node = new Node({
type: m[1] ? `<${m[1]}>` : '<->',
input: right.input,
negated: false,
left: node,
right,
value: undefined,
prefix: undefined,
quoted: undefined,
});
}
return node;
}
/**
*
* @returns {Node|undefined}
*/
parseWord(str) {
const s = str.trimStart();
const par = s.match(this.parStart);
if (par) {
const s2 = s.slice(par[0].length);
const node = this.parseOr(s2);
if (node) {
node.negated = node.negated || par[0].length > 1;
node.input = node.input.trimStart().replace(this.parEnd, '');
}
return node;
}
const m = s.match(this.word);
if (m === null || !m.groups) {
return;
}
const next = s.slice(m[0].length);
const prefix = this.prefix ? next.match(this.prefix)[0] : '';
const input = next.slice(prefix.length);
const value = m.groups.word
? m.groups.word.replace(/'/g, this.singleQuoteReplacement) // replace single quotes, else you'd get a syntax error in pg's ts_query
: `"${m.groups.phrase.split(this.quotedWordSep).join('<->')}"`; // it looks nasty, but to_tsquery will handle this well, see tests, in the end it behaves like websearch_to_tsquery
const negated = !!m.groups.negated;
return new Node({
type: undefined,
value,
negated,
left: undefined,
right: undefined,
input,
prefix,
quoted: !!m.groups.quote,
});
}
}
/**
* Initializes tsquery parser
* @param {TsqueryOptions} opts
* @returns {(string) => string}
*/
function tsquery(opts) {
const parser = new Tsquery(opts);
return str => `${parser.parse(str) || ''}`;
}
tsquery.Tsquery = Tsquery;
tsquery.Node = Node;
module.exports = tsquery;