1. /**
  2. * Indexes content of entries for internal use with basic sanity checks.
  3. *
  4. * @param {Array<Object>} entries An array of entry objects. Each object is expected
  5. * to have a 'content' property (string) to be indexed.
  6. * @returns {Object} An object containing the indexed data. Keys are entry IDs,
  7. * values are the indexed content. Returns an empty object if
  8. * input is invalid.
  9. */
  10. function indexEntries(entries) {
  11. if (!Array.isArray(entries)) {
  12. console.error("Invalid input: entries must be an array.");
  13. return {};
  14. }
  15. const indexedData = {};
  16. for (let i = 0; i < entries.length; i++) {
  17. const entry = entries[i];
  18. if (typeof entry !== 'object' || entry === null) {
  19. console.warn(`Skipping invalid entry at index ${i}: Not an object.`);
  20. continue;
  21. }
  22. if (!entry.hasOwnProperty('content') || typeof entry.content !== 'string') {
  23. console.warn(`Skipping entry at index ${i}: 'content' property missing or not a string.`);
  24. continue;
  25. }
  26. const content = entry.content.toLowerCase(); // Sanitize: lowercase for consistency
  27. const entryId = String(i); // Use index as a simple entry ID
  28. // Basic sanity check: remove leading/trailing whitespace
  29. const trimmedContent = content.trim();
  30. if (trimmedContent === "") {
  31. console.warn(`Skipping entry at index ${i}: Content is empty after trimming.`);
  32. continue;
  33. }
  34. indexedData[entryId] = trimmedContent;
  35. }
  36. return indexedData;
  37. }

Add your comment