UNPKG

arrange-act-assert

Version:

The lightweight "Act-Arrange-Assert" oriented testing framework

190 lines (189 loc) 5.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Line = void 0; class Line { constructor(start, end, ignored) { this.start = start; this.end = end; this.ignored = ignored; this._blocks = null; this.length = end - start; } _prepend(block, start, end, count) { if (block.start <= end && block.count === count) { block.start = start; } else { if (block.start < end) { block.start = end; if (block.start === block.end) { if (count > 0) { this._overwrite(block, start, end, count); } else { this._delete(block); } return; } } if (count > 0) { block.prev = { start: start, end: end, count: count, prev: block.prev, next: block }; if (block.prev.prev) { block.prev.prev.next = block.prev; } else { this._blocks = block.prev; } } } } _append(block, start, end, count) { if (block.end >= start && block.count === count) { block.end = end; } else { if (block.end > start) { block.end = start; if (block.start === block.end) { if (count > 0) { this._overwrite(block, start, end, count); } else { this._delete(block); } return block; } } if (count > 0) { block.next = { start: start, end: end, count: count, prev: block, next: block.next }; if (block.next.next) { block.next.next.prev = block.next; } return block.next; } } return block; } _overwrite(block, start, end, count) { block.start = start; block.end = end; block.count = count; } _split(block, start, end, count) { if (block.count !== count) { const oldEnd = block.end; block.end = start; const appendedBlock = this._append(block, start, end, count); if (end < oldEnd) { this.count(end, oldEnd, block.count, appendedBlock); } } } _delete(block) { if (block.prev) { block.prev.next = block.next; } else { this._blocks = block.next; } if (block.next) { block.next.prev = block.prev; } } count(start, end, count, nextBlock = this._blocks) { if (end == null || end > this.length) { end = this.length; } if (start === end) { return; } if (!nextBlock) { this._blocks = { start: start, end: end, count: count, next: null, prev: null }; return; } let lastNextBlock = nextBlock; do { if (nextBlock.end >= end) { if (nextBlock.start < start) { this._split(nextBlock, start, end, count); } else { this._prepend(nextBlock, start, end, count); } return; } else if (nextBlock.end >= start) { let limitEnd = (nextBlock.next && nextBlock.next.start < end) ? nextBlock.next.start : end; if (limitEnd > start) { const appendedBlock = this._append(nextBlock, start, limitEnd, count); if (limitEnd !== end) { this.count(limitEnd, end, count, appendedBlock); } return; } } lastNextBlock = nextBlock; nextBlock = nextBlock.next; } while (nextBlock); this._append(lastNextBlock, start, end, count); } getRanges(partial) { const ranges = []; let nextOffset = 0; if (this.ignored) { ranges.push({ start: 0, end: nextOffset = this.length, count: 1 }); } else { let nextBlock = this._blocks; let lastRange = null; while (nextBlock) { if (lastRange && lastRange.end === nextBlock.start && lastRange.count === nextBlock.count) { lastRange.end = nextBlock.end; if (lastRange.end !== nextOffset) { nextOffset = lastRange.end; } } else { if (nextBlock.start !== nextOffset) { nextOffset = nextBlock.end; } ranges.push(lastRange = { start: nextBlock.start, end: nextBlock.end, count: nextBlock.count }); } nextBlock = nextBlock.next; } } if (partial || nextOffset === this.length) { return ranges; } else { return []; } } } exports.Line = Line;