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

Method _replaceAllString

src/MagicString.ts:1462–1497  ·  view source on GitHub ↗

@internal

(string: string, replacement: string | ReplacementFunction)

Source from the content-addressed store, hash-verified

1460
1461 /** @internal */
1462 _replaceAllString(string: string, replacement: string | ReplacementFunction): this {
1463 const { original } = this
1464 const stringLength = string.length
1465
1466 // an empty search string matches the empty range before every character plus
1467 // one at the end, and `indexOf` clamps its start index to the string length,
1468 // so it can neither find those ranges nor ever report -1 - step through them
1469 if (stringLength === 0) {
1470 for (let index = 0; index <= original.length; index += 1) {
1471 const _replacement
1472 = typeof replacement === 'function'
1473 ? replacement('', index, original)
1474 : expandReplacement(replacement, '', index, original, [], undefined)
1475 if (_replacement !== '')
1476 this.appendRight(index, _replacement)
1477 }
1478
1479 return this
1480 }
1481
1482 for (
1483 let index = original.indexOf(string);
1484 index !== -1;
1485 index = original.indexOf(string, index + stringLength)
1486 ) {
1487 const previous = original.slice(index, index + stringLength)
1488 const _replacement
1489 = typeof replacement === 'function'
1490 ? replacement(previous, index, original)
1491 : expandReplacement(replacement, previous, index, original, [], undefined)
1492 if (previous !== _replacement)
1493 this.overwrite(index, index + stringLength, _replacement)
1494 }
1495
1496 return this
1497 }
1498
1499 /**
1500 * Same as `s.replace`, but replace all matched strings instead of just one.

Callers 1

replaceAllMethod · 0.95

Calls 4

appendRightMethod · 0.95
overwriteMethod · 0.95
expandReplacementFunction · 0.85
sliceMethod · 0.45

Tested by

no test coverage detected