Skip to content

Commit 522b267

Browse files
Adding pretty printer for path queries
1 parent 750486a commit 522b267

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

lib/query/woqlCore.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,8 @@ function copyJSON(orig, rollup) {
840840
}
841841
}
842842
nuj[k] = nupart;
843+
} else if (part === null) {
844+
// do nothing
843845
} else if (typeof part === 'object') {
844846
const q = copyJSON(part, rollup);
845847
if (!q || !UTILS.empty(q)) nuj[k] = q;

lib/query/woqlPrinter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,9 @@ WOQLPrinter.prototype.decompilePathPattern = function (pstruct) {
357357
// eslint-disable-next-line default-case
358358
switch (t) {
359359
case 'InversePathPredicate':
360-
return `<${pstruct.predicate}`;
360+
return pstruct.predicate ? `<${pstruct.predicate}` : '<.'
361361
case 'PathPredicate':
362-
return pstruct.predicate;
362+
return pstruct.predicate ? `${pstruct.predicate}` : '.'
363363
case 'PathPlus':
364364
var next = pstruct.plus;
365365
if (Array.isArray(next)) next = next[0];

test/woqlPathquery.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,37 @@ describe('woql path query and path query prettyprint', () => {
2727
expect(query.prettyPrint()).to.eql('WOQL.path("v:X", "hop", "v:Y", "v:Path")');
2828
});
2929

30+
it('simple any path query', () => {
31+
const query = WOQL.path('v:X', '.*,name', 'v:Y');
32+
const json = {
33+
'@type': 'Path',
34+
subject: {
35+
'@type': 'NodeValue',
36+
variable: 'X',
37+
},
38+
pattern: {
39+
'@type': "PathSequence",
40+
'sequence': [
41+
{
42+
'@type': "PathStar",
43+
'star': {
44+
'@type': "PathPredicate"
45+
}
46+
},
47+
{
48+
'@type': "PathPredicate"
49+
}
50+
]
51+
},
52+
object: {
53+
'@type': 'Value',
54+
variable: 'Y',
55+
}
56+
};
57+
expect(query.json()).to.eql(json);
58+
expect(query.prettyPrint()).to.eql('WOQL.path("v:X", "(.*),.", "v:Y")');
59+
});
60+
3061
it('test plus directed path query', () => {
3162
const query = WOQL.path('v:X', '<hop+', 'v:Y', 'v:Path');
3263
const json = {

0 commit comments

Comments
 (0)