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

Method _replaceString

src/MagicString.ts:1265–1287  ·  view source on GitHub ↗

@internal

(string: string, replacement: string | ReplacementFunction)

Source from the content-addressed store, hash-verified

1263
1264 /** @internal */
1265 _replaceString(string: string, replacement: string | ReplacementFunction): this {
1266 const { original } = this
1267 const index = original.indexOf(string)
1268
1269 if (index !== -1) {
1270 if (typeof replacement === 'function') {
1271 replacement = replacement(string, index, original)
1272 }
1273 if (string !== replacement) {
1274 if (string.length === 0) {
1275 // an empty search string matches the empty range at the start of the
1276 // string, which has no characters to overwrite - the replacement is an
1277 // insertion there, as it is for `String.prototype.replace`
1278 this.appendRight(index, replacement)
1279 }
1280 else {
1281 this.overwrite(index, index + string.length, replacement)
1282 }
1283 }
1284 }
1285
1286 return this
1287 }
1288
1289 /**
1290 * String replacement with RegExp or string.

Callers 1

replaceMethod · 0.95

Calls 2

appendRightMethod · 0.95
overwriteMethod · 0.95

Tested by

no test coverage detected