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

Method remove

src/MagicString.ts:951–997  ·  view source on GitHub ↗

* Removes the characters from `start` to `end` (of the original string, **not** the generated string). * Content appended or prepended at positions strictly inside the range is removed with it, while * content attached at `start` or `end` is preserved — use `s.overwrite(start, end, '')` to rem

(start: number, end: number)

Source from the content-addressed store, hash-verified

949 * Repeated removals and removals with partially overlapping ranges are allowed.
950 */
951 remove(start: number, end: number): this {
952 start = start + this.offset
953 end = end + this.offset
954
955 if (this.original.length !== 0) {
956 if (start < 0)
957 start = Math.max(0, start + this.original.length)
958 if (end < 0)
959 end = Math.max(0, end + this.original.length)
960 }
961
962 if (start === end)
963 return this
964
965 if (start < 0 || end > this.original.length) {
966 throw new MagicStringError(`range ${start}–${end} is out of bounds`)
967 }
968 if (start > end) {
969 throw new MagicStringError(`end must be greater than start (start: ${start}, end: ${end})`)
970 }
971
972 /* v8 ignore next 2 -- DEBUG is always true in tests */
973 if (DEBUG)
974 this.stats.time('remove')
975
976 this._split(start)
977 this._split(end)
978
979 let chunk = this.byStart.get(start)
980
981 while (chunk) {
982 // inserts anchored to the edges of the removed range are kept; only
983 // inserts strictly inside the range are removed with the original chars
984 if (chunk.start > start)
985 chunk.intro = ''
986 if (chunk.end < end)
987 chunk.outro = ''
988 chunk.edit('', false, true)
989
990 chunk = end > chunk.end ? this.byStart.get(chunk.end) : null
991 }
992
993 /* v8 ignore next 2 -- DEBUG is always true in tests */
994 if (DEBUG)
995 this.stats.timeEnd('remove')
996 return this
997 }
998
999 /**
1000 * Reset the modified characters from `start` to `end` (of the original string, **not** the generated string).

Callers 1

snipMethod · 0.45

Calls 4

_splitMethod · 0.95
timeMethod · 0.80
timeEndMethod · 0.80
editMethod · 0.45

Tested by

no test coverage detected