2.0.0 • Published 5 years ago

uniql-js v2.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

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

2.0.0

5 years ago

1.0.0

5 years ago

0.1.0

9 years ago

0.0.1

9 years ago