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

Method _replaceAllString

src/MagicString.ts:1558–1596  ·  view source on GitHub ↗

@internal

(string: string, replacement: string | ReplacementFunction)

Source from the content-addressed store, hash-verified

1556
1557 /** @internal */
1558 _replaceAllString(string: string, replacement: string | ReplacementFunction): this {
1559 const { original } = this
1560 const stringLength = string.length
1561
1562 // an empty search string matches the empty range before every character plus
1563 // one at the end, and `indexOf` clamps its start index to the string length,
1564 // so it can neither find those ranges nor ever report -1 - step through them
1565 if (stringLength === 0) {
1566 for (let index = 0; index <= original.length; index += 1) {
1567 const _replacement
1568 = typeof replacement === 'function'
1569 ? replacement('', index, original)
1570 : expandReplacement(replacement, '', index, original, [], undefined)
1571 if (_replacement !== '')
1572 this.appendRight(index, _replacement)
1573 }
1574
1575 return this
1576 }
1577
1578 for (
1579 let index = original.indexOf(string);
1580 index !== -1;
1581 index = original.indexOf(string, index + stringLength)
1582 ) {
1583 if (this._hasRemovedContent(index, index + stringLength))
1584 continue
1585
1586 const previous = original.slice(index, index + stringLength)
1587 const _replacement
1588 = typeof replacement === 'function'
1589 ? replacement(previous, index, original)
1590 : expandReplacement(replacement, previous, index, original, [], undefined)
1591 if (previous !== _replacement)
1592 this.overwrite(index, index + stringLength, _replacement)
1593 }
1594
1595 return this
1596 }
1597
1598 /**
1599 * Same as `s.replace`, but replace all matched strings instead of just one.

Callers 1

replaceAllMethod · 0.95

Calls 5

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

Tested by

no test coverage detected