export class Guid { public static validator = new RegExp ( "^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$" , "i" ) ; public static EMPTY = "00000000-0000-0000-0000-000000000000" ; public static isGuid ( guid ) { const value = guid . toString ( ) ; return guid && ( guid instanceof Guid || Guid . validator . test ( value ) ) ; } public static create ( ) { return new Guid ( [ Guid . gen ( 2 ) , Guid . gen ( 1 ) , Guid . gen ( 1 ) , Guid . gen ( 1 ) , Guid . gen ( 3 ) ] . join ( "-" ) ) ; } public static createEmpty ( ) { return new Guid ( "emptyguid" ) ; } public static parse ( guid ) { return new Guid ( guid ) ; } public static raw ( ) { return [ Guid . gen ( 2 ) , Guid . gen ( 1 ) , Guid . gen ( 1 ) , Guid . gen ( 1 ) , Guid . gen ( 3 ) ] . join ( "-" ) ; } private static gen ( count ) { let out = "" ; for ( let i = 0 ; i < count ; i ++ ) { out += ( ( ( 1 + Math . random ( ) ) * 0x10000 ) | 0 ) . toString ( 16 ) . substring ( 1 ) ; } return out ; } private value : string ; private constructor ( guid ) { if ( ! guid ) { throw new TypeError ( "Invalid argument; `value` has no value." ) ; } this . value = Guid . EMPTY ; if ( guid && Guid . isGuid ( guid ) ) { this . value = guid ; } } public equals ( other ) { return Guid . isGuid ( other ) && this . value === other . toString ( ) ; } public isEmpty ( ) { return this . value === Guid . EMPTY ; } public toString ( ) { return this . value ; } public toJSON ( ) { return { value : this . value , } ; } }