1. /**
  2. * Indexes content of entries for non-production use with graceful failure handling.
  3. *
  4. * @param {Array<Object>} entries An array of entry objects. Each object should have a 'content' property.
  5. * @param {Function} indexContent A function that takes content as input and returns an indexable representation.
  6. * @param {Function} saveIndex A function that takes the indexable representation and saves it.
  7. */
  8. function indexEntries(entries, indexContent, saveIndex) {
  9. if (!Array.isArray(entries)) {
  10. console.error("Entries must be an array.");
  11. return;
  12. }
  13. if (typeof indexContent !== 'function') {
  14. console.error("indexContent must be a function.");
  15. return;
  16. }
  17. if (typeof saveIndex !== 'function') {
  18. console.error("saveIndex must be a function.");
  19. return;
  20. }
  21. for (let i = 0; i < entries.length; i++) {
  22. const entry = entries[i];
  23. if (typeof entry !== 'object' || entry === null || !entry.hasOwnProperty('content')) {
  24. console.warn(`Skipping invalid entry at index ${i}: Missing or invalid 'content' property.`);
  25. continue;
  26. }
  27. try {
  28. const indexableContent = indexContent(entry.content); // Convert content to indexable format.
  29. if (typeof indexableContent !== 'undefined') {
  30. saveIndex(indexableContent); // Save the indexable content.
  31. } else {
  32. console.warn(`Skipping entry at index ${i}: indexContent returned undefined.`);
  33. }
  34. } catch (error) {
  35. console.error(`Error processing entry at index ${i}:`, error);
  36. // Continue to the next entry even if one fails.
  37. }
  38. }
  39. console.log("Indexing complete.");
  40. }
  41. export default indexEntries;

Add your comment