如何JavaScript項(xiàng)目中實(shí)現(xiàn)一個(gè)path.join方法-創(chuàng)新互聯(lián)

如何JavaScript項(xiàng)目中實(shí)現(xiàn)一個(gè)path.join方法?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

成都創(chuàng)新互聯(lián)公司專注于井陘礦企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè),商城開發(fā)。井陘礦網(wǎng)站建設(shè)公司,為井陘礦等地區(qū)提供建站服務(wù)。全流程按需求定制網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)

其實(shí)直接從 node.js 的 path.js 拿到源碼加工一下就可以了:

1. 將 const 等 es6 屬性改為 var,以便前端瀏覽器兼容
2. 添加一個(gè)判斷路戲分隔符的變量 sep,即左斜杠還是右斜杠,以第一個(gè)路戲分隔符為準(zhǔn)
3. 將引用的變量和函數(shù)放到一個(gè)文件里就可以了:

Path 的源碼: https://github.com/nodejs/node/blob/master/lib/path.js

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 (path.length === 0)
  return '.';
 var isAbsolute = path.charCodeAt(0) === CHAR_FORWARD_SLASH;
 var trailingSeparator =
  path.charCodeAt(path.length - 1) === CHAR_FORWARD_SLASH;
 // Normalize the path
 path = normalizeString(path, !isAbsolute, '/', isPosixPathSeparator);
 if (path.length === 0 && !isAbsolute)
  path = '.';
 if (path.length > 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 <= path.length; ++i) {
  if (i < path.length)
   code = path.charCodeAt(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 (res.length < 2 || lastSegmentLength !== 2 ||
      res.charCodeAt(res.length - 1) !== CHAR_DOT ||
      res.charCodeAt(res.length - 2) !== CHAR_DOT) {
     if (res.length > 2) {
      const lastSlashIndex = res.lastIndexOf(separator);
      if (lastSlashIndex !== res.length - 1) {
       if (lastSlashIndex === -1) {
        res = '';
        lastSegmentLength = 0;
       } else {
        res = res.slice(0, lastSlashIndex);
        lastSegmentLength = res.length - 1 - res.lastIndexOf(separator);
       }
       lastSlash = i;
       dots = 0;
       continue;
      }
     } else if (res.length === 2 || res.length === 1) {
      res = '';
      lastSegmentLength = 0;
      lastSlash = i;
      dots = 0;
      continue;
     }
    }
    if (allowAboveRoot) {
     if (res.length > 0)
      res += `${separator}..`;
     else
      res = '..';
     lastSegmentLength = 2;
    }
   } else {
    if (res.length > 0)
     res += separator + path.slice(lastSlash + 1, i);
    else
     res = path.slice(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 (arguments.length === 0)
  return '.';
 var sep = arguments[0].indexOf('/') > -1 ? '/' : '\\'
 var joined;
 var firstPart;
 for (var i = 0; i < arguments.length; ++i) {
  var arg = arguments[i];
  if (arg.length > 0) {
   if (joined === undefined)
    joined = firstPart = arg;
   else
    joined += sep + arg;
  }
 }
 if (joined === undefined)
  return '.';
 var needsReplace = true;
 var slashCount = 0;
 if (isPathSeparator(firstPart.charCodeAt(0))) {
  ++slashCount;
  var firstLen = firstPart.length;
  if (firstLen > 1) {
   if (isPathSeparator(firstPart.charCodeAt(1))) {
    ++slashCount;
    if (firstLen > 2) {
     if (isPathSeparator(firstPart.charCodeAt(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 < joined.length; ++slashCount) {
   if (!isPathSeparator(joined.charCodeAt(slashCount)))
    break;
  }
  // Replace the slashes if needed
  if (slashCount >= 2)
   joined = sep + joined.slice(slashCount);
 }
 return normalize(joined);
}

使用:

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

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。

本文標(biāo)題:如何JavaScript項(xiàng)目中實(shí)現(xiàn)一個(gè)path.join方法-創(chuàng)新互聯(lián)
瀏覽路徑:http://muchs.cn/article38/dhiisp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、網(wǎng)站制作、網(wǎng)站設(shè)計(jì)電子商務(wù)、網(wǎng)站收錄、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)