This is a porting of jq to javascript+WebAssembly using emscripten. The steps on how to do it can be found at here. The goal is to provide jq as a library for javascript applications.
npm install jqdash --save
The package exposes a function that asynchronously load the Module object of emscripten API. After loaded, a jq
method is added to it, and expected to be the only method user will ever need to use.
function jq(input: string, query: string, options: string[] | null): {stdout: string, stderr: string};
import jqdash from 'jqdash';
// jq --version
jqdash().then((module: any) => {
const { jq } = module;
result = jq('', '', ['--version']);
console.log(result.stdout);
});
// jq -n now
jqdash().then((module: any) => {
const { jq } = module;
result = jq('', 'now', ['-n']);
console.log(result.stdout);
});
// echo '{"name":"test"}' | jq .name
jqdash().then((module: any) => {
const { jq } = module;
result = jq('{"name":"test"}', '.name');
console.log(result.stdout);
});
// load by require
const jqdash = require('jqdash').default;
jqdash.then((module) => {
const jq = module.jq;
const jqoutput = jq('null', 'now', ['-M']).stdout;
......
}
1.6