Skip to content

Commit bc2d311

Browse files
committed
shortcut id selection
1 parent ba0c972 commit bc2d311

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

features/selection-by-id.feature

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ Feature: select values by type using query syntax
1717
Scenario: Query for nested node by id
1818
Given I query for "[@id=test:2]"
1919
When I query the result for "test:name @value"
20-
Then the result should be "Thing 2"
20+
Then the result should be "Thing 2"
21+
22+
Scenario: Shortcut query for node by id
23+
Given I query for "#test:2"
24+
When I query the result for "test:name @value"
25+
Then the result should be "Thing 2"

src/ld-query.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,22 +461,24 @@
461461
function extractStep(path, steps) {
462462

463463
// try and extract a 'where' [@attribute=value] part from the start of the string
464-
var wherePart = /^(\s*\*?)\[(.+?)=(.+?)\](.*)/.exec(path);
464+
var wherePart = /^(\s*\*?)(?:\[(.+?)=(.+?)\]|#(\S*))(.*)/.exec(path);
465465
if (wherePart) {
466466

467467
if (wherePart[1]) {
468468

469469
steps.push({ id: -1, path: undefined, directChild: false, tests: [] });
470470

471471
}
472-
var step = { key: wherePart[2].trim(), value: wherePart[3].trim() };
472+
var step = wherePart[3]
473+
? { key: wherePart[2].trim(), value: wherePart[3].trim() }
474+
: { key: "@id", value: wherePart[4].trim() };
473475
if (!nonExpandableValuePropNamePattern.test(step.key)) {
474476

475477
step.value = expand(step.value);
476478

477479
}
478480
steps.push(step);
479-
return (wherePart[4] || "");
481+
return (wherePart[5] || "");
480482

481483
}
482484
// try and extract a > part from the start of the string

0 commit comments

Comments
 (0)