Skip to content

Commit

Permalink
Add ^ prefix for qs() instead of qsa(), fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
danburzo committed Jul 20, 2020
1 parent f03a0e2 commit 1fb91a0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,23 @@ qsx(
],
];
```

### Pick first query result with `^`

For more complex queries where there resulting JSON contains several nested arrays, but for which you want to select a single element, you can prefix a selector with `^` to select just the first matching element — like `querySelector()` rather than `querySelectorAll()`.

```js
qsx(document, `li { ^a, @title }`);

// =>
[
{
title: "item 1",
".scoped": ['<a href="/first-link">First link</a>'],
},
{
title: "item 2",
".scoped": ['<a href="/second-link">Second link</a>'],
},
];
```
18 changes: 13 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const T = {
FUNC_START: "(",
FUNC_END: ")",
ATTR: "@",
ONCE: "^",
};

const RE = new RegExp(
Expand Down Expand Up @@ -46,7 +47,7 @@ function qsa(el, $node, tree) {
let attrs = $node.attrs;
let $children = tree.childrenToArray($node);

return elements.map((element) => {
let res = elements.map((element) => {
let scoped_els;
if ($children.length) {
scoped_els = $children.map(($child) => qsa(element, $child, tree));
Expand Down Expand Up @@ -75,14 +76,18 @@ function qsa(el, $node, tree) {
}
return res;
});

return $node.once ? res[0] : res;
}

module.exports = function qsx(el, selector) {
const tree = new SymbolTree();
const $root = {
const node = () => ({
ctx: "",
attrs: [],
};
once: false,
});
const $root = node();
let $curr = $root;

const tokens = selector
Expand All @@ -108,7 +113,7 @@ module.exports = function qsx(el, selector) {
break;
case T.GROUP_START:
ctx_depth++;
$curr = tree.appendChild($curr, { ctx: "", attrs: [] });
$curr = tree.appendChild($curr, node());
break;
case T.GROUP_END:
if (ctx_depth <= 0) {
Expand All @@ -123,7 +128,7 @@ module.exports = function qsx(el, selector) {
break;
case T.SEP:
if (!fn_depth && ctx_depth) {
let $sibling = tree.insertAfter($curr, { ctx: "", attrs: [] });
let $sibling = tree.insertAfter($curr, node());
if (!$curr.ctx) {
tree.remove($curr);
}
Expand All @@ -139,6 +144,9 @@ module.exports = function qsx(el, selector) {
}
tree.parent($curr).attrs.push(attr.trim());
break;
case T.ONCE:
$curr.once = true;
break;
default:
$curr.ctx += token;
}
Expand Down
11 changes: 11 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ tape("README examples", (t) => {
},
]);

t.deepEqual(qsx(links, `li { ^a, @title }`), [
{
title: "item 1",
".scoped": ['<a href="/first-link">First link</a>'],
},
{
title: "item 2",
".scoped": ['<a href="/second-link">Second link</a>'],
},
]);

let terms = document(`
<dl>
<dt><a href='#ref1'>First term</a></dt>
Expand Down

0 comments on commit 1fb91a0

Please sign in to comment.