Skip to content

Commit

Permalink
Add fuzzy search algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
jlukic committed Mar 6, 2015
1 parent cf235f0 commit 4cd4a9e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
3 changes: 2 additions & 1 deletion RELEASE-NOTES.md
Expand Up @@ -5,8 +5,9 @@
**Enhancements**
- **Accordion** - Accordion can now specify a trigger element instead of `title`, added an [example in docs](http://www.semantic-ui.com/modules/accordion.html#changing-trigger)
- **Accordion** - Accordion can now hide while opening animation is still occuring
- **Grid** - Equal width grids will now make column content stretch to full height, not just the column itself (requires flexbox)
- **Grid** - Equal width grids will now make column content stretch to full height, not just the column itself (requires flexbox). See examples [in the grid docs](http://www.semantic-ui.com/collections/grid.html#equal-height)
- **Header** - Labels inside headers have been slightly increased in size
- **Search** - Search now uses internally [fuzzy search](https://github.com/bevacqua/fuzzysearch) as its new full text search algorithm.

**Important Fixes**
- **Build Tools** - Fix issues with minified component CSS `@import` not always being on top of files due to [bug in clean-css](https://github.com/jakubpawlowicz/clean-css/issues/476)
Expand Down
34 changes: 30 additions & 4 deletions src/definitions/modules/search.js
Expand Up @@ -439,8 +439,7 @@ $.fn.search = function(parameters) {
? settings.searchFields
: [settings.searchFields],
searchExp = searchTerm.replace(regExp.escape, '\\$&'),
searchRegExp = new RegExp(regExp.exact + searchExp, 'i'),
fullTextRegExp = new RegExp(searchExp, 'i')
searchRegExp = new RegExp(regExp.exact + searchExp, 'i')
;

source = source || settings.source;
Expand All @@ -462,7 +461,7 @@ $.fn.search = function(parameters) {
if( content[field].match(searchRegExp) ) {
results.push(content);
}
else if( settings.searchFullText && content[field].match(fullTextRegExp) ) {
else if(settings.searchFullText && module.fuzzySearch(searchTerm, content[field]) ) {
fullTextResults.push(content);
}
}
Expand All @@ -472,6 +471,33 @@ $.fn.search = function(parameters) {
}
},

fuzzySearch: function(query, term) {
var
termLength = term.length,
queryLength = query.length
;
query = query.toLowerCase();
term = term.toLowerCase();
if(queryLength > termLength) {
return false;
}
if(queryLength === termLength) {
return (query === term);
}
search: for (var characterIndex = 0, nextCharacterIndex = 0; characterIndex < queryLength; characterIndex++) {
var
queryCharacter = query.charCodeAt(characterIndex)
;
while(nextCharacterIndex < termLength) {
if(term.charCodeAt(nextCharacterIndex++) === queryCharacter) {
continue search;
}
}
return false;
}
return true;
},

parse: {
response: function(response, searchTerm) {
var
Expand Down Expand Up @@ -795,7 +821,7 @@ $.fn.search = function(parameters) {
}
});
}
if ( $.isFunction( found ) ) {
if( $.isFunction( found ) ) {
response = found.apply(context, passedArguments);
}
else if(found !== undefined) {
Expand Down

0 comments on commit 4cd4a9e

Please sign in to comment.