deno.land / x / fuse@v6.4.1 / src / core / queryParser.js

queryParser.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
import { isArray, isObject, isString } from '../helpers/types'import { createSearcher } from './register'import * as ErrorMsg from './errorMessages'import { createKeyId } from '../tools/KeyStore'
export const LogicalOperator = { AND: '$and', OR: '$or'}
const KeyType = { PATH: '$path', PATTERN: '$val'}
const isExpression = (query) => !!(query[LogicalOperator.AND] || query[LogicalOperator.OR])
const isPath = (query) => !!query[KeyType.PATH]
const isLeaf = (query) => !isArray(query) && isObject(query) && !isExpression(query)
const convertToExplicit = (query) => ({ [LogicalOperator.AND]: Object.keys(query).map((key) => ({ [key]: query[key] }))})
// When `auto` is `true`, the parse function will infer and initialize and add// the appropriate `Searcher` instanceexport function parse(query, options, { auto = true } = {}) { const next = (query) => { let keys = Object.keys(query)
const isQueryPath = isPath(query)
if (!isQueryPath && keys.length > 1 && !isExpression(query)) { return next(convertToExplicit(query)) }
if (isLeaf(query)) { const key = isQueryPath ? query[KeyType.PATH] : keys[0]
const pattern = isQueryPath ? query[KeyType.PATTERN] : query[key]
if (!isString(pattern)) { throw new Error(ErrorMsg.LOGICAL_SEARCH_INVALID_QUERY_FOR_KEY(key)) }
const obj = { keyId: createKeyId(key), pattern }
if (auto) { obj.searcher = createSearcher(pattern, options) }
return obj }
let node = { children: [], operator: keys[0] }
keys.forEach((key) => { const value = query[key]
if (isArray(value)) { value.forEach((item) => { node.children.push(next(item)) }) } })
return node }
if (!isExpression(query)) { query = convertToExplicit(query) }
return next(query)}
fuse

Version Info

Tagged at
3 years ago