/**
* Indexes content of entries for non-production use with graceful failure handling.
*
* @param {Array<Object>} entries An array of entry objects. Each object should have a 'content' property.
* @param {Function} indexContent A function that takes content as input and returns an indexable representation.
* @param {Function} saveIndex A function that takes the indexable representation and saves it.
*/
function indexEntries(entries, indexContent, saveIndex) {
if (!Array.isArray(entries)) {
console.error("Entries must be an array.");
return;
}
if (typeof indexContent !== 'function') {
console.error("indexContent must be a function.");
return;
}
if (typeof saveIndex !== 'function') {
console.error("saveIndex must be a function.");
return;
}
for (let i = 0; i < entries.length; i++) {
const entry = entries[i];
if (typeof entry !== 'object' || entry === null || !entry.hasOwnProperty('content')) {
console.warn(`Skipping invalid entry at index ${i}: Missing or invalid 'content' property.`);
continue;
}
try {
const indexableContent = indexContent(entry.content); // Convert content to indexable format.
if (typeof indexableContent !== 'undefined') {
saveIndex(indexableContent); // Save the indexable content.
} else {
console.warn(`Skipping entry at index ${i}: indexContent returned undefined.`);
}
} catch (error) {
console.error(`Error processing entry at index ${i}:`, error);
// Continue to the next entry even if one fails.
}
}
console.log("Indexing complete.");
}
export default indexEntries;
Add your comment