query-parser.jsx

Google-ish query string parser for JSX/JS/CommonJS/AMD

npm install query-parser.jsx
13 downloads in the last week
22 downloads in the last month

query-parser.jsx

Build Status

Synopsis

Google-ish query string parser for JSX/JS/CommonJS/AMD

Motivation

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"

Code Example

This module provides two classes. These classes

  • QueryStringParser: Good for parse query string on HTML forms.
  • QueryListParser: Good for processing command line tools.

Use from JSX (Query

// 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" : "");
        }
    }
}

Use from node.js

var QueryStringParser = require('query-parser.common.js').QueryStringParser;
var QueryListParser = require('query-parser.common.js').QueryListParser;

Use from require.js

// use query-parser.amd.js
define(['query-parser.amd.jsx'], function (queryparser) {
    var QueryStringParser = queryparser.QueryStringParser;
    var QueryListParser = queryparser.QueryListParser;

});

Use via standard JSX function

<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>

Use via global variables

<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>

Installation

$ 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.

API Reference

This module provides the following two classes:

class QueryListParser()

It is good for prcessing command line parameters. query() method receives array of string.

class QueryStringParser()

It is good for prcessing search query in text input. query() method receives string.

QueryParser#parse(query) : void

Parse query.

QueryParser#queries : Query[]

Parse result.

class Query

It has query result. It has the following members:

  • word : string
  • or : boolean
  • not : boolean
  • raw : boolean

Development

JSX

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.

Setup

To create development environment, call following command:

$ npm install

Repository

Run Test

$ grunt test

Build

$ grunt build

Generate API reference

$ grunt doc

Author

  • shibukawa / yoshiki@shibu.jp

License

MIT

Complete license is written in LICENSE.md.

npm loves you