Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
Fuzzy channel search
Browse files Browse the repository at this point in the history
  • Loading branch information
cuschk committed Dec 12, 2018
1 parent 46f1710 commit e67f91d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,14 @@ function getChannel(id, options) {

function getChannelById(id, channels) {
return new Promise((resolve, reject) => {
if (id === 'random') {
if (utils.fuzzyMatch(id, 'random')) {
resolve(channels[
Math.floor(channels.length * Math.random())
]);
}

for (const channel of channels) {
if (id.toLowerCase() === channel.id) {
if (utils.fuzzyMatch(id, channel.id)) {
resolve(channel);
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"conf": "^2.0.0",
"copy-paste": "^1.3.0",
"date-fns": "^1.29.0",
"fast-levenshtein": "^2.0.6",
"got": "^8.3.2",
"indent-string": "^3.2.0",
"ini": "^1.3.5",
Expand Down
6 changes: 5 additions & 1 deletion utils.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
'use strict';
const levenshtein = require('fast-levenshtein').get;

const equalsAny = (search, arr) => {
return arr.indexOf(search) > -1;
};

const fuzzyMatch = (search, str) => levenshtein(search, str) < 3;

const isSubstringOfAny = (search, arr) => {
return arr.some(str => str.toLowerCase().includes(search));
return arr.some(str => str.toLowerCase().includes(search) || fuzzyMatch(str.toLowerCase(), search));
};

const searchArrayMatchesAny = (search, items) => {
Expand All @@ -20,6 +23,7 @@ const searchArrayMatchesAny = (search, items) => {

module.exports = {
equalsAny,
fuzzyMatch,
isSubstringOfAny,
searchArrayMatchesAny
};

0 comments on commit e67f91d

Please sign in to comment.