diff --git a/index.js b/index.js index d8818e5..f892b74 100644 --- a/index.js +++ b/index.js @@ -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); } } diff --git a/package.json b/package.json index f832029..6ca8ecf 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/utils.js b/utils.js index 90d6bf5..08e9909 100644 --- a/utils.js +++ b/utils.js @@ -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) => { @@ -20,6 +23,7 @@ const searchArrayMatchesAny = (search, items) => { module.exports = { equalsAny, + fuzzyMatch, isSubstringOfAny, searchArrayMatchesAny };