Skip to content

Signatures

applyChangesToString(text: string, changes: StringChange[]): string

Applies a list of changes to a string’s original value.

This is useful when working with ASTs.

For Example, to rename a property in a method’s options:

const code = `bootstrap({
target: document.querySelector('#app')
})`;
const indexOfPropertyName = 13; // Usually determined by analyzing an AST.
const updatedCode = applyChangesToString(code, [
{
type: ChangeType.Insert,
index: indexOfPropertyName,
text: 'element'
},
{
type: ChangeType.Delete,
start: indexOfPropertyName,
length: 6
},
]);
bootstrap({
element: document.querySelector('#app')
});

Parameters

  • text: string
  • changes: StringChange[]