Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/asterjs/aster-equery
Replace nodes with pattern-matching selectors in aster.
https://github.com/asterjs/aster-equery
Last synced: about 1 month ago
JSON representation
Replace nodes with pattern-matching selectors in aster.
- Host: GitHub
- URL: https://github.com/asterjs/aster-equery
- Owner: asterjs
- License: mit
- Created: 2014-06-03T20:19:07.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-11-23T21:49:58.000Z (about 8 years ago)
- Last Synced: 2024-08-10T19:59:47.901Z (4 months ago)
- Language: JavaScript
- Homepage: https://npmjs.org/package/aster-equery
- Size: 191 KB
- Stars: 7
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-starred - asterjs/aster-equery - Replace nodes with pattern-matching selectors in aster. (others)
README
# aster-equery
[![NPM version][npm-image]][npm-url]
[![Build Status][travis-image]][travis-url]> Replace nodes with pattern-matching selectors in aster.
Allows to use pattern-matching (JavaScript code examples with wildcards and some other special syntax) for finding nodes and replacing them with results of corresponding handlers.
Uses [grasp-equery](https://npmjs.org/package/grasp-equery) behind the scenes, so check out [official documentation](http://graspjs.com/docs/equery/) for syntax details.
## Usage
First, install `aster-equery` as a development dependency:
```shell
npm install --save-dev aster-equery
```Then, add it to your build script:
```javascript
var aster = require('aster');
var equery = require('aster-equery');aster.src('src/**/*.js')
.map(equery({
'if ($cond) return $expr1; else return $expr2;': function (node, named) {
return {
type: 'ReturnStatement',
argument: {
type: 'ConditionalExpression',
test: named.cond,
consequent: named.expr1,
alternate: named.expr2
}
};
}
// , ...
}))
.map(aster.dest('dist'))
.subscribe(aster.runner);
```can be also written as:
```javascript
var aster = require('aster');
var equery = require('aster-equery');aster.src('src/**/*.js')
.map(equery({
'if ($cond) return $expr1; else return $expr2;': 'return <%= cond %> ? <%= expr1 %> : <%= expr2 %>'
// , ...
}))
.map(aster.dest('dist'))
.subscribe(aster.runner);
```## API
### equery(mappings)
#### mappings
Type: `{pattern: handler}`Replacement mappings.
##### pattern
Type: `String`[JavaScript example pattern](http://graspjs.com/docs/equery/).
##### handler (option 1: callback)
Type: `Function(node, named)`Callback to be called on each found match. It will get two arguments - matched node object and hashmap of named subpatterns.
##### handler (option 2: template)
Type: `String`[estemplate](https://github.com/RReverser/estemplate) string to be used for generating AST.
## License
[MIT License](http://en.wikipedia.org/wiki/MIT_License)
[npm-url]: https://npmjs.org/package/aster-equery
[npm-image]: https://badge.fury.io/js/aster-equery.png[travis-url]: http://travis-ci.org/asterjs/aster-equery
[travis-image]: https://secure.travis-ci.org/asterjs/aster-equery.png?branch=master