From ee9bd23169ae28f5fd6c084b27992872cf9ef09d Mon Sep 17 00:00:00 2001 From: coopw1 <48886919+coopw1@users.noreply.github.com> Date: Tue, 9 Jan 2024 15:54:11 -0500 Subject: [PATCH] add unicode conversion to better work with the MusicBrainz guidelines --- src/commands/general/top.js | 25 +++++++++++++---- src/commands/util/stringToUnicode.js | 42 ++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 src/commands/util/stringToUnicode.js diff --git a/src/commands/general/top.js b/src/commands/general/top.js index c407c83..88437ae 100644 --- a/src/commands/general/top.js +++ b/src/commands/general/top.js @@ -1,8 +1,10 @@ const { ApplicationCommandOptionType, EmbedBuilder } = require("discord.js"); + const getTopStatistics = require("./util/getTopStatistics"); const pagination = require("../util/pagination"); const getMBID = require("./util/getMBID"); const getAuth = require("../util/getAuth"); +const stringToUnicode = require("../util/stringToUnicode"); module.exports = { name: "top", @@ -327,7 +329,7 @@ module.exports = { return; } - const count = interaction.options.get("artist") ? 100 : 100; + const count = interaction.options.get("artist") ? 1000 : 100; const topStatistics = await getTopStatistics( listenBrainzToken, brainzUsername, @@ -352,9 +354,18 @@ module.exports = { let footer; if (interaction.options.get("artist")) { topStatistics[searchType] = topStatistics[searchType].filter((item) => { - return item.artist_name - .toLowerCase() - .includes(interaction.options.get("artist").value.toLowerCase()); + return ( + item.artist_name + .toLowerCase() + .includes(interaction.options.get("artist").value.toLowerCase()) || + item.artist_name + .toLowerCase() + .includes( + stringToUnicode( + interaction.options.get("artist").value.toLowerCase() + ) + ) + ); }); if (topStatistics[searchType].length === 0) { const embed = new EmbedBuilder() @@ -367,9 +378,11 @@ module.exports = { interaction.editReply({ embeds: [embed], ephemeral: true }); return; } - footer = ` • ${topStatistics[searchType].length} results for ${ + footer = ` • ${ + topStatistics[searchType].length + } results for ${stringToUnicode( interaction.options.get("artist").value - }`; + )}`; } const maxPages = Math.ceil(topStatistics[searchType].length / 10); diff --git a/src/commands/util/stringToUnicode.js b/src/commands/util/stringToUnicode.js new file mode 100644 index 0000000..22854d8 --- /dev/null +++ b/src/commands/util/stringToUnicode.js @@ -0,0 +1,42 @@ +module.exports = function stringToUnicode(inputString) { + const regexPatterns = [ + [/(?<=[^\p{L}\d]|^)"(.+?)"(?=[^\p{L}\d]|$)/gu, "\u201c$1\u201d"], + [/(?<=\W|^)'(n)'(?=\W|$)/gi, "\u2019$1\u2019"], + [/(?<=[^\p{L}\d]|^)'(.+?)'(?=[^\p{L}\d]|$)/gu, "\u2018$1\u2019"], + [/(\d+)"/g, "$1\u2033"], + [/(\d+)'(\d+)/g, "$1\u2032$2"], + [/'/g, "\u2019"], + [/(? (Number.isNaN(Date.parse(e)) ? e : e.replaceAll("-", "\u2010")), + ], + [/\d+(-\d+){2,}/g, (e) => e.replaceAll("-", "\u2012")], + [/(\d+)-(\d+)/g, "$1\u2013$2"], + [/(?<=\S)-(?=\S)/g, "\u2010"], + ]; + + const replacePatterns = [ + [/\[(.+?)(\|.+?)?\]/g, (e, t, a = "") => `[${btoa(t)}${a}]`], + [/(?<=\/\/)(\S+)/g, (e, t) => btoa(t)], + [/'''/g, ""], + [/''/g, ""], + ...regexPatterns, + [//g, "'''"], + [//g, "''"], + [/(?<=\/\/)([A-Za-z0-9+/=]+)/g, (e, t) => atob(t)], + [/\[([A-Za-z0-9+/=]+)(\|.+?)?\]/g, (e, t, a = "") => `[${atob(t)}${a}]`], + ]; + + function modifyStringWithPatterns(input, patterns) { + let modifiedString = input; + patterns.forEach(([regex, replacement]) => { + modifiedString = modifiedString.replace(regex, replacement); + }); + return modifiedString; + } + + const modifiedString = modifyStringWithPatterns(inputString, regexPatterns); + return modifyStringWithPatterns(modifiedString, replacePatterns); +};