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

Method _replaceAllString

src/MagicString.ts:1280–1296  ·  view source on GitHub ↗

@internal

(string: string, replacement: string | ReplacementFunction)

Source from the content-addressed store, hash-verified

1278
1279 /** @internal */
1280 _replaceAllString(string: string, replacement: string | ReplacementFunction): this {
1281 const { original } = this
1282 const stringLength = string.length
1283 for (
1284 let index = original.indexOf(string);
1285 index !== -1;
1286 index = original.indexOf(string, index + stringLength)
1287 ) {
1288 const previous = original.slice(index, index + stringLength)
1289 const _replacement
1290 = typeof replacement === 'function' ? replacement(previous, index, original) : replacement
1291 if (previous !== _replacement)
1292 this.overwrite(index, index + stringLength, _replacement)
1293 }
1294
1295 return this
1296 }
1297
1298 /**
1299 * Same as `s.replace`, but replace all matched strings instead of just one.

Callers 1

replaceAllMethod · 0.95

Calls 2

overwriteMethod · 0.95
sliceMethod · 0.45

Tested by

no test coverage detected