import voca from 'voca' ; import { UniqueNameGenerator , upperCaseSnakeCase , camelCase } from '@sap-cloud-sdk/util' ; import { stripPrefix } from './internal-prefix' ; import { applyPrefixOnJsConflictFunctionImports } from './name-formatting-strategies' ; import { defaultReservedWords , reservedServiceKeywords } from './reserved-words' ; internal export class ServiceNameFormatter { static originalToServiceName ( name ) { let formattedName = name . replace ( /\.|\//g , '_' ) ; formattedName = stripAPIUnderscore ( formattedName ) ; formattedName = stripUnderscoreSrv ( formattedName ) ; formattedName = voca . kebabCase ( formattedName ) ; return formattedName . endsWith ( 'service' ) ? formattedName : `${ formattedName }-service` ; } static directoryToSpeakingModuleName ( packageName ) { return voca . titleCase ( packageName . replace ( /-/g , ' ' ) ) ; } private serviceWideNameGenerator = new UniqueNameGenerator ( '_' , [ ... defaultReservedWords , ... reservedServiceKeywords ] ) ; private parameterNameGenerators : { [ functionImportName ] : UniqueNameGenerator ; } = { } ; private staticPropertyNameGenerators : { [ entitySetOrComplexTypeName ] : UniqueNameGenerator ; } = { } ; private instancePropertyNameGenerators : { [ entitySetOrComplexTypeName ] : UniqueNameGenerator ; } = { } ; constructor ( entitySetNames = [ ] , complexTypeNames = [ ] , functionImportNames = [ ] ) { [ ... entitySetNames , ... complexTypeNames ] . forEach ( entitySetOrComplexTypeName => { this . staticPropertyNameGenerators [ entitySetOrComplexTypeName ] = new UniqueNameGenerator ( '_' , defaultReservedWords ) ; this . instancePropertyNameGenerators [ entitySetOrComplexTypeName ] = new UniqueNameGenerator ( '_' , defaultReservedWords ) ; } ) ; if ( functionImportNames ) { functionImportNames . forEach ( functionImportName => { this . parameterNameGenerators [ functionImportName ] = new UniqueNameGenerator ( '_' , defaultReservedWords ) ; } ) ; } } originalToStaticPropertyName ( originalContainerTypeName , originalPropertyName ) { const transformedName = upperCaseSnakeCase ( stripPrefix ( originalPropertyName ) ) ; const generator = this . getOrInitGenerator ( this . staticPropertyNameGenerators , originalContainerTypeName ) ; return generator . generateAndSaveUniqueName ( transformedName ) ; } originalToInstancePropertyName ( originalContainerTypeName , originalPropertyName ) { const transformedName = camelCase ( stripPrefix ( originalPropertyName ) ) ; const generator = this . getOrInitGenerator ( this . instancePropertyNameGenerators , originalContainerTypeName ) ; return generator . generateAndSaveUniqueName ( transformedName ) ; } originalToFunctionImportName ( str ) { const transformedName = voca . camelCase ( str ) ; const newName = this . serviceWideNameGenerator . generateAndSaveUniqueName ( transformedName ) ; return applyPrefixOnJsConflictFunctionImports ( newName ) ; } originalToActionImportName ( str ) { return this . originalToFunctionImportName ( str ) ; } originalToComplexTypeName ( str ) { const transformedName = stripAUnderscore ( voca . titleCase ( str ) ) . replace ( '_' , '' ) ; return this . serviceWideNameGenerator . generateAndSaveUniqueName ( transformedName , false ) ; } originalToEnumTypeName ( str ) { return this . originalToComplexTypeName ( str ) ; } typeNameToFactoryName ( str ) { const factoryName = `create${ str }` ; return this . serviceWideNameGenerator . generateAndSaveUniqueName ( factoryName ) ; } originalToNavigationPropertyName ( entitySetName , originalPropertyName ) { const transformedName = voca . camelCase ( originalPropertyName ) ; const generator = this . getOrInitGenerator ( this . instancePropertyNameGenerators , entitySetName ) ; return generator . generateAndSaveUniqueName ( transformedName ) ; } originalToParameterName ( originalFunctionImportName , originalParameterName ) { const transformedName = voca . camelCase ( originalParameterName ) ; const generator = this . getOrInitGenerator ( this . parameterNameGenerators , originalFunctionImportName ) ; return generator . generateAndSaveUniqueName ( transformedName ) ; } originalToEntityClassName ( entitySetName ) { let transformedName = entitySetName ; if ( transformedName . endsWith ( 'Collection' ) ) { transformedName = stripCollection ( entitySetName ) ; } transformedName = stripAUnderscore ( voca . titleCase ( transformedName ) ) ; const newNames = this . serviceWideNameGenerator . generateAndSaveUniqueNamesWithSuffixes ( transformedName , getInterfaceNamesSuffixes ( ) , false ) ; return newNames [ 0 ] ; } private getOrInitGenerator ( generators , name ) { if ( ! generators [ name ] ) { generators [ name ] = new UniqueNameGenerator ( '_' , defaultReservedWords ) ; } return generators [ name ] ; } } function stripUnderscoreSrv ( name ) { return name . endsWith ( '_SRV' ) ? name . substr ( 0 , name . length - 4 ) : name ; } function stripAPIUnderscore ( name ) { return name . startsWith ( 'API_' ) ? name . substring ( 4 , name . length ) : name ; } internal export function stripCollection ( name ) { return name . endsWith ( 'Collection' ) ? name . substring ( 0 , name . length - 10 ) : name ; } function stripAUnderscore ( name ) { return name . startsWith ( 'A_' ) ? name . substring ( 2 , name . length ) : name ; } function getInterfaceNamesSuffixes ( ) { return [ 'Type' ] ; }