Navigation Menu

Skip to content

Commit

Permalink
Support term
Browse files Browse the repository at this point in the history
  • Loading branch information
darashi committed Aug 1, 2012
1 parent 9ba59ee commit 28770e5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/q-translator.js
Expand Up @@ -43,6 +43,14 @@ QueryTranslator.prototype = {
}
return context.defaultField + ":'" + escapeTerm(term) + "'";
},
translateTerm: function(query, context) {
this.skipSpaces(query, context);
if (query[context.offset] == '"') {
return this.translatePhraseTerm(query, context);
} else {
return this.translateIndividualTerm(query, context);
}
},
skipSpaces: function(query, context) {
for (; context.offset < query.length; context.offset++) {
if (query[context.offset] != " ") {
Expand Down
25 changes: 25 additions & 0 deletions test/q-translator.test.js
Expand Up @@ -27,6 +27,26 @@ function testIndividualTerm(label, individualTerm, expectedBooleanQuery,
});
}

function testTerm(label, term, expectedBooleanQuery, expectedOffset) {
test('term: ' + label + ': ' +
'<' + term + '> -> <' + expectedBooleanQuery + '>', function() {
var translator = new QueryTranslator();
var context = {
offset: 0,
defaultField: 'field'
};
var actualBooleanQuery = translator.translateTerm(term, context);
assert.deepEqual({
booleanQuery: actualBooleanQuery,
offset: context.offset
},
{
booleanQuery: expectedBooleanQuery,
offset: expectedOffset
});
});
}

suite('QueryTranslator', function() {
testIndividualTerm("an individual term",
"star wars",
Expand All @@ -36,4 +56,9 @@ suite('QueryTranslator', function() {
"let's go",
"field:'let\\'s'",
"let's".length);

testTerm("a term",
" star wars",
"field:'star'",
" star".length);
});

0 comments on commit 28770e5

Please sign in to comment.