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

Method overwrite

benchmark/data.js:505–553  ·  view source on GitHub ↗
(start, end, content, options)

Source from the content-addressed store, hash-verified

503 return this;
504 }
505 overwrite(start, end, content, options) {
506 if (typeof content !== 'string') throw new TypeError('replacement content must be a string');
507 while (start < 0) start += this.original.length;
508 while (end < 0) end += this.original.length;
509 if (end > this.original.length) throw new Error('end is out of bounds');
510 if (start === end)
511 throw new Error(
512 'Cannot overwrite a zero-length range \u2013 use appendLeft or prependRight instead',
513 );
514 this._split(start);
515 this._split(end);
516 if (options === true) {
517 if (!warned.storeName) {
518 console.warn(
519 'The final argument to magicString.overwrite(...) should be an options object. See https://github.com/rich-harris/magic-string',
520 );
521 warned.storeName = true;
522 }
523 options = { storeName: true };
524 }
525 const storeName = options !== void 0 ? options.storeName : false;
526 const contentOnly = options !== void 0 ? options.contentOnly : false;
527 if (storeName) {
528 const original = this.original.slice(start, end);
529 Object.defineProperty(this.storedNames, original, {
530 writable: true,
531 value: true,
532 enumerable: true,
533 });
534 }
535 const first = this.byStart[start];
536 const last = this.byEnd[end];
537 if (first) {
538 let chunk = first;
539 while (chunk !== last) {
540 if (chunk.next !== this.byStart[chunk.end]) {
541 throw new Error('Cannot overwrite across a split point');
542 }
543 chunk = chunk.next;
544 chunk.edit('', false);
545 }
546 first.edit(content, storeName, contentOnly);
547 } else {
548 const newChunk = new Chunk(start, end, '').edit(content, storeName);
549 last.next = newChunk;
550 newChunk.previous = last;
551 }
552 return this;
553 }
554 prepend(content) {
555 if (typeof content !== 'string') throw new TypeError('outro content must be a string');
556 this.intro = content + this.intro;

Callers 2

mainFunction · 0.95
replaceMethod · 0.95

Calls 3

_splitMethod · 0.95
sliceMethod · 0.45
editMethod · 0.45

Tested by

no test coverage detected