Implement fuzzy-matching for the user list

This commit is contained in:
Jérémie Astori 2016-12-30 00:32:27 -05:00
parent 30bf20eb12
commit 6a26014b81
3 changed files with 23 additions and 10 deletions

View file

@ -6,6 +6,7 @@ const $ = require("jquery");
const moment = require("moment"); const moment = require("moment");
const Mousetrap = require("mousetrap"); const Mousetrap = require("mousetrap");
const URI = require("urijs"); const URI = require("urijs");
const fuzzy = require("fuzzy");
// our libraries // our libraries
require("./libs/jquery/inputhistory"); require("./libs/jquery/inputhistory");
@ -1067,16 +1068,26 @@ $(function() {
}); });
chat.on("input", ".search", function() { chat.on("input", ".search", function() {
var value = $(this).val().toLowerCase(); const value = $(this).val().toLowerCase();
var names = $(this).closest(".users").find(".names"); const names = $(this).closest(".users").find(".names");
names.find(".user").each(function() {
var btn = $(this); names.find(".user").each((i, el) => {
var name = btn.text().toLowerCase().replace(/[+%@~]/, ""); $(el).text($(el).text().replace(/<\/?b>;/, "")).hide();
if (name.indexOf(value) > -1) { });
btn.show();
} else { const fuzzyOptions = {
btn.hide(); pre: "<b>",
} post: "</b>",
extract: el => $(el).text().toLowerCase().replace(/[+%@~]/, "")
};
fuzzy.filter(
value,
names.find(".user").toArray(),
fuzzyOptions
).forEach(el => {
const firstChar = $(el.original).text()[0].replace(/[^+%@~]/, "");
$(el.original).html(firstChar + el.string).show();
}); });
}); });

View file

@ -65,6 +65,7 @@
"chai": "3.5.0", "chai": "3.5.0",
"eslint": "3.19.0", "eslint": "3.19.0",
"font-awesome": "4.7.0", "font-awesome": "4.7.0",
"fuzzy": "0.1.3",
"handlebars": "4.0.6", "handlebars": "4.0.6",
"handlebars-loader": "1.5.0", "handlebars-loader": "1.5.0",
"jquery": "3.2.1", "jquery": "3.2.1",

View file

@ -18,6 +18,7 @@ let config = {
"mousetrap", "mousetrap",
"socket.io-client", "socket.io-client",
"urijs", "urijs",
"fuzzy",
], ],
}, },
devtool: "source-map", devtool: "source-map",