* 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)
| 1040 | * Throws error if the indices are for characters that were already removed. |
| 1041 | */ |
| 1042 | slice(start: number = 0, end: number = this.original.length - this.offset): string { |
| 1043 | start = start + this.offset |
| 1044 | end = end + this.offset |
| 1045 | |
| 1046 | if (this.original.length !== 0) { |
| 1047 | if (start < 0) |
| 1048 | start = Math.max(0, start + this.original.length) |
| 1049 | if (end < 0) |
| 1050 | end = Math.max(0, end + this.original.length) |
| 1051 | } |
| 1052 | |
| 1053 | let result = '' |
| 1054 | |
| 1055 | // find start chunk |
| 1056 | let chunk = this.firstChunk |
| 1057 | while (chunk && (chunk.start > start || chunk.end <= start)) { |
| 1058 | // found end chunk before start |
| 1059 | if ((chunk.start < end && chunk.end >= end) || (end === 0 && chunk.start === 0)) { |
| 1060 | return result |
| 1061 | } |
| 1062 | |
| 1063 | chunk = chunk.next |
| 1064 | } |
| 1065 | |
| 1066 | if (chunk && chunk.edited && chunk.start !== start) { |
| 1067 | throw new MagicStringError(`cannot use edited character ${start} as slice start anchor`) |
| 1068 | } |
| 1069 | |
| 1070 | const startChunk = chunk |
| 1071 | while (chunk) { |
| 1072 | // for end === 0 the slice ends at the very start of the original, so stop |
| 1073 | // once the walk reaches the chunk holding that position, taking nothing from it |
| 1074 | if (end === 0 && chunk.start === 0) { |
| 1075 | break |
| 1076 | } |
| 1077 | |
| 1078 | if (chunk.intro && (startChunk !== chunk || chunk.start === start)) { |
| 1079 | result += chunk.intro |
| 1080 | } |
| 1081 | |
| 1082 | const containsEnd = chunk.start < end && chunk.end >= end |
| 1083 | if (containsEnd && chunk.edited && chunk.end !== end) { |
| 1084 | throw new MagicStringError(`cannot use edited character ${end} as slice end anchor`) |
| 1085 | } |
| 1086 | |
| 1087 | const sliceStart = startChunk === chunk ? start - chunk.start : 0 |
| 1088 | const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length |
| 1089 | |
| 1090 | result += chunk.content.slice(sliceStart, sliceEnd) |
| 1091 | |
| 1092 | if (chunk.outro && (!containsEnd || chunk.end === end)) { |
| 1093 | result += chunk.outro |
| 1094 | } |
| 1095 | |
| 1096 | if (containsEnd) { |
| 1097 | break |
| 1098 | } |
| 1099 |
no outgoing calls
no test coverage detected