Make user search do a bit better on word boundary

pull/21833/head
David Baker 2017-01-17 18:17:51 +00:00
parent fcb1d7a664
commit a87e7d6617
1 changed files with 15 additions and 6 deletions

View File

@ -27,6 +27,10 @@ var Modal = require('../../../Modal');
const TRUNCATE_QUERY_LIST = 40; const TRUNCATE_QUERY_LIST = 40;
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
module.exports = React.createClass({ module.exports = React.createClass({
displayName: "ChatInviteDialog", displayName: "ChatInviteDialog",
propTypes: { propTypes: {
@ -315,13 +319,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
let expr = new RegExp("(?:^|[\\s\\('\",\.-])" + escapeRegExp(query));
if (expr.test(name)) {
return true;
} }
return false; return false;
}, },