* 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)
| 852 | * Throws error if the indices are for characters that were already removed. |
| 853 | */ |
| 854 | slice(start: number = 0, end: number = this.original.length - this.offset): string { |
| 855 | start = start + this.offset |
| 856 | end = end + this.offset |
| 857 | |
| 858 | if (this.original.length !== 0) { |
| 859 | while (start < 0) start += this.original.length |
| 860 | while (end < 0) end += this.original.length |
| 861 | } |
| 862 | |
| 863 | let result = '' |
| 864 | |
| 865 | // find start chunk |
| 866 | let chunk = this.firstChunk |
| 867 | while (chunk && (chunk.start > start || chunk.end <= start)) { |
| 868 | // found end chunk before start |
| 869 | if (chunk.start < end && chunk.end >= end) { |
| 870 | return result |
| 871 | } |
| 872 | |
| 873 | chunk = chunk.next |
| 874 | } |
| 875 | |
| 876 | if (chunk && chunk.edited && chunk.start !== start) |
| 877 | throw new Error(`Cannot use replaced character ${start} as slice start anchor.`) |
| 878 | |
| 879 | const startChunk = chunk |
| 880 | while (chunk) { |
| 881 | if (chunk.intro && (startChunk !== chunk || chunk.start === start)) { |
| 882 | result += chunk.intro |
| 883 | } |
| 884 | |
| 885 | const containsEnd = chunk.start < end && chunk.end >= end |
| 886 | if (containsEnd && chunk.edited && chunk.end !== end) |
| 887 | throw new Error(`Cannot use replaced character ${end} as slice end anchor.`) |
| 888 | |
| 889 | const sliceStart = startChunk === chunk ? start - chunk.start : 0 |
| 890 | const sliceEnd = containsEnd ? chunk.content.length + end - chunk.end : chunk.content.length |
| 891 | |
| 892 | result += chunk.content.slice(sliceStart, sliceEnd) |
| 893 | |
| 894 | if (chunk.outro && (!containsEnd || chunk.end === end)) { |
| 895 | result += chunk.outro |
| 896 | } |
| 897 | |
| 898 | if (containsEnd) { |
| 899 | break |
| 900 | } |
| 901 | |
| 902 | chunk = chunk.next |
| 903 | } |
| 904 | |
| 905 | return result |
| 906 | } |
| 907 | |
| 908 | // TODO deprecate this? not really very useful |
| 909 | /** |
no outgoing calls
no test coverage detected