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

Method _replaceString

src/MagicString.ts:1378–1403  ·  view source on GitHub ↗

@internal

(string: string, replacement: string | ReplacementFunction)

Source from the content-addressed store, hash-verified

1376
1377 /** @internal */
1378 _replaceString(string: string, replacement: string | ReplacementFunction): this {
1379 const { original } = this
1380 const index = original.indexOf(string)
1381
1382 if (index !== -1) {
1383 if (typeof replacement === 'function') {
1384 replacement = replacement(string, index, original)
1385 }
1386 else {
1387 replacement = expandReplacement(replacement, string, index, original, [], undefined)
1388 }
1389 if (string !== replacement) {
1390 if (string.length === 0) {
1391 // an empty search string matches the empty range at the start of the
1392 // string, which has no characters to overwrite - the replacement is an
1393 // insertion there, as it is for `String.prototype.replace`
1394 this.appendRight(index, replacement)
1395 }
1396 else {
1397 this.overwrite(index, index + string.length, replacement)
1398 }
1399 }
1400 }
1401
1402 return this
1403 }
1404
1405 /**
1406 * 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