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

Method slice

benchmark/data.js:635–670  ·  view source on GitHub ↗
(start = 0, end = this.original.length)

Source from the content-addressed store, hash-verified

633 return this.intro + lineStr;
634 }
635 slice(start = 0, end = this.original.length) {
636 if (this.original.length !== 0) {
637 while (start < 0) start += this.original.length;
638 while (end < 0) end += this.original.length;
639 }
640 let result = '';
641 let chunk = this.firstChunk;
642 while (chunk && (chunk.start > start || chunk.end <= start)) {
643 if (chunk.start < end && chunk.end >= end) {
644 return result;
645 }
646 chunk = chunk.next;
647 }
648 if (chunk && chunk.edited && chunk.start !== start)
649 throw new Error(`Cannot use replaced character ${start} as slice start anchor.`);
650 const startChunk = chunk;
651 while (chunk) {
652 if (chunk.intro && (startChunk !== chunk || chunk.start === start)) {
653 result += chunk.intro;
654 }
655 const containsEnd = chunk.start < end && chunk.end >= end;
656 if (containsEnd && chunk.edited && chunk.end !== end)
657 throw new Error(`Cannot use replaced character ${end} as slice end anchor.`);
658 const sliceStart = startChunk === chunk ? start - chunk.start : 0;
659 const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length;
660 result += chunk.content.slice(sliceStart, sliceEnd);
661 if (chunk.outro && (!containsEnd || chunk.end === end)) {
662 result += chunk.outro;
663 }
664 if (containsEnd) {
665 break;
666 }
667 chunk = chunk.next;
668 }
669 return result;
670 }
671 snip(start, end) {
672 const clone = this.clone();
673 clone.remove(0, start);

Callers 5

constructorMethod · 0.45
splitMethod · 0.45
cloneMethod · 0.45
overwriteMethod · 0.45
indentMethod · 0.45

Calls

no outgoing calls

Tested by

no test coverage detected