Skip to content

Commit

Permalink
feat: specify object of filters to not use if value not good
Browse files Browse the repository at this point in the history
  • Loading branch information
joao-conde committed Nov 27, 2023
1 parent af03ee2 commit 4c8f097
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 6 deletions.
15 changes: 12 additions & 3 deletions js/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ export const filterToParams = (
nameFunc = {},
filterFields = {},
keywordFields = {},
{ imperfectFilterFields = null, keywords = FILTER_KEYWORDS } = {}
{ imperfectFilterFields = null, keywords = FILTER_KEYWORDS } = {},
removeFunc = {}
) => {
let operator = "$or";
const { sort, reverse, filter, start, limit } = options;
Expand All @@ -85,7 +86,10 @@ export const filterToParams = (
const fieldFunc = nameFunc[field];
value = keywords[value] || !fieldFunc ? value : fieldFunc(value);
arithOp = arithOp === "=" ? filterFields[field] : OP_ALIAS[arithOp];
if (!field || !arithOp) continue;

const remove = removeFunc[field] ? removeFunc[field](value) : false;
if (remove || !field || !arithOp) continue;

filters.push(
..._buildFilter(field, arithOp, value, keywordFields, { keywords: keywords })
);
Expand All @@ -107,17 +111,22 @@ export const filterToParams = (
...flatMap(
([field, operator]) =>
_buildFilter(field, operator, filterS, keywordFields, { keywords: keywords }),
Object.entries(imperfectFilterFields)
Object.entries(imperfectFilterFields).filter(
([field, _]) => !removeFunc[field] || !removeFunc[field](filterS)
)
)
);
}

if (sort) {
params.sort = sortS;
}

if (filterS && filters.length > 0) {
params["filters[]"] = filters;
params.filter_operator = operator;
}

return params;
};

Expand Down
87 changes: 85 additions & 2 deletions test/filter.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const assert = require("assert");
const ripeCommons = require("..");

describe("Filter", function() {
describe("#filterToParams()", function() {
describe("Filter", function () {
describe("#filterToParams()", function () {
it("should be able to build a filter string to filter only for id equal to 2", () => {
const options = {
filter: "id=2",
Expand Down Expand Up @@ -552,5 +552,88 @@ describe("Filter", function() {
start_record: 0
});
});

it("should not leave out filter fields when applicable to the searched value", () => {
const options = {
filter: "friends=13",
limit: 5,
start: 0
};
const filterFields = {
friends: "in"
};
const removeFunc = {
friends: value => isNaN(parseInt(value))
};
const result = ripeCommons.filterToParams(
options,
{},
{},
filterFields,
{},
{},
removeFunc
);
assert.deepStrictEqual(result, {
filter_operator: "$and",
"filters[]": ["friends:in:13"],
number_records: 5,
start_record: 0
});
});

it("should leave out filter fields not applicable to the searched value", () => {
const options = {
filter: "friends=john",
limit: 5,
start: 0
};
const filterFields = {
friends: "in"
};
const removeFunc = {
friends: value => isNaN(parseInt(value))
};
const result = ripeCommons.filterToParams(
options,
{},
{},
filterFields,
{},
{},
removeFunc
);
assert.deepStrictEqual(result, {
number_records: 5,
start_record: 0
});
});

it("should leave out imperfect filter strings not applicable to the searched value", () => {
const options = {
filter: "john",
limit: 5,
start: 0
};
const filterFields = {
friends: "in"
};
const removeFunc = {
friends: value => isNaN(parseInt(value))
};
const result = ripeCommons.filterToParams(
options,
{},
{},
filterFields,
{},
{},
removeFunc
);
assert.deepStrictEqual(result, {
number_records: 5,
start_record: 0
});
});
});
});
4 changes: 3 additions & 1 deletion types/filter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ export declare function filterToParams(
nameAlias: Record<string, string>,
nameFunc: Record<string, () => unknown>,
filterFields: Record<string, string>,
keywordFields: Record<string, string[]>
keywordFields: Record<string, string[]>,
{ imperfectFilterFields, keywords }: {imperfectFilterFields: Record<string, string>, keywords: Record<string, () => unknown>} = {},
removeFunc: Record<string, () => boolean>
): FilterParams;

0 comments on commit 4c8f097

Please sign in to comment.