Skip to content

Commit

Permalink
fix: powerfilter input parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
daniele-mng committed May 30, 2024
1 parent e456617 commit 084a1f4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/gmp/models/filter/__tests__/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,20 @@ describe('convert tests', () => {
value: 'regexp',
});
});

test('should return correct value and relation when keyword is a number or number string', () => {
expect(convert('123', '456', '=')).toEqual({
value: '123=456',
relation: '~',
});
});

test('should return correct value and relation when keyword is a number', () => {
expect(convert(123, 456, ':')).toEqual({
value: '123:456',
relation: '~',
});
});
});

// vim: set ts=2 sw=2 tw=80:
6 changes: 5 additions & 1 deletion src/gmp/models/filter/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import {isDefined} from '../../utils/identity';
import {isDefined, isNumberOrNumberString} from '../../utils/identity';
import {isEmpty} from '../../utils/string';

import {parseInt} from '../../parser';
Expand Down Expand Up @@ -87,6 +87,10 @@ const convert = (keyword, value, relation) => {
return {value, relation};
}

if (isNumberOrNumberString(keyword)) {
return {value: `${keyword}${relation}${value}`, relation: '~'};
}

return {
value,
keyword,
Expand Down
2 changes: 1 addition & 1 deletion src/gmp/models/filter/filterterm.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class FilterTerm {
* @param {String} relation Relation between keyword and filter, =,<,>,...
*/
constructor({keyword, value, relation}) {
this.keyword = isDefined(keyword) ? keyword.toLowerCase() : keyword;
this.keyword = isDefined(keyword) ? String(keyword).toLowerCase() : keyword;
this.value = value;
this.relation = relation;
}
Expand Down
5 changes: 5 additions & 0 deletions src/gmp/utils/identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import {parseFloat} from 'gmp/parser';

export const {isArray} = global.Array;

export const isDefined = value => value !== undefined;
Expand All @@ -29,6 +32,8 @@ export const isNull = value => value === null;

export const isNumber = value => typeof value === 'number';

export const isNumberOrNumberString = value => !isNaN(parseFloat(value));

export const isFunction = value => typeof value === 'function';

export const isJsDate = value =>
Expand Down

0 comments on commit 084a1f4

Please # to comment.