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 -1Ignore whitespace
public/scripts/world-info.js+45 -1
@@ -1899,6 +1899,46 @@ function getWIElement(name) {
1899}1899}
19001900
1901/**1901/**
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 */
1907function 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/**
1902 * Sorts the given data based on the selected sort option1942 * Sorts the given data based on the selected sort option
1903 *1943 *
1904 * @param {any[]} data WI entries1944 * @param {any[]} data WI entries
@@ -2123,11 +2163,15 @@ async function displayWorldEntries(name, data, navigation = navigation_option.no
2123 // Convert the data.entries object into an array2163 // Convert the data.entries object into an array
2124 let entriesArray = Object.keys(data.entries).map(uid => {2164 let entriesArray = Object.keys(data.entries).map(uid => {
2125 const entry = data.entries[uid];2165 const entry = data.entries[uid];
2166 if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
2167 return null;
2168 }
2126 entry.displayIndex = entry.displayIndex ?? entry.uid;2169 entry.displayIndex = entry.displayIndex ?? entry.uid;
2127 return entry;2170 return entry;
2128 });2171 }).filter(entry => entry !== null);
21292172
2130 // Apply the filter and do the chosen sorting2173 // Apply the filter and do the chosen sorting
2174 entriesArray = addMissingWorldInfoFields(entriesArray);
2131 entriesArray = worldInfoFilter.applyFilters(entriesArray);2175 entriesArray = worldInfoFilter.applyFilters(entriesArray);
2132 entriesArray = sortWorldInfoEntries(entriesArray);2176 entriesArray = sortWorldInfoEntries(entriesArray);
21332177