SoFunction
Updated on 2025-02-28

Methods in JavaScript implementation

The one in the 10th century is very convenient, and can directly merge paths according to relative or absolute, and use: ([path1], [path2], [...]). Sometimes the front-end also needs this method. How to implement it?

In fact, you can just get the source code from :

1. Change the es6 attributes such as const to var to enable front-end browser compatibility
2. Add a variable sep that determines the delimiter of the road scene, that is, the left slash or the right slash, the first road scene separator shall prevail
3. Just put the referenced variables and functions in a file:

Path's source code:/nodejs/node/blob/master/lib/

var CHAR_FORWARD_SLASH = 47
var CHAR_BACKWARD_SLASH = 92
var CHAR_DOT = 46
function isPathSeparator(code) {
 return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
}
function isPosixPathSeparator(code) {
 return code === CHAR_FORWARD_SLASH;
}
function normalize(path) {
 if ( === 0)
  return '.';
 var isAbsolute = (0) === CHAR_FORWARD_SLASH;
 var trailingSeparator =
  ( - 1) === CHAR_FORWARD_SLASH;
 // Normalize the path
 path = normalizeString(path, !isAbsolute, '/', isPosixPathSeparator);
 if ( === 0 && !isAbsolute)
  path = '.';
 if ( > 0 && trailingSeparator)
  path += '/';
 if (isAbsolute)
  return '/' + path;
 return path;
}
function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
 var res = '';
 var lastSegmentLength = 0;
 var lastSlash = -1;
 var dots = 0;
 var code;
 for (var i = 0; i <= ; ++i) {
  if (i < )
   code = (i);
  else if (isPathSeparator(code))
   break;
  else
   code = CHAR_FORWARD_SLASH;
  if (isPathSeparator(code)) {
   if (lastSlash === i - 1 || dots === 1) {
    // NOOP
   } else if (lastSlash !== i - 1 && dots === 2) {
    if ( < 2 || lastSegmentLength !== 2 ||
      ( - 1) !== CHAR_DOT ||
      ( - 2) !== CHAR_DOT) {
     if ( > 2) {
      const lastSlashIndex = (separator);
      if (lastSlashIndex !==  - 1) {
       if (lastSlashIndex === -1) {
        res = '';
        lastSegmentLength = 0;
       } else {
        res = (0, lastSlashIndex);
        lastSegmentLength =  - 1 - (separator);
       }
       lastSlash = i;
       dots = 0;
       continue;
      }
     } else if ( === 2 ||  === 1) {
      res = '';
      lastSegmentLength = 0;
      lastSlash = i;
      dots = 0;
      continue;
     }
    }
    if (allowAboveRoot) {
     if ( > 0)
      res += `${separator}..`;
     else
      res = '..';
     lastSegmentLength = 2;
    }
   } else {
    if ( > 0)
     res += separator + (lastSlash + 1, i);
    else
     res = (lastSlash + 1, i);
    lastSegmentLength = i - lastSlash - 1;
   }
   lastSlash = i;
   dots = 0;
  } else if (code === CHAR_DOT && dots !== -1) {
   ++dots;
  } else {
   dots = -1;
  }
 }
 return res;
}
function join() {
 if ( === 0)
  return '.';
 var sep = arguments[0].indexOf('/') > -1 ? '/' : '\\'
 var joined;
 var firstPart;
 for (var i = 0; i < ; ++i) {
  var arg = arguments[i];
  if ( > 0) {
   if (joined === undefined)
    joined = firstPart = arg;
   else
    joined += sep + arg;
  }
 }
 if (joined === undefined)
  return '.';
 var needsReplace = true;
 var slashCount = 0;
 if (isPathSeparator((0))) {
  ++slashCount;
  var firstLen = ;
  if (firstLen > 1) {
   if (isPathSeparator((1))) {
    ++slashCount;
    if (firstLen > 2) {
     if (isPathSeparator((2)))
      ++slashCount;
     else {
      // We matched a UNC path in the first part
      needsReplace = false;
     }
    }
   }
  }
 }
 if (needsReplace) {
  // Find any more consecutive slashes we need to replace
  for (; slashCount < ; ++slashCount) {
   if (!isPathSeparator((slashCount)))
    break;
  }
  // Replace the slashes if needed
  if (slashCount >= 2)
   joined = sep + (slashCount);
 }
 return normalize(joined);
}

use:

join('../var/www', '../abc')
> "../var/abc"
join('../var/www', '\abc')
../var/www/abc

Summarize

The above is the method in implementing JavaScript introduced to you by the editor. I hope it will be helpful to you. If it is helpful to you, if you have any questions, please leave me a message and the editor will reply to you in time!