欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用JavaScript实现node.js中的path.join方法

程序员文章站 2022-06-19 21:22:53
node.js中的 path.join 非常方便,能直接按相对或绝对合并路径,使用: path.join([path1], [path2], [...]),有时侯前端也需要...

node.js中的 path.join 非常方便,能直接按相对或绝对合并路径,使用: path.join([path1], [path2], [...]),有时侯前端也需要这种方法,如何实现呢?

其实直接从 node.js 的 path.js 拿到源码加工一下就可以了:

1. 将 const 等 es6 属性改为 var,以便前端浏览器兼容
2. 添加一个判断路戏分隔符的变量 sep,即左斜杠还是右斜杠,以第一个路戏分隔符为准
3. 将引用的变量和函数放到一个文件里就可以了:

path 的源码:

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

总结

以上所述是小编给大家介绍的使用javascript实现node.js中的path.join方法,希望对大家有所帮助,如果对大家有所帮助