-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathquery.js
More file actions
34 lines (28 loc) · 718 Bytes
/
Copy pathquery.js
File metadata and controls
34 lines (28 loc) · 718 Bytes
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
module.exports = function (query) {
// objects
query = query.replace(/(^| )([A-Z])/, '$1.$2')
// flags
query = query.replace(/ -/g, '.-')
var parts = query.split(',')
var results = parts.map(handleDepth)
return results.join(', ')
}
function handleDepth (query) {
var result = []
var parts = query.trim().split(' ')
for (var i = 0; i < parts.length; i++) {
var part = parts[i]
var deepQuery = getDeepQuery(part)
part = deepQuery || part
if (!deepQuery && i > 0) {
result.push('>')
}
result.push(part)
}
return result.join(' ')
}
function getDeepQuery (query) {
if (query.slice(0, 1) === '(' && query.slice(-1) === ')') {
return query.slice(1, -1)
}
}