From a87e7d66170bcae5c28075aef6aae3de696703ae Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 17 Jan 2017 18:17:51 +0000 Subject: [PATCH] Make user search do a bit better on word boundary --- .../views/dialogs/ChatInviteDialog.js | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/components/views/dialogs/ChatInviteDialog.js b/src/components/views/dialogs/ChatInviteDialog.js index e9a041357f..59e95d1538 100644 --- a/src/components/views/dialogs/ChatInviteDialog.js +++ b/src/components/views/dialogs/ChatInviteDialog.js @@ -27,6 +27,10 @@ var Modal = require('../../../Modal'); const TRUNCATE_QUERY_LIST = 40; +function escapeRegExp(str) { + return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); +} + module.exports = React.createClass({ displayName: "ChatInviteDialog", propTypes: { @@ -315,13 +319,18 @@ module.exports = React.createClass({ return true; } - // split spaces in name and try matching constituent parts - var parts = name.split(" "); - for (var i = 0; i < parts.length; i++) { - if (parts[i].indexOf(query) === 0) { - return true; - } + // Try to find the query following a "word boundary", except that + // this does avoids using \b because it only considers letters from + // the roman alphabet to be word characters. + // Instead, we look for the query following either: + // * 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; },