From 2f2f88dedd44e4efe767b5bd53d281492297018b Mon Sep 17 00:00:00 2001 From: kingbri Date: Tue, 27 Jun 2023 21:01:12 -0400 Subject: [PATCH] World Info: Fix array splice method Character lore layers used array.splice() in the wrong way. Somehow this passed my testing through sheer luck. Rewrite for using indicies to splice and for more efficiency with iteration. Signed-off-by: kingbri --- public/script.js | 15 +++++++-------- public/scripts/world-info.js | 18 ++++++++++-------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/public/script.js b/public/script.js index a160d723b..4665ab038 100644 --- a/public/script.js +++ b/public/script.js @@ -5543,21 +5543,20 @@ function openCharacterWorldPopup() { const fileName = getCharaFilename(chid); const tempExtraBooks = selectedWorlds.map((index) => world_names[index]).filter((e) => e !== undefined); - const existingCharLore = charLore.find((e) => e.name === fileName); - if (existingCharLore) { - if (tempExtraBooks.length === 0) { - charLore.splice(existingCharLore, 1); - } else { - existingCharLore.extraBooks = tempExtraBooks; - } - } else { + const existingCharIndex = charLore.findIndex((e) => e.name === fileName); + if (existingCharIndex === -1) { const newCharLoreEntry = { name: fileName, extraBooks: tempExtraBooks } charLore.push(newCharLoreEntry); + } else if (tempExtraBooks.length === 0) { + charLore.splice(existingCharIndex, 1); + } else { + charLore[existingCharIndex].extraBooks = tempExtraBooks; } + Object.assign(world_info, { charLore: charLore }); saveSettingsDebounced(); } diff --git a/public/scripts/world-info.js b/public/scripts/world-info.js index 81df9197f..c292127e8 100644 --- a/public/scripts/world-info.js +++ b/public/scripts/world-info.js @@ -250,16 +250,18 @@ function displayWorldEntries(name, data) { return; } - const existingCharLores = world_info.charLore?.filter((e) => e.extraBooks.includes(name)); - if (existingCharLores && existingCharLores.length > 0) { - existingCharLores.forEach((charLore) => { - const tempCharLore = charLore.extraBooks.filter((e) => e !== name); - if (tempCharLore.length === 0) { - world_info.charLore.splice(charLore, 1); - } else { - charLore.extraBooks = tempCharLore; + if (world_info.charLore) { + world_info.charLore.forEach((charLore, index) => { + if (charLore.extraBooks?.includes(name)) { + const tempCharLore = charLore.extraBooks.filter((e) => e !== name); + if (tempCharLore.length === 0) { + world_info.charLore.splice(index, 1); + } else { + charLore.extraBooks = tempCharLore; + } } }); + saveSettingsDebounced(); }