/*********************************************************************
 * Copyright (c) 2019 Red Hat, Inc.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 **********************************************************************/ import { CLASSES } from '../inversify.types' ; import { inject , injectable } from 'inversify' ; import { BrowserTabsUtil } from './BrowserTabsUtil' ; import { Logger } from './Logger' ; @ injectable ( ) export class WorkspaceNameHandler { constructor ( @ inject ( CLASSES . BrowserTabsUtil ) private readonly browserTabsUtil ) { } public generateWorkspaceName ( prefix , randomLength ) { const possibleCharacters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' ; const possibleCharactersLength = possibleCharacters . length ; let randomPart = '' ; Logger . debug ( 'WorkspaceNameHandler.generateWorkspaceName' ) ; for ( let i = 0 ; i < randomLength ; i ++ ) { let currentRandomIndex = Math . floor ( Math . random ( ) * Math . floor ( possibleCharactersLength ) ) ; randomPart += possibleCharacters [ currentRandomIndex ] ; } return prefix + randomPart ; } public async getNameFromUrl ( ) { let workspaceUrl = await this . browserTabsUtil . getCurrentUrl ( ) ; Logger . debug ( `WorkspaceNameHandler.fromWorkspaceUrl  workspaceUrl: ${ workspaceUrl }` ) ; const workspaceUrlParts = workspaceUrl . split ( '/' ) ; const workspaceNameQueryString = workspaceUrlParts [ workspaceUrlParts . length - 1 ] ; const workspaceName = workspaceNameQueryString . split ( '?' ) [ 0 ] ; Logger . debug ( `workspaceName: ${ workspaceName }` ) ; return workspaceName ; } }