Sure, if you want to map directly without using an array for explicit mapping, we can adjust our approach to find the corresponding section in the original text in a more algorithmic and step-by-step manner. Here is a version that maps normalized indices back to the original string without explicitly creating a map array.
Explanation:
In this approach, we keep track of how many characters have been removed during the normalization process. When mapping back from normalized indices, we factor in those removed characters.
Here's the modified code:
const normalize = (text) => {
let normalized = '';
let removedCharCount = 0;
const operations = [];
for (let i = 0; i < text.length; i++) {
const char = text[i];
// Define the conditions for removing characters
const isFormattingChar = /[\s-`\n\r\t]/.test(char);
const isStartOfBold = char === '*' && text[i + 1] === '*';
const isNumberedListPrefix = /^\d+\./.test(text.slice(i));
// Record what was removed
if (isFormattingChar) {
removedCharCount++;
continue;
}
if (isStartOfBold) {
removedCharCount += 2; // Skip two characters
i++;
continue;
}
if (isNumberedListPrefix) {
const matchLength = text.slice(i).match(/^\d+\./)[0].length;
removedCharCount += matchLength;
i += matchLength - 1;
continue;
}
normalized += char;
operations.push({ index: i, removedChars: removedCharCount, normalized: char });
}
return { normalized, operations };
};
const mapIndexToOriginal = (operations, normalizedStart, normalizedEnd) => {
let originalStart = -1;
let originalEnd = -1;
let normalizedIndex = 0;
for (let i = 0; i < operations.length; i++) {
const op = operations[i];
if (normalizedIndex === normalizedStart && originalStart === -1) {
originalStart = op.index;
}
if (normalizedIndex === normalizedEnd - 1) {
originalEnd = op.index + 1; // end of the last character of the range
break;
}
normalizedIndex++;
}
if (originalStart === -1) {
throw new Error("Invalid start index");
}
if (originalEnd === -1) {
throw new Error("Invalid end index");
}
return { originalStart, originalEnd };
};
// Example Usage
const text = "This is **bold** text with 1. numbered 2. lists and weird-formats.\nAnd more!";
const { normalized, operations } = normalize(text);
console.log('Normalized text:', normalized);
// Example of mapping normalized indices back to original (e.g., [10, 15] in normalized string)
const normalizedStart = 10;
const normalizedEnd = 15;
const originalIndices = mapIndexToOriginal(operations, normalizedStart, normalizedEnd);
console.log(`Normalized indices [${normalizedStart}, ${normalizedEnd}] map back to original indices [${originalIndices.originalStart}, ${originalIndices.originalEnd}]`);
Explanation:
-
Normalization Process:
- Keep a separate operations log
operations that keeps track of the index of the original text and the cumulative count of removed characters.
- Only add non-removed characters to the
normalized string and log each inclusion operation.
-
Mapping Function:
mapIndexToOriginal uses the operations log to find the corresponding original text index for a given range of indices in the normalized text.
This solution provides a way to trace back normalized indices to the original text without building a full mapping array, but rather by logging and using these logs for step tracing. This might save some memory if the text is extremely large.