Produces Javascript from UniQL ASTs.
  • JavaScript 100%
Find a file
2019-04-05 17:57:09 -07:00
src allow options when creating a compiler 2019-04-05 17:57:09 -07:00
tests allow options when creating a compiler 2019-04-05 17:57:09 -07:00
.eslintrc.json update to ES6+, add tests 2019-04-04 23:44:44 -07:00
.gitignore Initial commit. 2015-01-07 18:52:51 -08:00
LICENSE Initial commit. 2015-01-07 18:52:51 -08:00
package-lock.json allow options when creating a compiler 2019-04-05 17:57:09 -07:00
package.json allow options when creating a compiler 2019-04-05 17:57:09 -07:00
README.md allow options when creating a compiler 2019-04-05 17:57:09 -07:00

UniQL-JS

This generates Javascript based on UniQL ASTs.

Example

const parse = require( 'uniql' );
const uniql_to_js_compiler = require( 'uniql-js' );

const compiler = uniql_to_js_compiler.create();

const ast = parse( `
    ( height <= 20 or
      ( favorites.color == "green" and height != 25 ) )
      and firstname ~= "o.+"` );
const javascript = compiler.compile( ast );
console.log( javascript );

Results:

( height <= 20 ||
  ( favorites.color == "green" && height != 25 ) )
  && firstname.match( new RegExp( "o.+" ) )

API

.create( [ options ] )

Creates a JS compiler. You can override generators for various symbols or the post-processor for the compiled code, eg:

const compiler = uniql_to_js_compiler.create( {
    generators: {
        // eg: rename a certain variable
        SYMBOL: node => {
            const symbol_text = node.arguments[ 0 ];
            if ( symbol_text === 'foo' ) {
                return 'bar';
            }

            return symbol_text;
        }
    },

    // eg: return a markdown block instead of just the compiled code
    post_process: compiled => {
        return `\`\`\`javascript\n${ compiled }\n\`\`\``;
    }
} );

License

MIT