Skip to content

Commit

Permalink
Add support to unquoted output #8
Browse files Browse the repository at this point in the history
  • Loading branch information
xonixx committed Jul 16, 2020
1 parent ba2a1e1 commit d34fbfd
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions jsqry-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,21 @@ function err(msg) {
const isTtyIn = os.isatty(0);
const isTtyOut = os.isatty(1);

function doWork(jsonStr, queryStr, queryArgs, useFirst, compact) {
function unquoteResult(res) {
function isPrimitive(val) {
const type = typeof val;
return type === "string" || type === "boolean" || type === "number";
}
if (isPrimitive(res)) {
return res;
} else if (Array.isArray(res)) {
return res.map((e) => (isPrimitive(e) ? e : JSON.stringify(e))).join("\n");
} else {
return JSON.stringify(res);
}
}

function doWork(jsonStr, queryStr, queryArgs, useFirst, compact, unquote) {
let json;
try {
json = JSON.parse(jsonStr);
Expand All @@ -85,7 +99,9 @@ function doWork(jsonStr, queryStr, queryArgs, useFirst, compact) {
return "error: " + e;
}
print(
compact
unquote
? unquoteResult(res)
: compact
? JSON.stringify(res)
: isTtyOut
? colorJson(res, 2)
Expand Down Expand Up @@ -151,6 +167,7 @@ Usage: echo $JSON | jsqry 'query'
-h,--help print help and exit
-v,--version print version and exit
-c,--compact compact output (no pretty-print)
-u,--unquote unquote output string(s)
-as ARG,
--arg-str ARG supply string query argument
-a ARG,
Expand All @@ -162,7 +179,8 @@ Usage: echo $JSON | jsqry 'query'
args[0] || "",
queryArgsParsed,
params["-1"] || params["--first"],
params["-c"] || params["--compact"]
params["-c"] || params["--compact"],
params["-u"] || params["--unquote"]
);
if (errMsg) {
err(errMsg);
Expand Down

0 comments on commit d34fbfd

Please sign in to comment.