MCPcopy Create free account
hub / github.com/Rich-Harris/magic-string / _replaceString

Method _replaceString

src/MagicString.ts:1399–1424  ·  view source on GitHub ↗

@internal

(string: string, replacement: string | ReplacementFunction)

Source from the content-addressed store, hash-verified

1397
1398 /** @internal */
1399 _replaceString(string: string, replacement: string | ReplacementFunction): this {
1400 const { original } = this
1401 const index = original.indexOf(string)
1402
1403 if (index !== -1) {
1404 if (typeof replacement === 'function') {
1405 replacement = replacement(string, index, original)
1406 }
1407 else {
1408 replacement = expandReplacement(replacement, string, index, original, [], undefined)
1409 }
1410 if (string !== replacement) {
1411 if (string.length === 0) {
1412 // an empty search string matches the empty range at the start of the
1413 // string, which has no characters to overwrite - the replacement is an
1414 // insertion there, as it is for `String.prototype.replace`
1415 this.appendRight(index, replacement)
1416 }
1417 else {
1418 this.overwrite(index, index + string.length, replacement)
1419 }
1420 }
1421 }
1422
1423 return this
1424 }
1425
1426 /**
1427 * String replacement with RegExp or string.

Callers 1

replaceMethod · 0.95

Calls 3

appendRightMethod · 0.95
overwriteMethod · 0.95
expandReplacementFunction · 0.85

Tested by

no test coverage detected