All files / roosterjs-content-model-plugins/lib/findReplace/utils sortRanges.ts

100% Statements 5/5
100% Branches 4/4
100% Functions 2/2
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19        1x 4x       8x 1x   7x            
/**
 * @internal
 * Sort ranges in the order of their position in the document
 */
export function sortRanges(ranges: Range[]) {
    return ranges.sort(compareRange);
}
 
function compareRange(r1: Range, r2: Range): number {
    if (r1.startContainer == r2.startContainer) {
        return r1.startOffset - r2.startOffset;
    } else {
        return r1.startContainer.compareDocumentPosition(r2.startContainer) &
            Node.DOCUMENT_POSITION_FOLLOWING
            ? -1
            : 1;
    }
}