Skip to content

Commit

Permalink
Merge c94d879 into 2a9c84e
Browse files Browse the repository at this point in the history
  • Loading branch information
Wardrop authored Dec 19, 2017
2 parents 2a9c84e + c94d879 commit db09284
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ Performs a search for `query` with the provided `options`.
<td valign="top">boolean</td>
<td valign="top">If <code>true</code>, nested fields will be available for search and sort using dot-notation to reference them (e.g. <code>nested.property</code>)<br><em>Warning: can reduce performance</em></td>
</tr>
<tr>
<td valign="top"><code>respect_word_boundaries</code></td>
<td valign="top">boolean</td>
<td valign="top">If <code>true</code>, matches only at start of word boundaries (e.g. the beginning of words, instead of matching the middle of words)</td>
</tr>
</table>

## CLI
Expand Down
5 changes: 3 additions & 2 deletions lib/sifter.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* @param {string} query
* @returns {array}
*/
Sifter.prototype.tokenize = function(query) {
Sifter.prototype.tokenize = function(query, respect_word_boundaries) {
query = trim(String(query || '').toLowerCase());
if (!query || !query.length) return [];

Expand All @@ -62,6 +62,7 @@
}
}
}
if (respect_word_boundaries) regex = "\\b"+regex
tokens.push({
string : words[i],
regex : new RegExp(regex, 'i')
Expand Down Expand Up @@ -318,7 +319,7 @@
return {
options : options,
query : String(query || '').toLowerCase(),
tokens : this.tokenize(query),
tokens : this.tokenize(query, options.respect_word_boundaries),
total : 0,
items : []
};
Expand Down
21 changes: 21 additions & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,27 @@ describe('Sifter', function() {
assert.equal(result.items[0].id, 0);
});

it('should allow word boundaries to be respected', function() {
var sifter = new Sifter([
{name: 'John Smith'},
{name: 'Jane Doe'},
]);
var result = sifter.search('mith', {fields: 'name'});
assert.equal(result.items.length, 1);

var result = sifter.search('mith', {fields: 'name', respect_word_boundaries: true});
assert.equal(result.items.length, 0);

var result = sifter.search('Smi', {fields: 'name', respect_word_boundaries: true});
assert.equal(result.items.length, 1);

var result = sifter.search('John Sm', {fields: 'name', respect_word_boundaries: true});
assert.equal(result.items.length, 1);

var result = sifter.search('ohn Smith', {fields: 'name', respect_word_boundaries: true, conjunction: 'and'});
assert.equal(result.items.length, 0);
});

describe('sorting', function() {
it('should respect "sort_empty" option when query absent', function() {
var sifter = new Sifter([
Expand Down

0 comments on commit db09284

Please sign in to comment.