Google-ish query string parser for JSX/JS/CommonJS/AMD
This is a part of Oktavia. This module provides Google-ish query. It supports the following syntax:
AND
:hello world
OR
:hello OR world
NOT
:-hello
RAW
:"hello world"
This module provides two classes. These classes
QueryStringParser
: Good for parse query string on HTML forms.QueryListParser
: Good for processing command line tools.
// QueryStringParser for console
import "query-string-parser.jsx";
import "js/web.jsx";
class _Main {
static function main(argv : string[]) : void
{
dom.id('search_button').addEventListener('click', (event : Event) -> {
var parser = new QueryStringParser();
var input = dom.id('search_box') as HTMLInputElement;
var queries = parser.parse(input.value);
for (var i = 0; i < queries.length; i++)
{
var query = queries[i];
var str = [query.word, query.or ? "OR" : "", query.not ? "NOT" : "", query.raw ? "ROW" : "", '<br/>'].join('')
dom.document.write(str);
}
event.preventDefault();
}, false);
}
}
// QueryListParser for console
import "query-parser.jsx";
import "console.jsx";
class _Main {
static function main(argv : string[]) : void
{
var parser = new QueryListParser();
var queries = parser.parse(argv);
for (var i = 0; i < queries.length; i++)
{
var query = queries[i];
console.log(query.word, query.or ? "OR" : "", query.not ? "NOT" : "", query.raw ? "ROW" : "");
}
}
}
var QueryStringParser = require('query-parser.common.js').QueryStringParser;
var QueryListParser = require('query-parser.common.js').QueryListParser;
// use query-parser.amd.js
define(['query-parser.amd.jsx'], function (queryparser) {
var QueryStringParser = queryparser.QueryStringParser;
var QueryListParser = queryparser.QueryListParser;
});
<script src="query-parser.js" type="text/javascript"></script>
<script type="text/javascript">
window.onload = function () {
var QueryStringParser = JSX.require("src/query-parser.js").QueryStringParser;
var QueryListParser = JSX.require("src/query-parser.js").QueryListParser;
});
</script>
<script src="query-parser.global.js" type="text/javascript"></script>
<script type="text/javascript">
window.onload = function () {
var parser1 = new QueryStringParser();
var parser2 = new QueryListParser();
}
</script>
$ npm install query-parser.jsx
If you want to use this library from other JSX project, install like the following:
$ npm install query-parser.jsx --save-dev
or add like these lines to your parent project's package.json
:
devDependencies: {
"query-parser.jsx": "~0.3.0"
},
peerDepenencies: {
"query-parser.jsx": "~0.3.0"
}
And add node_modules/query-parser.jsx/src
as a search path.
You should add to peerDepenencies
if your product is library.
This module provides the following two classes:
It is good for prcessing command line parameters. query()
method receives array of string.
It is good for prcessing search query in text input. query()
method receives string.
Parse query.
Parse result.
It has query result. It has the following members:
word : string
or : boolean
not : boolean
raw : boolean
Don't be afraid JSX! If you have an experience of JavaScript, you can learn JSX quickly.
- Static type system and unified class syntax.
- All variables and methods belong to class.
- JSX includes optimizer. You don't have to write tricky unreadalbe code for speed.
- You can use almost all JavaScript API as you know. Some functions become static class functions. See reference.
To create development environment, call following command:
$ npm install
- Repository: git://github.com/shibukawa/query-parser.jsx.git
- Issues: https://github.com/shibukawa/query-parser.jsx/issues
$ grunt test
$ grunt build
$ grunt doc
- shibukawa / [email protected]
MIT
Complete license is written in LICENSE.md
.