| 1899 | } | 1899 | } |
| 1900 | | 1900 | |
| 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 | */ |
| | 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 | /** |
| 1902 | * Sorts the given data based on the selected sort option | 1942 | * Sorts the given data based on the selected sort option |
| 1903 | * | 1943 | * |
| 1904 | * @param {any[]} data WI entries | 1944 | * @param {any[]} data WI entries |