format export type ClientIdConstituents = { app : string ; os : string ; device : string ; device_id : string ; } ; type PluginKeyConstituents = { type : 'device' ; pluginName : string ; client : string ; } | ( { type : 'client' ; pluginName : string ; client : string ; } & ClientIdConstituents ) ; export function buildClientId ( clientInfo ) { for ( const key of [ 'app' , 'os' , 'device' ] as Array < keyof ClientIdConstituents > ) { if ( ! clientInfo [ key ] ) { console . error ( `Attempted to build clientId with invalid ${ key }: "${ clientInfo [ key ] }` ) ; } } const escapedName = escape ( clientInfo . app ) ; return `${ escapedName }#${ clientInfo . os }#${ clientInfo . device }#${ clientInfo . device_id }` ; } export function deconstructClientId ( clientId ) { if ( ! clientId || clientId . split ( '#' ) . length !== 4 ) { console . error ( `Attempted to deconstruct invalid clientId: "${ clientId }"` ) ; } let [ app , os , device , device_id ] = clientId . split ( '#' ) ; app = unescape ( app ) ; return { app , os , device , device_id , } ; } export function deconstructPluginKey ( pluginKey ) { const parts = pluginKey . split ( '#' ) ; if ( parts . length === 2 ) { return { type : 'device' , client : parts [ 0 ] , pluginName : parts [ 1 ] , } ; } else { const lastHashIndex = pluginKey . lastIndexOf ( '#' ) ; const clientId = pluginKey . slice ( 0 , lastHashIndex ) ; const pluginName = pluginKey . slice ( lastHashIndex + 1 ) ; if ( ! pluginName ) { console . error ( `Attempted to deconstruct invalid pluginKey: "${ pluginKey }"` ) ; } return { type : 'client' , ... deconstructClientId ( clientId ) , client : clientId , pluginName : pluginName , } ; } }