UNPKG

three

Version:

JavaScript 3D library

60 lines (42 loc) 1.29 kB
/** * A class with loader utility functions. */ class LoaderUtils { /** * Extracts the base URL from the given URL. * * @param {string} url -The URL to extract the base URL from. * @return {string} The extracted base URL. */ static extractUrlBase( url ) { const index = url.lastIndexOf( '/' ); if ( index === - 1 ) return './'; return url.slice( 0, index + 1 ); } /** * Resolves relative URLs against the given path. Absolute paths, data urls, * and blob URLs will be returned as is. Invalid URLs will return an empty * string. * * @param {string} url -The URL to resolve. * @param {string} path - The base path for relative URLs to be resolved against. * @return {string} The resolved URL. */ static resolveURL( url, path ) { // Invalid URL if ( typeof url !== 'string' || url === '' ) return ''; // Host Relative URL if ( /^https?:\/\//i.test( path ) && /^\//.test( url ) ) { path = path.replace( /(^https?:\/\/[^\/]+).*/i, '$1' ); } // Absolute URL http://,https://,// if ( /^(https?:)?\/\//i.test( url ) ) return url; // Data URI if ( /^data:.*,.*$/i.test( url ) ) return url; // Blob URL if ( /^blob:.*$/i.test( url ) ) return url; // Relative URL return path + url; } } export { LoaderUtils };