Skip to content

Commit

Permalink
Merge pull request #183 from sliit-foss/fix/filter-query-object-id-pa…
Browse files Browse the repository at this point in the history
…rsing

Fix(filter-query): in & nin object id parse
  • Loading branch information
Akalanka47000 authored May 19, 2024
2 parents c8651dd + d6efc2a commit e067694
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions packages/mongoose-filter-query/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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);
if (operator) value = replaceOperator(value, operator);
if (isNaN(value)) {
if (!isNaN(Date.parse(value))) {
value = new Date(value);
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 e067694

Please sign in to comment.