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

Method slice

src/MagicString.js:598–650  ·  view source on GitHub ↗
(start = 0, end = this.original.length - this.offset)

Source from the content-addressed store, hash-verified

596 }
597
598 slice(start = 0, end = this.original.length - this.offset) {
599 start = start + this.offset;
600 end = end + this.offset;
601
602 if (this.original.length !== 0) {
603 while (start < 0) start += this.original.length;
604 while (end < 0) end += this.original.length;
605 }
606
607 let result = '';
608
609 // find start chunk
610 let chunk = this.firstChunk;
611 while (chunk && (chunk.start > start || chunk.end <= start)) {
612 // found end chunk before start
613 if (chunk.start < end && chunk.end >= end) {
614 return result;
615 }
616
617 chunk = chunk.next;
618 }
619
620 if (chunk && chunk.edited && chunk.start !== start)
621 throw new Error(`Cannot use replaced character ${start} as slice start anchor.`);
622
623 const startChunk = chunk;
624 while (chunk) {
625 if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
626 result += chunk.intro;
627 }
628
629 const containsEnd = chunk.start < end && chunk.end >= end;
630 if (containsEnd && chunk.edited && chunk.end !== end)
631 throw new Error(`Cannot use replaced character ${end} as slice end anchor.`);
632
633 const sliceStart = startChunk === chunk ? start - chunk.start : 0;
634 const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
635
636 result += chunk.content.slice(sliceStart, sliceEnd);
637
638 if (chunk.outro && (!containsEnd || chunk.end === end)) {
639 result += chunk.outro;
640 }
641
642 if (containsEnd) {
643 break;
644 }
645
646 chunk = chunk.next;
647 }
648
649 return result;
650 }
651
652 // TODO deprecate this? not really very useful
653 snip(start, end) {

Callers 7

cloneMethod · 0.45
updateMethod · 0.45
_replaceAllStringMethod · 0.45
indentMethod · 0.45
splitMethod · 0.45
constructorMethod · 0.45
addEditMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected