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

Method _replaceAllString

src/MagicString.ts:1417–1452  ·  view source on GitHub ↗

@internal

(string: string, replacement: string | ReplacementFunction)

Source from the content-addressed store, hash-verified

1415
1416 /** @internal */
1417 _replaceAllString(string: string, replacement: string | ReplacementFunction): this {
1418 const { original } = this
1419 const stringLength = string.length
1420
1421 // an empty search string matches the empty range before every character plus
1422 // one at the end, and `indexOf` clamps its start index to the string length,
1423 // so it can neither find those ranges nor ever report -1 - step through them
1424 if (stringLength === 0) {
1425 for (let index = 0; index <= original.length; index += 1) {
1426 const _replacement
1427 = typeof replacement === 'function'
1428 ? replacement('', index, original)
1429 : expandReplacement(replacement, '', index, original, [], undefined)
1430 if (_replacement !== '')
1431 this.appendRight(index, _replacement)
1432 }
1433
1434 return this
1435 }
1436
1437 for (
1438 let index = original.indexOf(string);
1439 index !== -1;
1440 index = original.indexOf(string, index + stringLength)
1441 ) {
1442 const previous = original.slice(index, index + stringLength)
1443 const _replacement
1444 = typeof replacement === 'function'
1445 ? replacement(previous, index, original)
1446 : expandReplacement(replacement, previous, index, original, [], undefined)
1447 if (previous !== _replacement)
1448 this.overwrite(index, index + stringLength, _replacement)
1449 }
1450
1451 return this
1452 }
1453
1454 /**
1455 * 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