import { Constructor , Id , IdsCache } from './inversify.types' ; export let idsCache = { } ; export function resetIdsCache ( ) { idsCache = { } ; } export function generateIdFromName ( name , id ? ) { return id || Symbol ( name ) ; } export function generateIdOfDependency < T > ( constructor , id ? ) { return generateIdFromName ( constructor . name , id ) ; } export function generateIdName ( constructorName ) { const name = constructorName ; return name . charAt ( 0 ) . toUpperCase ( ) + name . slice ( 1 ) ; } export function generateIdNameOfDependency < T > ( constructor , id ? ) { return id ? id . toString ( ) : generateIdName ( constructor . name ) ; } export function addIdToCache ( id , name ) { const existingId = idsCache [ name ] ; if ( existingId ) { return existingId ; } idsCache [ `I${ name }` ] = id ; return ( idsCache [ name ] = id ) ; } export function generateIdAndAddToCache < T > ( constructor , id ? ) { const dependencyId = generateIdOfDependency ( constructor , id ) ; const dependencyIdName = generateIdNameOfDependency ( constructor , id ) ; const cachedId = addIdToCache ( dependencyId , dependencyIdName ) ; return cachedId ; } export function getOrSetIdFromCache ( dependencyIdName , id ? ) { const cachedId = idsCache [ dependencyIdName ] ; if ( cachedId ) { return cachedId ; } const dependencyId = generateIdFromName ( dependencyIdName , id ) ; const newCachedId = addIdToCache ( dependencyId , dependencyIdName ) ; return newCachedId ; }