diff --git a/src/gmp/models/filter/__tests__/convert.js b/src/gmp/models/filter/__tests__/convert.js index 8012536885..89d941e666 100644 --- a/src/gmp/models/filter/__tests__/convert.js +++ b/src/gmp/models/filter/__tests__/convert.js @@ -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: diff --git a/src/gmp/models/filter/convert.js b/src/gmp/models/filter/convert.js index c94fa8a1c1..40be6da10d 100644 --- a/src/gmp/models/filter/convert.js +++ b/src/gmp/models/filter/convert.js @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -import {isDefined} from '../../utils/identity'; +import {isDefined, isNumberOrNumberString} from '../../utils/identity'; import {isEmpty} from '../../utils/string'; import {parseInt} from '../../parser'; @@ -87,6 +87,10 @@ const convert = (keyword, value, relation) => { return {value, relation}; } + if (isNumberOrNumberString(keyword)) { + return {value: `${keyword}${relation}${value}`, relation: '~'}; + } + return { value, keyword, diff --git a/src/gmp/models/filter/filterterm.js b/src/gmp/models/filter/filterterm.js index acaf5a0c57..4f06cc4b59 100644 --- a/src/gmp/models/filter/filterterm.js +++ b/src/gmp/models/filter/filterterm.js @@ -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; } diff --git a/src/gmp/utils/identity.js b/src/gmp/utils/identity.js index 8287a8eacf..6cd9b98666 100644 --- a/src/gmp/utils/identity.js +++ b/src/gmp/utils/identity.js @@ -15,6 +15,9 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ + +import {parseFloat} from 'gmp/parser'; + export const {isArray} = global.Array; export const isDefined = value => value !== undefined; @@ -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 =>