Add backfill for missing WI entries fields (#4203) * Add backfill for missing WI entries fields Fixes #4202 * Backfill before filter/sorting * Fix array wrapping * Check if characterFilter is array * Add console debug for isArray check * Add isArray check for entry

780bd1fc65145bd95428bacbe17089cffa3fe7a0

Cohee <18619528+Cohee1207@users.noreply.github.com>

Signed
1 files changed, +45 -1Showing whitespace changes
public/scripts/world-info.js+45 -1
@@ -1899,6 +1899,46 @@ function getWIElement(name) {
18991899}
19001900
19011901/**
1902+ * Adds missing fields to WI entries that are present in the entry template, but not in the data.
1903+ * Additionally verify that array/object fields are of the expected type.
1904+ * @param {any[]} data WI entries
1905+ * @returns {any[]} Data with backfilled fields
1906+ */
1907+function addMissingWorldInfoFields(data) {
1908+ data.forEach((entry) => {
1909+ // Add missing fields from the template
1910+ Object.entries(newWorldInfoEntryTemplate).forEach(([key, value]) => {
1911+ if (!Object.hasOwn(entry, key)) {
1912+ entry[key] = structuredClone(value);
1913+ }
1914+ });
1915+
1916+ // Ensure that the key is always an array
1917+ if (!Array.isArray(entry.key)) {
1918+ console.debug('[WI] Fixing invalid "key" field for entry', entry);
1919+ entry.key = [];
1920+ }
1921+
1922+ // Ensure that the keysecondary is always an array
1923+ if (!Array.isArray(entry.keysecondary)) {
1924+ console.debug('[WI] Fixing invalid "keysecondary" field for entry', entry);
1925+ entry.keysecondary = [];
1926+ }
1927+
1928+ // Ensure that the characterFilter is an object with the expected structure
1929+ if (!entry.characterFilter || typeof entry.characterFilter !== 'object' || Array.isArray(entry.characterFilter)) {
1930+ entry.characterFilter = {
1931+ isExclude: false,
1932+ names: [],
1933+ tags: [],
1934+ };
1935+ }
1936+ });
1937+
1938+ return data;
1939+}
1940+
1941+/**
19021942 * Sorts the given data based on the selected sort option
19031943 *
19041944 * @param {any[]} data WI entries
@@ -2123,11 +2163,15 @@ async function displayWorldEntries(name, data, navigation = navigation_option.no
21232163 // Convert the data.entries object into an array
21242164 let entriesArray = Object.keys(data.entries).map(uid => {
21252165 const entry = data.entries[uid];
2166+ if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
2167+ return null;
2168+ }
21262169 entry.displayIndex = entry.displayIndex ?? entry.uid;
21272170 return entry;
2128- });
2171+ }).filter(entry => entry !== null);
21292172
21302173 // Apply the filter and do the chosen sorting
2174+ entriesArray = addMissingWorldInfoFields(entriesArray);
21312175 entriesArray = worldInfoFilter.applyFilters(entriesArray);
21322176 entriesArray = sortWorldInfoEntries(entriesArray);
21332177