Giter VIP home page Giter VIP logo

query-parser.jsx's Introduction

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

License

MIT

Complete license is written in LICENSE.md.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.