Skip to content

Commit

Permalink
Fix(filter-query): in & nin object id parse
Browse files Browse the repository at this point in the history
  • Loading branch information
Akalanka47000 authored May 19, 2024
1 parent c8651dd commit a989f41
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/mongoose-filter-query/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const complexOperators = ["and", "or"];

const replaceOperator = (value, operator) => value.replace(`${operator}(`, "").slice(0, -1)
const replaceOperator = (value, operator) => value.replace(`${operator}(`, "").slice(0, -1);

const parseOperatorValue = (value, operator) => {
value = replaceOperator(value, operator);
Expand Down Expand Up @@ -35,16 +35,24 @@ export const mapValue = (value) => {
} else if (value.startsWith("lte(")) {
return { $lte: parseOperatorValue(value, "lte") };
} else if (value.startsWith("in(")) {
return { $in: parseOperatorValue(value, "in").split(",") };
return {
$in: replaceOperator(value, "in")
.split(",")
.map((v) => parseOperatorValue(v))
};
} else if (value.startsWith("nin(")) {
return { $nin: parseOperatorValue(value, "nin").split(",") };
return {
$nin: replaceOperator(value, "nin")
.split(",")
.map((v) => parseOperatorValue(v))
};
} else if (value.startsWith("reg(")) {
const [regex, modifiers] = replaceOperator(value, "reg").split("...[")
const [regex, modifiers] = replaceOperator(value, "reg").split("...[");
return { $regex: new RegExp(regex, modifiers?.slice(0, -1)) };
} else if (value.startsWith("exists(")) {
return { $exists: parseOperatorValue(value, "exists") === "true" };
}
if (value === "true" || value === "false") return value === "true"
if (value === "true" || value === "false") return value === "true";
return value;
};

Expand Down

0 comments on commit a989f41

Please sign in to comment.