Merge pull request #623 from matrix-org/dbkr/user_search_word_boundary
Make user search do a bit better on word boundary
This commit is contained in:
commit
f64a4e0ecb
1 changed files with 20 additions and 6 deletions
|
@ -27,6 +27,15 @@ var Modal = require('../../../Modal');
|
||||||
|
|
||||||
const TRUNCATE_QUERY_LIST = 40;
|
const TRUNCATE_QUERY_LIST = 40;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Escapes a string so it can be used in a RegExp
|
||||||
|
* Basically just replaces: \ ^ $ * + ? . ( ) | { } [ ]
|
||||||
|
* From http://stackoverflow.com/a/6969486
|
||||||
|
*/
|
||||||
|
function escapeRegExp(str) {
|
||||||
|
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = React.createClass({
|
module.exports = React.createClass({
|
||||||
displayName: "ChatInviteDialog",
|
displayName: "ChatInviteDialog",
|
||||||
propTypes: {
|
propTypes: {
|
||||||
|
@ -307,13 +316,18 @@ module.exports = React.createClass({
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// split spaces in name and try matching constituent parts
|
// Try to find the query following a "word boundary", except that
|
||||||
var parts = name.split(" ");
|
// this does avoids using \b because it only considers letters from
|
||||||
for (var i = 0; i < parts.length; i++) {
|
// the roman alphabet to be word characters.
|
||||||
if (parts[i].indexOf(query) === 0) {
|
// Instead, we look for the query following either:
|
||||||
return true;
|
// * The start of the string
|
||||||
}
|
// * Whitespace, or
|
||||||
|
// * A fixed number of punctuation characters
|
||||||
|
const expr = new RegExp("(?:^|[\\s\\(\)'\",\.-_@\?;:{}\\[\\]\\#~`\\*\\&\\$])" + escapeRegExp(query));
|
||||||
|
if (expr.test(name)) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue