urlJoin.js 719 B

123456789101112131415161718192021222324252627282930313233
  1. import path from 'path-browserify';
  2. // Function that properly handles path resolution for parts that have
  3. // a protocol component like "http://".
  4. export function urlJoin( ...args ) {
  5. const protocolRegex = /^[a-zA-Z]+:\/\//;
  6. let lastRoot = - 1;
  7. for ( let i = 0, l = args.length; i < l; i ++ ) {
  8. if ( protocolRegex.test( args[ i ] ) ) {
  9. lastRoot = i;
  10. }
  11. }
  12. if ( lastRoot === - 1 ) {
  13. return path.join( ...args ).replace( /\\/g, '/' );
  14. } else {
  15. const parts = lastRoot <= 0 ? args : args.slice( lastRoot );
  16. const protocol = parts[ 0 ].match( protocolRegex )[ 0 ];
  17. parts[ 0 ] = parts[ 0 ].substring( protocol.length );
  18. return ( protocol + path.join( ...parts ) ).replace( /\\/g, '/' );
  19. }
  20. }