From a87e7d66170bcae5c28075aef6aae3de696703ae Mon Sep 17 00:00:00 2001 From: David Baker Date: Tue, 17 Jan 2017 18:17:51 +0000 Subject: [PATCH 1/3] 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; }, From 242f5e03016e6aad73890c18914c0d2eaafcc8d4 Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 19 Jan 2017 10:24:21 +0000 Subject: [PATCH 2/3] PR feedback * Doc & properly indent escapeRegExp * Add close bracket to the list of punctuation chars we search after --- src/components/views/dialogs/ChatInviteDialog.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/components/views/dialogs/ChatInviteDialog.js b/src/components/views/dialogs/ChatInviteDialog.js index 59e95d1538..dfeb2a3978 100644 --- a/src/components/views/dialogs/ChatInviteDialog.js +++ b/src/components/views/dialogs/ChatInviteDialog.js @@ -27,8 +27,13 @@ var Modal = require('../../../Modal'); 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, "\\$&"); + return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); } module.exports = React.createClass({ @@ -326,7 +331,7 @@ module.exports = React.createClass({ // * The start of the string // * Whitespace, or // * A fixed number of punctuation characters - let expr = new RegExp("(?:^|[\\s\\('\",\.-])" + escapeRegExp(query)); + let expr = new RegExp("(?:^|[\\s\\(\)'\",\.-])" + escapeRegExp(query)); if (expr.test(name)) { return true; } From b58a67f6b19fec62731a57656c7a1f27246d9fc4 Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 19 Jan 2017 10:51:43 +0000 Subject: [PATCH 3/3] Add more punctuation. Also s/let/const/ --- src/components/views/dialogs/ChatInviteDialog.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/views/dialogs/ChatInviteDialog.js b/src/components/views/dialogs/ChatInviteDialog.js index dfeb2a3978..b5222d77d0 100644 --- a/src/components/views/dialogs/ChatInviteDialog.js +++ b/src/components/views/dialogs/ChatInviteDialog.js @@ -331,7 +331,7 @@ module.exports = React.createClass({ // * The start of the string // * Whitespace, or // * A fixed number of punctuation characters - let expr = new RegExp("(?:^|[\\s\\(\)'\",\.-])" + escapeRegExp(query)); + const expr = new RegExp("(?:^|[\\s\\(\)'\",\.-_@\?;:{}\\[\\]\\#~`\\*\\&\\$])" + escapeRegExp(query)); if (expr.test(name)) { return true; }