-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for Fuzzy Searching Strings #2028 rebased against current master branch to fix merge conflict #2599
Open
philwolstenholme
wants to merge
2
commits into
harvesthq:master
Choose a base branch
from
philwolstenholme:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ GEM | |
|
||
PLATFORMS | ||
ruby | ||
x86-mingw32 | ||
|
||
DEPENDENCIES | ||
compass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ class AbstractChosen | |
@enable_split_word_search = if @options.enable_split_word_search? then @options.enable_split_word_search else true | ||
@group_search = if @options.group_search? then @options.group_search else true | ||
@search_contains = @options.search_contains || false | ||
@search_fuzzy = @options.search_fuzzy || false | ||
@single_backstroke_delete = if @options.single_backstroke_delete? then @options.single_backstroke_delete else true | ||
@max_selected_options = @options.max_selected_options || Infinity | ||
@inherit_select_classes = @options.inherit_select_classes || false | ||
|
@@ -157,7 +158,7 @@ class AbstractChosen | |
searchText = this.get_search_text() | ||
escapedSearchText = searchText.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&") | ||
zregex = new RegExp(escapedSearchText, 'i') | ||
regex = this.get_search_regex(escapedSearchText) | ||
regex = this.get_search_regex(searchText) | ||
|
||
for option in @results_data | ||
|
||
|
@@ -183,9 +184,7 @@ class AbstractChosen | |
|
||
if option.search_match | ||
if searchText.length | ||
startpos = option.search_text.search zregex | ||
text = option.search_text.substr(0, startpos + searchText.length) + '</em>' + option.search_text.substr(startpos + searchText.length) | ||
option.search_text = text.substr(0, startpos) + '<em>' + text.substr(startpos) | ||
option.search_text = this.highlight_search_result(regex, zregex, option.search_text, searchText) | ||
|
||
results_group.group_match = true if results_group? | ||
|
||
|
@@ -201,10 +200,49 @@ class AbstractChosen | |
this.update_results_content this.results_option_build() | ||
this.winnow_results_set_highlight() | ||
|
||
highlight_search_result: (regex, zregex, value, search) -> | ||
if @search_fuzzy | ||
search_text = [] | ||
|
||
tokens = search.split(' ').sort (a, b) -> | ||
b.length - a.length | ||
|
||
search_string = value.split ' ' | ||
|
||
for string in search_string | ||
for token in tokens | ||
if string.indexOf('<em>') == -1 && string.indexOf('</em>') == -1 | ||
string = string.replace(new RegExp('(' + token.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&") + ')', 'gi'), '<em>$1</em>') | ||
search_text.push string | ||
|
||
result = search_text.join ' ' | ||
else | ||
startpos = value.search zregex | ||
text = value.substr(0, startpos + search.length) + '</em>' + value.substr(startpos + search.length) | ||
result = text.substr(0, startpos) + '<em>' + text.substr(startpos) | ||
|
||
result | ||
|
||
get_tokenized_regex_string: (text) -> | ||
regex_tokens = [] | ||
tokens = this.get_tokenized_search_string text | ||
|
||
for text in tokens | ||
text = text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&") | ||
regex_tokens.push "(?=.*" + text + ")" | ||
|
||
regex_tokens.join '' | ||
|
||
get_tokenized_search_string: (text) -> | ||
text.split ' ' | ||
|
||
get_search_regex: (escaped_search_string) -> | ||
regex_anchor = if @search_contains then "" else "^" | ||
regex_flag = if @case_sensitive_search then "" else "i" | ||
new RegExp(regex_anchor + escaped_search_string, regex_flag) | ||
if @search_fuzzy | ||
new RegExp(this.get_tokenized_regex_string(escaped_search_string), 'i') | ||
else | ||
regex_anchor = if @search_contains then "" else "^" | ||
regex_flag = if @case_sensitive_search then "" else "i" | ||
new RegExp(regex_anchor + escaped_search_string, regex_flag) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Chosen used to pass an escaped string here. If search_fuzzy is true, the unescaped string is being escaped in |
||
|
||
search_string_match: (search_string, regex) -> | ||
if regex.test search_string | ||
|
@@ -298,4 +336,3 @@ class AbstractChosen | |
@default_multiple_text: "Select Some Options" | ||
@default_single_text: "Select an Option" | ||
@default_no_result_text: "No results match" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this change accidentally indents this line too far.