import invariant from 'invariant' ; /**
 * A serializable object representing a selection of items in the  {@link (Typer:class)} .
 *
 *  public export interface SelectionShape { /**
     * **Inclusive** first item index in selection.
     */ readonly start : number ; /**
     * **Exclusive** last item index in selection.
     */ readonly end : number ; } /**
 * A class representing a range of character indexes.
 * This range can represent a selection of those characters.
 *
 */ export class Selection implements SelectionShape { public readonly start : number ; public readonly end : number ; private constructor ( start , end ? ) { invariant ( end === undefined || end - start >= 0 , 'start must be equal or inferior to end' ) ; this . start = start ; this . end = typeof end === 'number' ? end : start ; } public static between ( one , two ) { return Selection . fromBounds ( Math . min ( one , two ) , Math . max ( one , two ) ) ; } public static fromBounds ( start , end ? ) { return new Selection ( start , end ) ; } public static fromShape ( { start , end } ) { return new Selection ( start , end ) ; } remarks param selectionIndex public touchesIndex ( selectionIndex ) { return selectionIndex >= this . start && selectionIndex <= this . end ; } param selection public touchesSelection ( selection ) { const lowerBound = selection . start ; const upperBound = selection . end ; return this . touchesIndex ( lowerBound ) || this . touchesIndex ( upperBound ) ; } public intersectionLength ( selection ) { const intersection = this . intersection ( selection ) ; return intersection ? intersection . length ( ) : 0 ; } param selection public intersection ( selection ) { const maximumMin = Math . max ( this . start , selection . start ) ; const minimumMax = Math . min ( this . end , selection . end ) ; if ( maximumMin < minimumMax ) { return Selection . fromBounds ( maximumMin , minimumMax ) ; } return null ; } public move ( position ) { const { start , end } = this ; return { start : start + position , end : end + position , } ; } public toShape ( ) { const { start , end } = this ; return { start , end , } ; } public length ( ) { return this . end - this . start ; } }