Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#436: filter appliance based on value #56

Merged
merged 5 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

* Remove Travis CI - [products/#97](https://github.com/ripe-tech/products/issues/97)
*

### Fixed

*

## [0.13.0] - 2023-11-27

### Added

* `removeFunc` to specify which fields to remove (not filter for) based on functions - [ripe-pulse/#436](https://github.com/ripe-tech/ripe-pulse/issues/436)

### Changed

* Remove Travis CI - [products/#97](https://github.com/ripe-tech/products/issues/97)

## [0.12.4] - 2022-12-19

### Added
Expand Down
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)
joao-conde marked this conversation as resolved.
Show resolved Hide resolved
)
)
);
}

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

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

return params;
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ripe-commons",
"version": "0.12.4",
"version": "0.13.0",
"description": "The RIPE Commons library",
"keywords": [
"commons",
Expand Down
83 changes: 83 additions & 0 deletions test/filter.js
Original file line number Diff line number Diff line change
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
});
});
});
});
2 changes: 1 addition & 1 deletion test/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("Time", function() {
});

it("should format simple date strings and reverse the output", () => {
const result = ripeCommons.dateStringUTC(new Date("10/12/2020") / 1000, "-", {
const result = ripeCommons.dateStringUTC(new Date("10/12/2020Z") / 1000, "-", {
reverseDate: true
});
assert.deepStrictEqual(result, "2020-10-12");
Expand Down
10 changes: 9 additions & 1 deletion types/filter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@ 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>;
joao-conde marked this conversation as resolved.
Show resolved Hide resolved
} = {},
removeFunc: Record<string, () => boolean>
): FilterParams;