import { Segment } from './Segment' ; /**
 * An identifier that can uniquely identify an atom in a sequence.
 */ export class Ident { /**
     * The local logical time of the replica that created the identifier.
     */ time : number ; /**
     * The ordered set of path segments that make up the identifier.
     */ private path : Segment [ ] ; param time param path returns constructor ( time , path ) { this . time = time ; this . path = path ; } param str returns static parse ( str ) { try { let [ time , pathstr ] = str . split ( '#' ) ; if ( time === undefined || time . length == 0 ) { throw new Error ( "The ident is missing a timestamp" ) ; } if ( pathstr === undefined || pathstr . length == 0 ) { throw new Error ( "The ident is missing a path" ) ; } let prev = undefined ; let path = pathstr . split ( '.' ) . map ( ( token ) => { let [ digit , replica ] = token . split ( ':' , 2 ) ; if ( ! replica ) replica = prev ; else prev = replica ; return Segment ( Number ( digit ) , replica ) ; } ) ; return new Ident ( Number ( time ) , path ) ; } catch ( err ) { throw new Error ( `Error parsing Ident: ${ err }` ) ; } } param depth returns get ( depth ) { return this . path [ depth ] ; } returns depth ( ) { return this . path . length ; } param other returns compare ( other ) { let depth = Math . max ( this . path . length , other . path . length ) ; for ( let i = 0 ; i < depth ; i ++ ) { let my = this . get ( i ) ; let their = other . get ( i ) ; if ( my === undefined && their !== undefined ) return - 1 ; if ( my !== undefined && their === undefined ) return 1 ; if ( my . digit < their . digit ) return - 1 ; if ( my . digit > their . digit ) return 1 ; if ( my . replica < their . replica ) return - 1 ; if ( my . replica > their . replica ) return 1 ; } if ( this . time < other . time ) return - 1 ; if ( this . time > other . time ) return 1 ; return 0 ; } returns toString ( ) { let prev = undefined ; let path = this . path . map ( ( seg ) => { if ( seg . replica == prev ) { return seg . digit ; } else { prev = seg . replica ; return `${ seg . digit }:${ seg . replica }` ; } } ) ; return `${ this . time }#${ path . join ( '.' ) }` ; } }