* Returns the content of the generated string that corresponds to the slice between `start` and `end` of the original string. * Throws error if the indices are for characters that were already removed.
(start: number = 0, end: number = this.original.length - this.offset)
| 1028 | * Throws error if the indices are for characters that were already removed. |
| 1029 | */ |
| 1030 | slice(start: number = 0, end: number = this.original.length - this.offset): string { |
| 1031 | start = start + this.offset |
| 1032 | end = end + this.offset |
| 1033 | |
| 1034 | if (this.original.length !== 0) { |
| 1035 | if (start < 0) |
| 1036 | start = Math.max(0, start + this.original.length) |
| 1037 | if (end < 0) |
| 1038 | end = Math.max(0, end + this.original.length) |
| 1039 | } |
| 1040 | |
| 1041 | let result = '' |
| 1042 | |
| 1043 | // find start chunk |
| 1044 | let chunk = this.firstChunk |
| 1045 | while (chunk && (chunk.start > start || chunk.end <= start)) { |
| 1046 | // found end chunk before start |
| 1047 | if (chunk.start < end && chunk.end >= end) { |
| 1048 | return result |
| 1049 | } |
| 1050 | |
| 1051 | chunk = chunk.next |
| 1052 | } |
| 1053 | |
| 1054 | if (chunk && chunk.edited && chunk.start !== start) { |
| 1055 | throw new MagicStringError(`cannot use edited character ${start} as slice start anchor`) |
| 1056 | } |
| 1057 | |
| 1058 | const startChunk = chunk |
| 1059 | while (chunk) { |
| 1060 | if (chunk.intro && (startChunk !== chunk || chunk.start === start)) { |
| 1061 | result += chunk.intro |
| 1062 | } |
| 1063 | |
| 1064 | const containsEnd = chunk.start < end && chunk.end >= end |
| 1065 | if (containsEnd && chunk.edited && chunk.end !== end) { |
| 1066 | throw new MagicStringError(`cannot use edited character ${end} as slice end anchor`) |
| 1067 | } |
| 1068 | |
| 1069 | const sliceStart = startChunk === chunk ? start - chunk.start : 0 |
| 1070 | const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length |
| 1071 | |
| 1072 | result += chunk.content.slice(sliceStart, sliceEnd) |
| 1073 | |
| 1074 | if (chunk.outro && (!containsEnd || chunk.end === end)) { |
| 1075 | result += chunk.outro |
| 1076 | } |
| 1077 | |
| 1078 | if (containsEnd) { |
| 1079 | break |
| 1080 | } |
| 1081 | |
| 1082 | chunk = chunk.next |
| 1083 | } |
| 1084 | |
| 1085 | return result |
| 1086 | } |
| 1087 |
no outgoing calls
no test coverage detected