Skip to content

Commit

Permalink
single-token-only option and Updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
anandphulwani committed Feb 28, 2021
1 parent 97270b4 commit af633ec
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -123,6 +123,11 @@ Performs a search for `query` with the provided `options`.
<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>
<tr>
<td valign="top"><code>single_token_only</code></td>
<td valign="top">boolean</td>
<td valign="top">If <code>true</code>, the whole search query is considered just one token, and the search query is not splitted into words.</td>
</tr>
</table>

## CLI
Expand Down
12 changes: 8 additions & 4 deletions lib/sifter.js
Expand Up @@ -45,13 +45,17 @@
* @param {string} query
* @returns {array}
*/
Sifter.prototype.tokenize = function(query, respect_word_boundaries) {
Sifter.prototype.tokenize = function(query, respect_word_boundaries, single_token_only) {
query = trim(String(query || '').toLowerCase());
if (!query || !query.length) return [];

var i, n, regex, letter;
var i, n, regex, letter, words;
var tokens = [];
var words = query.split(/ +/);
if (single_token_only) {
words = [query];
} else {
words = query.split(/ +/);
}

for (i = 0, n = words.length; i < n; i++) {
regex = escape_regex(words[i]);
Expand Down Expand Up @@ -319,7 +323,7 @@
return {
options : options,
query : String(query || '').toLowerCase(),
tokens : this.tokenize(query, options.respect_word_boundaries),
tokens : this.tokenize(query, options.respect_word_boundaries, options.single_token_only),
total : 0,
items : []
};
Expand Down
12 changes: 8 additions & 4 deletions sifter.js
Expand Up @@ -45,13 +45,17 @@
* @param {string} query
* @returns {array}
*/
Sifter.prototype.tokenize = function(query, respect_word_boundaries) {
Sifter.prototype.tokenize = function(query, respect_word_boundaries, single_token_only) {
query = trim(String(query || '').toLowerCase());
if (!query || !query.length) return [];

var i, n, regex, letter;
var i, n, regex, letter, words;
var tokens = [];
var words = query.split(/ +/);
if (single_token_only) {
words = [query];
} else {
words = query.split(/ +/);
}

for (i = 0, n = words.length; i < n; i++) {
regex = escape_regex(words[i]);
Expand Down Expand Up @@ -319,7 +323,7 @@
return {
options : options,
query : String(query || '').toLowerCase(),
tokens : this.tokenize(query, options.respect_word_boundaries),
tokens : this.tokenize(query, options.respect_word_boundaries, options.single_token_only),
total : 0,
items : []
};
Expand Down
2 changes: 1 addition & 1 deletion sifter.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit af633ec

Please sign in to comment.