Unify {{pick}} and {{random}} regex

- Allow all four possible syntax forms, just to make this easier: {{random:1️⃣:two}}, {{random1️⃣:two}}, {{random::one,two}} and {{random:one,two}}
This commit is contained in:
Wolfsblvt 2024-04-02 00:42:13 +02:00
parent 03a203d607
commit 9b24397f5a

View File

@ -185,8 +185,7 @@ function getTimeSinceLastMessage() {
}
function randomReplace(input, emptyListPlaceholder = '') {
const randomPatternNew = /{{random\s?::\s?([^}]+)}}/gi;
const randomPatternOld = /{{random\s?:\s?([^}]+)}}/gi;
const randomPatternNew = /{{random\s?::?\s?([^}]+)}}/gi;
input = input.replace(randomPatternNew, (match, listString) => {
//split on double colons instead of commas to allow for commas inside random items
@ -202,20 +201,11 @@ function randomReplace(input, emptyListPlaceholder = '') {
//trim() at the end to allow for empty random values
return list[randomIndex].trim();
});
input = input.replace(randomPatternOld, (match, listString) => {
const list = listString.split(',').map(item => item.trim()).filter(item => item.length > 0);
if (list.length === 0) {
return emptyListPlaceholder;
}
const rng = new Math.seedrandom('added entropy.', { entropy: true });
const randomIndex = Math.floor(rng() * list.length);
return list[randomIndex];
});
return input;
}
function pickReplace(input, rawContent, emptyListPlaceholder = '') {
const pickPattern = /{{pick\s?::\s?([^}]+)}}/gi;
const pickPattern = /{{pick\s?::?\s?([^}]+)}}/gi;
const chatIdHash = getStringHash(getCurrentChatId());
const rawContentHash = getStringHash(rawContent);