/**
* Indexes content of entries for internal use with basic sanity checks.
*
* @param {Array<Object>} entries An array of entry objects. Each object is expected
* to have a 'content' property (string) to be indexed.
* @returns {Object} An object containing the indexed data. Keys are entry IDs,
* values are the indexed content. Returns an empty object if
* input is invalid.
*/
function indexEntries(entries) {
if (!Array.isArray(entries)) {
console.error("Invalid input: entries must be an array.");
return {};
}
const indexedData = {};
for (let i = 0; i < entries.length; i++) {
const entry = entries[i];
if (typeof entry !== 'object' || entry === null) {
console.warn(`Skipping invalid entry at index ${i}: Not an object.`);
continue;
}
if (!entry.hasOwnProperty('content') || typeof entry.content !== 'string') {
console.warn(`Skipping entry at index ${i}: 'content' property missing or not a string.`);
continue;
}
const content = entry.content.toLowerCase(); // Sanitize: lowercase for consistency
const entryId = String(i); // Use index as a simple entry ID
// Basic sanity check: remove leading/trailing whitespace
const trimmedContent = content.trim();
if (trimmedContent === "") {
console.warn(`Skipping entry at index ${i}: Content is empty after trimming.`);
continue;
}
indexedData[entryId] = trimmedContent;
}
return indexedData;
}
Add your comment