-
Notifications
You must be signed in to change notification settings - Fork 896
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
Micro-optimize search #482
base: master
Are you sure you want to change the base?
Changes from 3 commits
a15e241
0f27cb7
5227a8f
fe31757
4368a6b
a0e12ab
098175b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
var REGEX_CHARACTERS_PATTERN = /[-[\]{}()*+?.,\\^$|#]/g; | ||
|
||
module.exports = function(list) { | ||
var item, | ||
text, | ||
columns, | ||
searchString, | ||
searchPattern, | ||
customSearch; | ||
|
||
var prepare = { | ||
|
@@ -30,10 +32,14 @@ module.exports = function(list) { | |
columns = (list.searchColumns === undefined) ? prepare.toArray(list.items[0].values()) : list.searchColumns; | ||
} | ||
}, | ||
setSearchString: function(s) { | ||
setSearchPattern: function(s) { | ||
s = list.utils.toString(s).toLowerCase(); | ||
s = s.replace(/[-[\]{}()*+?.,\\^$|#]/g, "\\$&"); // Escape regular expression characters | ||
searchString = s; | ||
if (s === "") { | ||
searchPattern = undefined; | ||
return; | ||
} | ||
s = s.replace(REGEX_CHARACTERS_PATTERN, "\\$&"); // Escape regular expression characters | ||
searchPattern = new RegExp(s); | ||
}, | ||
toArray: function(values) { | ||
var tmpColumn = []; | ||
|
@@ -61,7 +67,7 @@ module.exports = function(list) { | |
values: function(values, column) { | ||
if (values.hasOwnProperty(column)) { | ||
text = list.utils.toString(values[column]).toLowerCase(); | ||
if ((searchString !== "") && (text.search(searchString) > -1)) { | ||
if (searchPattern && (text.search(searchPattern) > -1)) { | ||
return true; | ||
} | ||
} | ||
|
@@ -77,16 +83,16 @@ module.exports = function(list) { | |
list.trigger('searchStart'); | ||
|
||
prepare.resetList(); | ||
prepare.setSearchString(str); | ||
prepare.setSearchPattern(str); | ||
prepare.setOptions(arguments); // str, cols|searchFunction, searchFunction | ||
prepare.setColumns(); | ||
|
||
if (searchString === "" ) { | ||
if (!searchPattern ) { | ||
search.reset(); | ||
} else { | ||
list.searched = true; | ||
if (customSearch) { | ||
customSearch(searchString, columns); | ||
customSearch(searchPattern, columns); | ||
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. This might be a breaking change, depending on how people use the customSearch option. 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.
|
||
} else { | ||
search.list(); | ||
} | ||
|
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.
searchPattern
will never be null: