Skip to content

Commit 4cbce18

Browse files
author
Ivan ROGER
committed
Remove zero-length terms
Fix term check on empty text
1 parent 7fcd56f commit 4cbce18

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

lib/search-query-parser.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ exports.parse = function (string, options) {
4545
var term = match[0];
4646
var sepIndex = term.indexOf(':');
4747
if (sepIndex !== -1) {
48-
var split = term.split(':'),
49-
key = term.slice(0, sepIndex),
48+
var key = term.slice(0, sepIndex),
5049
val = term.slice(sepIndex + 1);
5150
// Strip surrounding quotes
5251
val = val.replace(/^\"|\"$|^\'|\'$/g, '');
@@ -92,6 +91,11 @@ exports.parse = function (string, options) {
9291
}
9392
});
9493

94+
if (term.length === 0) {
95+
// Ignore empty strings after cleanup
96+
continue
97+
}
98+
9599
if (isExcludedTerm) {
96100
if (exclusion['text']) {
97101
if (exclusion['text'] instanceof Array) {
@@ -119,7 +123,7 @@ exports.parse = function (string, options) {
119123
var term;
120124
while (term = terms.pop()) {
121125
// When just a simple term
122-
if (term.text) {
126+
if (term.text !== undefined) {
123127
// We add it as pure text
124128
query.text.push(term.text);
125129
// When offsets is true, push a new offset

test/test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,35 @@ describe('Search query syntax parser', function () {
118118
parsedAfterStringifySearchQuery.should.be.eql(parsedSearchQuery);
119119
});
120120

121+
it('should return a tokenized string without empty text terms', function () {
122+
var searchQuery = "fancy pyjama wear ''";
123+
var options = { tokenize: true };
124+
var parsedSearchQuery = searchquery.parse(searchQuery, options);
125+
126+
parsedSearchQuery.should.be.an.Object;
127+
parsedSearchQuery.should.have.property('text', ['fancy', 'pyjama', 'wear']);
128+
129+
var parsedAfterStringifySearchQuery = searchquery.parse(searchquery.stringify(parsedSearchQuery, options), options);
130+
parsedAfterStringifySearchQuery.offsets = undefined;
131+
parsedSearchQuery.offsets = undefined;
132+
parsedAfterStringifySearchQuery.should.be.eql(parsedSearchQuery);
133+
});
134+
135+
it('should return a simple string without empty text terms', function () {
136+
var searchQuery = "key:value fancy pyjama wear ''";
137+
var options = { keywords: ['key'] };
138+
var parsedSearchQuery = searchquery.parse(searchQuery, options);
139+
140+
parsedSearchQuery.should.be.an.Object;
141+
parsedSearchQuery.should.have.property('text', 'fancy pyjama wear');
142+
parsedSearchQuery.should.have.property('key', 'value');
143+
144+
var parsedAfterStringifySearchQuery = searchquery.parse(searchquery.stringify(parsedSearchQuery, options), options);
145+
parsedAfterStringifySearchQuery.offsets = undefined;
146+
parsedSearchQuery.offsets = undefined;
147+
parsedAfterStringifySearchQuery.should.be.eql(parsedSearchQuery);
148+
});
149+
121150
it('should parse a single keyword with no text', function () {
122151
var searchQuery = 'from:jul@foo.com';
123152
var options = {keywords: ['from']};

0 commit comments

Comments
 (0)