From 4c8f0976506652992ec6723720d8fcacb3aa31b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Conde?= <16060539+joao-conde@users.noreply.github.com> Date: Mon, 27 Nov 2023 17:08:28 +0000 Subject: [PATCH] feat: specify object of filters to not use if value not good --- js/filter.js | 15 ++++++-- test/filter.js | 87 +++++++++++++++++++++++++++++++++++++++++++++-- types/filter.d.ts | 4 ++- 3 files changed, 100 insertions(+), 6 deletions(-) diff --git a/js/filter.js b/js/filter.js index 98cff09..67d2123 100644 --- a/js/filter.js +++ b/js/filter.js @@ -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; @@ -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 }) ); @@ -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; }; diff --git a/test/filter.js b/test/filter.js index 8d81b8c..df3f25a 100644 --- a/test/filter.js +++ b/test/filter.js @@ -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", @@ -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 + }); + }); }); }); diff --git a/types/filter.d.ts b/types/filter.d.ts index dccca95..ba618c8 100644 --- a/types/filter.d.ts +++ b/types/filter.d.ts @@ -14,5 +14,7 @@ export declare function filterToParams( nameAlias: Record, nameFunc: Record unknown>, filterFields: Record, - keywordFields: Record + keywordFields: Record, + { imperfectFilterFields, keywords }: {imperfectFilterFields: Record, keywords: Record unknown>} = {}, + removeFunc: Record boolean> ): FilterParams;