diff --git a/src/assetLoader.js b/src/assetLoader.js index 82dec2d12..d17f178bd 100644 --- a/src/assetLoader.js +++ b/src/assetLoader.js @@ -13,43 +13,43 @@ const cache = {}; */ export function getUrl(path) { - // Convert path to an array if it is a string - if (typeof path === 'string') { - path = path.split('/'); - } - - // Check if path is empty - if (path.length === 0) { - throw new Error('Path cannot be empty'); - } - - // Check if the result is already in the cache - const cacheKey = path.join('/'); - if (cache[cacheKey]) { - return cache[cacheKey]; - } - - // prev = children (starts with the assets) - // current = what we are looking at now - const result = path.reduce((prev, current) => { - const entity = prev.find((e) => e.id === current); - if (entity === undefined) { - throw new Error(`Could not find asset with path: ${path.join('/')}`); - } - - if (entity.children) { - // If there are still children left, return the children - return entity.children; - } else if (entity.url) { - // When there are no more children left, return the url - return entity.url; - } else { - throw new Error('Entity is of wrong type: ' + entity); - } - }, Assets); - - // Add the result to the cache - cache[cacheKey] = result; - - return result; + // Convert path to an array if it is a string + if (typeof path === 'string') { + path = path.split('/'); + } + + // Check if path is empty + if (path.length === 0) { + throw new Error('Path cannot be empty'); + } + + // Check if the result is already in the cache + const cacheKey = path.join('/'); + if (cache[cacheKey]) { + return cache[cacheKey]; + } + + // prev = children (starts with the assets) + // current = what we are looking at now + const result = path.reduce((prev, current) => { + const entity = prev.find((e) => e.id === current); + if (entity === undefined) { + throw new Error(`Could not find asset with path: ${path.join('/')}`); + } + + if (entity.children) { + // If there are still children left, return the children + return entity.children; + } else if (entity.url) { + // When there are no more children left, return the url + return entity.url; + } else { + throw new Error('Entity is of wrong type: ' + entity); + } + }, Assets); + + // Add the result to the cache + cache[cacheKey] = result; + + return result; }