-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move jq utils string to data/
- Loading branch information
Showing
2 changed files
with
62 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`; | ||
} |