prisma-json-types-generator
Version:
Changes JsonValues to your custom typescript type
37 lines • 1.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyTextChanges = applyTextChanges;
/**
* Applies multiple text changes to a string efficiently.
* Uses array-based approach to avoid quadratic string concatenation.
*
* @param content The original text content
* @param changes Array of changes to apply
* @returns The text with all changes applied
*/
function applyTextChanges(content, changes) {
if (changes.length === 0) {
return content;
}
// Sort the changes by their start index to apply them in order
const sortedChanges = [...changes].sort((a, b) => a.start - b.start);
const segments = [];
let lastEnd = 0;
for (const change of sortedChanges) {
// Add the unchanged content before this change
if (change.start > lastEnd) {
segments.push(content.substring(lastEnd, change.start));
}
// Add the replacement text
segments.push(change.text);
// Update the position for the next iteration
lastEnd = change.end;
}
// Add any remaining content after the last change
if (lastEnd < content.length) {
segments.push(content.substring(lastEnd));
}
// Join all segments once at the end
return segments.join('');
}
//# sourceMappingURL=text-changes.js.map