Skip to content

Commit

Permalink
add unicode conversion to better work with the MusicBrainz guidelines
Browse files Browse the repository at this point in the history
  • Loading branch information
coopw1 committed Jan 9, 2024
1 parent 54854ea commit ee9bd23
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/commands/general/top.js
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -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);
Expand Down
42 changes: 42 additions & 0 deletions src/commands/util/stringToUnicode.js
Original file line number Diff line number Diff line change
@@ -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"],
[/(?<!\.)\.{3}(?!\.)/g, "\u2026"],
[/ - /g, " \u2013 "],
[
/\d{4}-\d{2}(?:-\d{2})?(?=\W|$)/g,
(e) => (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, "<b>"],
[/''/g, "<i>"],
...regexPatterns,
[/<b>/g, "'''"],
[/<i>/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);
};

0 comments on commit ee9bd23

Please sign in to comment.