From a28918e80ea6ef6e3ed6144b774ae64c9a20750b Mon Sep 17 00:00:00 2001 From: Ivan ROGER Date: Thu, 9 Mar 2023 16:21:23 +0100 Subject: [PATCH] Remove zero-length terms Fix term check on empty text --- lib/search-query-parser.js | 10 +++++++--- test/test.js | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/lib/search-query-parser.js b/lib/search-query-parser.js index ef1f6a7..af93598 100644 --- a/lib/search-query-parser.js +++ b/lib/search-query-parser.js @@ -45,8 +45,7 @@ exports.parse = function (string, options) { var term = match[0]; var sepIndex = term.indexOf(':'); if (sepIndex !== -1) { - var split = term.split(':'), - key = term.slice(0, sepIndex), + var key = term.slice(0, sepIndex), val = term.slice(sepIndex + 1); // Strip surrounding quotes val = val.replace(/^\"|\"$|^\'|\'$/g, ''); @@ -92,6 +91,11 @@ exports.parse = function (string, options) { } }); + if (term.length === 0) { + // Ignore empty strings after cleanup + continue + } + if (isExcludedTerm) { if (exclusion['text']) { if (exclusion['text'] instanceof Array) { @@ -119,7 +123,7 @@ exports.parse = function (string, options) { var term; while (term = terms.pop()) { // When just a simple term - if (term.text) { + if (term.text !== undefined) { // We add it as pure text query.text.push(term.text); // When offsets is true, push a new offset diff --git a/test/test.js b/test/test.js index 8f3653d..128df25 100644 --- a/test/test.js +++ b/test/test.js @@ -118,6 +118,35 @@ describe('Search query syntax parser', function () { parsedAfterStringifySearchQuery.should.be.eql(parsedSearchQuery); }); + it('should return a tokenized string without empty text terms', function () { + var searchQuery = "fancy pyjama wear ''"; + var options = { tokenize: true }; + var parsedSearchQuery = searchquery.parse(searchQuery, options); + + parsedSearchQuery.should.be.an.Object; + parsedSearchQuery.should.have.property('text', ['fancy', 'pyjama', 'wear']); + + var parsedAfterStringifySearchQuery = searchquery.parse(searchquery.stringify(parsedSearchQuery, options), options); + parsedAfterStringifySearchQuery.offsets = undefined; + parsedSearchQuery.offsets = undefined; + parsedAfterStringifySearchQuery.should.be.eql(parsedSearchQuery); + }); + + it('should return a simple string without empty text terms', function () { + var searchQuery = "key:value fancy pyjama wear ''"; + var options = { keywords: ['key'] }; + var parsedSearchQuery = searchquery.parse(searchQuery, options); + + parsedSearchQuery.should.be.an.Object; + parsedSearchQuery.should.have.property('text', 'fancy pyjama wear'); + parsedSearchQuery.should.have.property('key', 'value'); + + var parsedAfterStringifySearchQuery = searchquery.parse(searchquery.stringify(parsedSearchQuery, options), options); + parsedAfterStringifySearchQuery.offsets = undefined; + parsedSearchQuery.offsets = undefined; + parsedAfterStringifySearchQuery.should.be.eql(parsedSearchQuery); + }); + it('should parse a single keyword with no text', function () { var searchQuery = 'from:jul@foo.com'; var options = {keywords: ['from']};