This repository was archived by the owner on Jan 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-filter.js
More file actions
520 lines (513 loc) · 17.4 KB
/
json-filter.js
File metadata and controls
520 lines (513 loc) · 17.4 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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
const path = require('path')
let {SyntaxError, parse} = require('./pegjs/mongodb-simple-query-syntax.js').default
let {parse: NumberParse} = require('./pegjs/filter-number.js').default
let {parse: DateParse} = require('./pegjs/filter-date.js').default
let {parse: JsonParse} = require('./pegjs/json-filter.js').default
const {DateTime} = require('luxon')
let _ = require('lodash')
class ParseError extends Error {
constructor (start, end, expected, before, after) {
super(`Parse error:\nbefore:\n${before}\nafter:\n${after}\nexpected:${JSON.stringify(expected)}`)
this.start = start
this.end = end
this.expected = expected
this.before = before
this.after = after
}
}
class Parser {
constructor ({tree, options, treeAnalyser}) {
this.treeAnalyser = treeAnalyser
this.tree = this.treeAnalyser.tree
this.options = options || {}
this.fullTreePath = new Map()
this.getFullTreePath(this.tree, this.fullTreePath)
this.fullTreePathList = Array.from(this.fullTreePath.keys()).filter(_ => _)
this.topLevelPath = this.fullTreePathList.filter(_ => !(_.includes('@')||_.includes('.')||_.includes('>')))
}
getFullTreePath (tree, paths) {
paths.set(tree.path, tree)
if (tree.children) {
for (let child of tree.children) {
this.getFullTreePath(child, paths)
}
}
}
parse (content) {
let result
try {
result = JsonParse(content)
} catch (e) {
if (e instanceof SyntaxError) {
let start = e.location.start.offset
let end = e.location.end.offset
let expected = e.expected
let before = content.slice(0, start)
let after = content.slice(start)
let error = new ParseError(start, end, expected, before, after)
this.error = error
throw error
} else {
this.error = e
throw e
}
}
this.result = result
this.error = null
}
getCursorObjects (cursor, tree, trace) {
if (tree.start<=cursor && cursor<=tree.end) {
if (trace.length&&trace[trace.length-1].end===tree.start) return
trace.push(tree)
if (Array.isArray(tree.value)) {
for (let each of tree.value) {
this.getCursorObjects(cursor, each, trace)
}
} else {
if (tree.value.type || tree.value.valueType) {
this.getCursorObjects(cursor, tree.value, trace)
}
}
if (tree.key) {
this.getCursorObjects(cursor, tree.key, trace)
}
}
}
getContext (trace) {
// bottom up
if (!trace.length) return null
// type could be root, key and value
let type, valueType, value, completeKey, completeKeyFull
if (trace[0].valueType) {
type = 'value'
valueType = trace[0].valueType
value = trace[0].string
}
let keys =[]
for (let each of trace) {
if (each.type === 'key') {
type = each.type
value = each.value.string
}
if (each.type === 'pair') {
keys.push(each.key.value.value)
}
}
if (!type) type = 'root'
keys = keys.reverse()
if (type==='key') keys.pop()
if (trace[0].subtype === 'missingValue') {
type = 'value'
valueType = 'missingValue'
value = ''
}
let subkeys = []
for (let each of keys) {
let eachsubkeys = each.split('|')
subkeys.push(eachsubkeys[0])
if (eachsubkeys.length>1) {
subkeys = subkeys.concat(eachsubkeys.slice(1).map(_ => '@'+_))
}
}
/* operators
* logical operators: @and, @or, @not,
* length operators: @len, @wlen,
* array operators: @every, @any,
* other operators: @js,
*/
let keyPrefixs = subkeys.filter(_ => !['@and', '@or', '@not', '@any', '@every'].includes(_))
let keyPrefix = keyPrefixs.join('')
let completeData = []
if (type === 'root') { // 'insert' mode
if (keys.length===0) {
completeData = [
{
group: `commands`,
data: [
{data: '@js', description: 'arbitary js code'},
{data: '@and', description: 'and logical structure'},
{data: '@or', description: 'or logical structure'},
{data: '@not', description: 'not logical structure'},
]
},
{
group: `paths`,
data: this.topLevelPath,
always: true,
}
]
} else { // nested keys
let nestedPaths = this.fullTreePathList.filter(_ => _.startsWith(keyPrefix))
if (nestedPaths.length) {
nestedPaths = nestedPaths.map(_ => _.slice(keyPrefix.length))
completeData = [
{
group: `commands`,
data: [
{data: '@js', description: 'arbitary js code'},
{data: '@and', description: 'and logical structure'},
{data: '@or', description: 'or logical structure'},
{data: '@not', description: 'not logical structure'},
]
},
{
group: `${keyPrefix}`,
data: nestedPaths,
always: true,
}
]
} else {
completeData = [
{
group: `unknown prefix: ${keyPrefix}`,
always: true,
}
]
}
}
} else if (type === 'key' || type === 'value' && keys.length===0) { // 'replace' mode
completeKey = value.split('|')[0]
if (keys.length>0) { // value in root, should be the same as 'root inseart'
completeKeyFull = keyPrefix + completeKey
} else {
completeKeyFull = completeKey
}
let thisKeys = this.fullTreePathList.filter(_ => _.startsWith(completeKeyFull))
if (thisKeys.includes(completeKeyFull)) { // add suffix commands for it
let trueValueType = this.treeAnalyser.getTypeByPath(completeKeyFull)
let extraData = {
group: `unknown: ${completeKey}`,
data: [],
always: true,
}
if (trueValueType) {
let thisExtraData = []
if (value.includes('>')) {
extraData.group = `[${trueValueType.type}]: ${completeKeyFull} `
} else {
extraData.group = `${trueValueType.type}: ${completeKeyFull} `
}
let completeKeyPrefixs = [
`${completeKey}`,
]
if (completeKey.includes('>')) { // value is an array
completeKeyPrefixs = completeKeyPrefixs.concat([
`${completeKey}|any`,
`${completeKey}|every`,
])
}
if (trueValueType.type === 'array' || trueValueType.type === 'object') {
for (let completeKeyPrefix of completeKeyPrefixs) {
if (trueValueType.type === 'array') {
thisExtraData = thisExtraData.concat([
{data: `${completeKeyPrefix}>`, description: 'js filter for obj length'},
])
}
thisExtraData = thisExtraData.concat([
{data: `${completeKeyPrefix}|js: ""`, description: 'js filter for obj length', cursorOffset: -1},
{data: `${completeKeyPrefix}|len: ""`, description: 'number filter obj length', cursorOffset: -1},
{data: `${completeKeyPrefix}|exists: `, description: ''},
])
}
} else if (trueValueType.type === 'string') {
for (let completeKeyPrefix of completeKeyPrefixs) {
thisExtraData = thisExtraData.concat([
{data: `${completeKeyPrefix}: ""`, description: 'exact match', cursorOffset: -1},
{data: `${completeKeyPrefix}: //`, description: 'regexp', cursorOffset: -1},
{data: `${completeKeyPrefix}|js: ""`, description: 'js filter', cursorOffset: -1},
{data: `${completeKeyPrefix}|len: ""`, description: 'number filter for string length', cursorOffset: -1},
{data: `${completeKeyPrefix}|wlen: ""`, description: 'number filter for string word length', cursorOffset: -1},
{data: `${completeKeyPrefix}|exists: `, description: ''},
])
}
} else if (trueValueType.type === 'date') {
for (let completeKeyPrefix of completeKeyPrefixs) {
thisExtraData = thisExtraData.concat([
{data: `${completeKeyPrefix}: ""`, description: 'date filter', cursorOffset: -1},
{data: `${completeKeyPrefix}|js: ""`, description: 'js filter for date', cursorOffset: -1},
{data: `${completeKeyPrefix}|exists: `, description: ''},
])
}
} else if (trueValueType.type === 'number') {
for (let completeKeyPrefix of completeKeyPrefixs) {
thisExtraData = thisExtraData.concat([
{data: `${completeKeyPrefix}: ""`, description: 'number filter', cursorOffset: -1},
{data: `${completeKeyPrefix}|js: ""`, description: 'js filter for number', cursorOffset: -1},
{data: `${completeKeyPrefix}|exists: `, description: ''},
])
}
} else if (trueValueType.type === 'mixed') {
for (let completeKeyPrefix of completeKeyPrefixs) {
thisExtraData = thisExtraData.concat([
{data: `${completeKeyPrefix}@`, description: ''},
{data: `${completeKeyPrefix}|js: ""`, description: 'js filter for number', cursorOffset: -1},
{data: `${completeKeyPrefix}|exists: `, description: ''},
])
}
} else {
for (let completeKeyPrefix of completeKeyPrefixs) {
thisExtraData = thisExtraData.concat([
{data: `${completeKeyPrefix}|js: ""`, description: 'js filter for number', cursorOffset: -1},
{data: `${completeKeyPrefix}|exists: `, description: ''},
])
}
}
extraData.data = thisExtraData
}
completeData = [
{
group: `commands`,
data: [
{data: '@js', description: 'arbitary js code'},
]
},
extraData,
{
group: `Paths:`,
data: this.fullTreePathList,
always: true,
}
]
} else { // just show the keys
completeData = [
{
group: `commands`,
data: [
{data: '@js', description: 'arbitary js code'},
{data: '@and', description: 'and logical structure'},
{data: '@or', description: 'or logical structure'},
{data: '@not', description: 'not logical structure'},
]
},
{
group: `Paths:`,
data: this.fullTreePathList,
always: true,
}
]
}
} else { // type === 'value' && keys.length > 0
let key = keyPrefix
if (key.endsWith('@exists')) {
completeData = [
{
group: `true or false`,
always: true
},
]
} else if (key.endsWith('@len') || key.endsWith('@wlen')) {
let data = [
'examples:',
' >number',
' <number',
' >=number',
' <=number',
' ==number',
]
completeData = [
{
group: `number filter`,
noComplete: true,
itemAlways: true,
noSort: true,
data,
always: true,
},
]
} else if (key.endsWith('@js')) {
completeData = [
{
group: `js filter`,
always: true
},
]
} else {
completeKey = key.split('|')[0]
let trueValueType = this.treeAnalyser.getTypeByPath(completeKey)
if (trueValueType) {
let column = this.treeAnalyser.getValueByPath(this.treeAnalyser.data, completeKey)
let length
if (column) {
if (Array.isArray(column)) {
length = column.length
} else {
length = 1
}
} else {
length = 'undefined'
}
let comment, data
if ('number' === trueValueType.type) {
comment = `number filter`
data = [
'examples:',
' >number',
' <number',
' >=number',
' <=number',
' ==number',
]
} else if ('date' === trueValueType.type) {
comment = `date filter`
data = [
'examples:',
{data: ' >YYYY-MM-DDThh:mm:ss', description: ' later than timestamp'},
{data: ' >MM-DD', description: ' later than date'},
{data: ' >mm:ss', description: ' later than time (and in any day)'},
{data: ' >:weekday:D', description: ' later than weekday D'},
{data: ' >-?h', description: ' later than ? unit before, units: yMdhms'},
{data: ' in:YYYY', description: ' in year YYYY'},
{data: ' in:YYYY-MM-DD', description: ' in day YYYY-MM-DD'},
{data: ' in:weekday:D', description: ' in weekday D'},
{data: ' in:year:YYYY', description: ' in year YYYY'},
{data: ' in:month:MM', description: ' in month MM'},
{data: ' in:day:DD', description: ' in day DD'},
]
} else {
comment = trueValueType.type
}
completeData = [
{
group: `${comment} (${length})`,
noComplete: true,
itemAlways: true,
noSort: true,
data,
always: true
},
]
} else {
completeData = [
{
group: `unknown type...`,
always: true
},
]
}
}
}
console.log({completeKey})
return {type, string:value, completeData, keys, keyPrefix}
}
analysis (cursor) {
let options = {maxDrop: 15}
if (!this.result) throw Error('should do parse first!')
if (cursor === null) {
// from getContext, root section
let completeData
let options = {maxDrop: 0}
completeData = [
{
group: `commands`,
data: [
{data: '@js', description: 'arbitary js code'},
{data: '@and', description: 'and logical structure'},
{data: '@or', description: 'or logical structure'},
{data: '@not', description: 'not logical structure'},
]
},
{
group: `keys`,
data: this.topLevelPath,
always: true,
}
]
result = {
range: null,
string: '',
completeData,
options,
}
return result
}
this.trace = []
this.getCursorObjects(cursor, this.result, this.trace)
this.trace = this.trace.reverse()
let {type, string, completeData, keys, keyPrefix} = this.getContext(this.trace)
let trace = this.trace[0]
let result, completeType, start, end, range
if (!trace) {
range = null
completeData = [ ]
string = ""
} else if (!trace.type) { // inside a value
completeType = 'replace'
string = trace.string
range = {start: trace.start, end: trace.end, color: 'rgba(0,255,0,0.2)'}
/*
completeData = [
{
group: `replacing: ${trace.valueType}`,
always: true,
}
]
*/
} else { // in some ws
completeType = 'insert'
options.maxDrop = 0
range = null
string = ""
if (trace.type === 'pair' && trace.subtype === 'complete' && cursor > trace.key.end && cursor < trace.value.start) {
// no complete not between key: value
completeData = [ ]
} else if (trace.badPositions && trace.badPositions.includes(cursor)) {
// no complete not between "&&" nad "||"
completeData = [ ]
} else if (['nested', 'array', 'object'].includes(trace.type) && (cursor===trace.start || cursor===trace.end)) {
completeData = [ ]
// only show the range, no complete
range = {start: trace.start, end: trace.end, color: 'rgba(0,255,0,0.2)'}
} else {
range = {start: cursor, end: cursor}
}
}
console.log({cursor, keys, keyPrefix, type, string, completeData, options, trace})
result = {
range,
string,
completeData,
options,
}
return result
}
static _simpleResult (tree) {
if (!tree.type && !tree.valueType) return tree
let result = {}
//if (tree.string) result.string = tree.string
if (tree.type) result.type = tree.type
if (tree.key) result.key = this._simpleResult(tree.key)
if (tree.valueType) result.valueType = tree.valueType
if (tree.subtype) result.subtype = tree.subtype
if (tree.value) {
if (Array.isArray(tree.value)) {
result.value = tree.value.map(_ => this._simpleResult(_))
} else {
result.value = this._simpleResult(tree.value)
}
}
return result
}
static simpleResult (tree) {
return this._simpleResult(tree)
}
static _getFilter (tree) { // return the filter function
return tree
}
static getFilter (content) { // return the filter function
let filter
try {
filter = JsonParse(content)
} catch (error) {
return {filter: _=>true, error}
}
filter = this.simpleResult(filter)
filter = this._getFilter(filter)
return {filter}
}
}
//module.exports =
export default {
parse: JsonParse,
SyntaxError,
Parser,
}