This commit is contained in:
QuiteAFancyEmerald 2024-07-06 23:20:43 -07:00
commit d286e6c47d

View file

@ -2,6 +2,29 @@ import pkg from './routes.mjs';
import { existsSync, readFileSync } from 'fs';
const { cookingInserts, vegetables, charRandom, splashRandom, cacheBustList, text404 } = pkg;
/*
// Try this instead of the .replace method. Might be more performant.
// Will edit str by replacing all matches of lis with newText.
// Usage: insertText(["<Example1>", "<Example2>"],
// "<Example1> Big Giant Paragraph <Example2> Smol Paragraph",
// stringOrFunctionToGenerateNewText);
*/
const insertText = (lis, str, newText) => {
// The lis argument should be a list of strings containing placeholders.
// This will put other relevant argument types, like a string, into a list.
lis = [].concat(lis);
// Loop through each of the placeholder strings.
for (let i = lis.length - 1, position; i >= 0; i--) {
// Find all matches of a placeholder string and insert new text there.
while ((position = str.indexOf(lis[i])) >= 0)
str = str.slice(0, position)
+ (typeof newText == "function" ? newText() : newText)
+ str.slice(position + lis[i].length);
}
return str;
};
function randomListItem(lis) {
return lis[Math.floor(Math.random() * lis.length)];
}