Skip to content

Commit

Permalink
chore: move jq utils string to data/
Browse files Browse the repository at this point in the history
  • Loading branch information
tokebe committed Oct 3, 2023
1 parent 7ab4e74 commit 1e3844d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 61 deletions.
48 changes: 48 additions & 0 deletions data/jq/utils.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# example increment function
def increment: . + 1;

# converts value to array if not already
def toArray: if (. | type) == "array" then . else [.] end;

# deletes key if empty array
def delifempty(k): (if k == [] then del(k) | . else . end);

# sets inputted value to "empty" if empty array
def remifempty: (if . == [] then empty else . end);

# gets first element of array (if array)
def getfirst: if (. | type) == "array" then .[0] else . end;

# generates a curie from a type and id
def generateCurie(idType; id): id | getfirst | split(":") | last | idType + ":" + .;

# generates a curie from a type and id [string] (by checking queryInputs)
def generateCurieWithInputs(idType; id; queryInputs): (id | getfirst) as $id | reduce (queryInputs | toArray | .[]) as $input (""; if ($id | ascii_upcase | contains($input | ascii_upcase)) then $input else . end) | idType + ":" + .;

# getting a nested field from inputted object (seperated by ., ie. drugcentral.bioactivity)
def get_nested_field(field):
. as $obj | reduce (field | split(".") | .[]) as $subfield ($obj; .[$subfield]?);

# checks if object meets any filter string
def any_filter(filter_strs):
. as $obj | reduce (filter_strs | .[]) as $filter_str (
false;
. or (
($obj | get_nested_field($filter_str | split(":") | first)) == ($filter_str | split(":") | last)
)
);

# checks if object meets all filter strings
def all_filter(filter_strs):
. as $obj | reduce (filter_strs | .[]) as $filter_str (
true;
. and (
$obj | any_filter([$filter_str | split(":") | last | split(",") | .[] | ($filter_str | split(":") | first) + ":" + .])
)
);

# filters the list that is supplied on all conditions
def list_filter_all(filter_strs): if (. | type) == "array" then [.[] | if . | all_filter(filter_strs) then . else empty end] else empty end;

# filters the list that is supplied on any conditions
def list_filter_any(filter_strs): if (. | type) == "array" then [.[] | if . | any_filter(filter_strs) then . else empty end] else empty end;
75 changes: 14 additions & 61 deletions src/jq_utils.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,23 @@
import { JQVariable, BTEKGOperationObject } from "./types"
import { JQVariable, BTEKGOperationObject } from "./types";
import Path from "path";
import fs from "fs";

const functions = `
# example increment function
def increment: . + 1;
# converts value to array if not already
def toArray: if (. | type) == "array" then . else [.] end;
# deletes key if empty array
def delifempty(k): (if k == [] then del(k) | . else . end);
# sets inputted value to "empty" if empty array
def remifempty: (if . == [] then empty else . end);
# gets first element of array (if array)
def getfirst: if (. | type) == "array" then .[0] else . end;
# generates a curie from a type and id
def generateCurie(idType; id): id | getfirst | split(":") | last | idType + ":" + .;
# generates a curie from a type and id [string] (by checking queryInputs)
def generateCurieWithInputs(idType; id; queryInputs): (id | getfirst) as $id | reduce (queryInputs | toArray | .[]) as $input (""; if ($id | ascii_upcase | contains($input | ascii_upcase)) then $input else . end) | idType + ":" + .;
# getting a nested field from inputted object (seperated by ., ie. drugcentral.bioactivity)
def get_nested_field(field):
. as $obj | reduce (field | split(".") | .[]) as $subfield ($obj; .[$subfield]?);
# checks if object meets any filter string
def any_filter(filter_strs):
. as $obj | reduce (filter_strs | .[]) as $filter_str (
false;
. or (
($obj | get_nested_field($filter_str | split(":") | first)) == ($filter_str | split(":") | last)
)
);
# checks if object meets all filter strings
def all_filter(filter_strs):
. as $obj | reduce (filter_strs | .[]) as $filter_str (
true;
. and (
$obj | any_filter([$filter_str | split(":") | last | split(",") | .[] | ($filter_str | split(":") | first) + ":" + .])
)
);
# filters the list that is supplied on all conditions
def list_filter_all(filter_strs): if (. | type) == "array" then [.[] | if . | all_filter(filter_strs) then . else empty end] else empty end;
# filters the list that is supplied on any conditions
def list_filter_any(filter_strs): if (. | type) == "array" then [.[] | if . | any_filter(filter_strs) then . else empty end] else empty end;
`
const functions = fs.readFileSync(Path.resolve(`${__dirname}/../data/jq/utils.jq`), { encoding: "utf8" });

function generateVariables(variables: JQVariable[]) {
let variableString = '';
for (const variable of variables) {
variableString += `${variable.value} as ${variable.name} | `;
}
return variableString;
let variableString = "";
for (const variable of variables) {
variableString += `${variable.value} as ${variable.name} | `;
}
return variableString;
}

export function generateFilterString(filterString: string, edge: BTEKGOperationObject) {
const variables = [
{
name: '$edge',
value: JSON.stringify(edge)
}
name: "$edge",
value: JSON.stringify(edge),
},
];
return `${functions}\n${generateVariables(variables)}${filterString}`
}
return `${functions}\n${generateVariables(variables)}${filterString}`;
}

0 comments on commit 1e3844d

Please sign in to comment.