Function: applyChangesToString

applyChangesToString(text, changes): 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:

1const code = `bootstrap({ 2 target: document.querySelector('#app') 3})`; 4 5const indexOfPropertyName = 13; // Usually determined by analyzing an AST. 6const updatedCode = applyChangesToString(code, [ 7 { 8 type: ChangeType.Insert, 9 index: indexOfPropertyName, 10 text: 'element', 11 }, 12 { 13 type: ChangeType.Delete, 14 start: indexOfPropertyName, 15 length: 6, 16 }, 17]); 18 19bootstrap({ 20 element: document.querySelector('#app'), 21}); 22

Parameters

NameType
textstring
changesStringChange[]

Returns

string