Skip to content

Commit

Permalink
feat: add inline subroutines
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Nov 21, 2019
1 parent 4ceb31c commit 115e402
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Have you got suggestions for improvement? [I am all ears](https://github.com/gaj
* [Quantifier expression](#quantifier-expression)
* [`test` subroutine](#test-subroutine)
* [User-defined subroutines](#user-defined-subroutines)
* [Inline subroutines](#inline-subroutines)
* [Built-in subroutine aliases](#built-in-subroutine-aliases)
* [Expression reference](#expression-reference)
* [The pipe operator (`|`)](#the-pipe-operator-)
Expand Down Expand Up @@ -457,6 +458,26 @@ For more examples of defining subroutines, refer to:
* [Validate the results using a user-defined test function](#validate-the-results-using-a-user-defined-test-function).
* [Source code](./src/subroutines) of the the built-in subroutines.

## Inline subroutines

Custom subroutines can be inlined into [pianola](https://github.com/gajus/pianola) instructions, e.g.

```js
x(
[
'foo',
(subject) => {
// `subject` is the return value of `foo` subroutine.

return 'bar';
},
'baz',
],
'qux'
);

```

## Built-in subroutine aliases

Surgeon exports an alias preset is used to reduce verbosity of the queries.
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
"dependencies": {
"cheerio": "^1.0.0-rc.3",
"es6-error": "^4.1.1",
"pianola": "^2.1.1",
"pianola": "^2.2.0",
"regex-parser": "^2.2.10",
"roarr": "^2.14.5"
"roarr": "^2.14.6"
},
"description": "Declarative DOM extraction expression evaluator.",
"devDependencies": {
Expand All @@ -36,15 +36,15 @@
"ava": "^2.4.0",
"babel-plugin-istanbul": "^5.2.0",
"babel-plugin-transform-export-default-name": "^2.0.4",
"coveralls": "^3.0.7",
"coveralls": "^3.0.8",
"eslint": "^6.6.0",
"eslint-config-canonical": "^18.1.0",
"flow-bin": "^0.112.0",
"flow-copy-source": "^2.0.8",
"husky": "^3.0.9",
"flow-copy-source": "^2.0.9",
"husky": "^3.1.0",
"lodash": "^4.17.15",
"nyc": "^14.1.1",
"semantic-release": "^15.13.30",
"semantic-release": "^15.13.31",
"sinon": "^7.5.0",
"sprintf-js": "^1.1.2"
},
Expand Down
21 changes: 21 additions & 0 deletions test/surgeon/queries/single-match.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,24 @@ test.skip('extracts a single value (expression string `.foo:has(+.bar)`)', (t):

t.true(x(query, subject) === 'foo');
});

test('extracts a single value (using inline subroutine)', (t): void => {
const x = surgeon();

const subject = `
<div class="foo">FOO</div>
<div class="bar">BAR</div>
`;

const query: DenormalizedQueryType = [
'select .foo',
(input) => {
t.is(input.text(), 'FOO');

return input.next();
},
'read property textContent',
];

t.is(x(query, subject), 'BAR');
});

0 comments on commit 115e402

Please sign in to comment.