Nodejs源码分析之Path

今天介绍一下nodejs Path的源码分析,Path的API文档在https://nodejs.org/dist/latest-v5.x/docs/api/path.html,使用相对简单,在API文档中,需要特别说明的是如下的文字:

This module contains utilities for handling and transforming file paths. Almost all these methods perform only string transformations. The file system is not consulted to check whether paths are valid.

也就是说这个类的作用是文件路径字符串的处理,而非验证文件路径的有效性,也就是说: path可以从一个文件路径中利用字符串的处理获取详细的目录,文件后缀,文件名等消息,但是我们无法判断这个路径是否合法有效。

API的类型分为下面几类:

  • 获取文件路径的详细信息,如目录,文件后缀,文件名等
  • 当前系统的一些属性,如分隔符,平台等
  • 文件路径的合成,相对路径的解析和标准化等。

首先查看该模块的导出:

// 当前是否是Windows 平台
var isWindows = process.platform === 'win32';

// 如果是windows,直接导出的win32,否则为posix
if (isWindows)
  module.exports = win32;
else /* posix */
  module.exports = posix;

// 同时导出中含有属性win32 和posix
module.exports.posix = posix;
module.exports.win32 = win32;

从上面的源码可以看出,其导出的为win32 或者posix,后面可以看到这是两个对象。 一个代表的是windows平台,一个是非windows的可移植性操作系统接口,一般就是指Linux。之所有两个不同的平台对象,是因为windows和linux上的文件路径,分隔符等不一样,所以分开处理,但是其实现的API接口都基本都一样,也就是win32和posix对象里面的属性和方法基本一致。为简单和避免重复,我们只分析win32对象的API源码与对象。

查看一下源码中的帮助函数:

// resolves . and .. elements in a path array with directory names there
// must be no slashes or device names (c:\) in the array
// (so also no leading and trailing slashes - it does not distinguish
// relative and absolute paths)
// 解决文件目录中的相对路径
// @parts 文件目录数组,从0- 高位分别代表一级目录
// @allowAboveRoot 布尔值,代表是否可以超过根目录
// @returns, 解决掉相对路径后的数组,比如说数组 
// ['/test', '/re', '..']将会返回 ['/test']
function normalizeArray(parts, allowAboveRoot) {
   
  // 返回值
  var res = [];
  // 遍历数组,处理数组中的相对路径字符 '.' 或者'..'
  for (var i = 0; i < parts.length; i++) {
    // 取得当前的数组的字符
    var p = parts[i];

    // ignore empty parts
    // 对空或者'.'不处理
    if (!p || p === '.')
      continue;
    // 处理相对路径中的'..'
    if (p === '..') {
      if (res.length && res[res.length - 1] !== '..') {
      // 直接弹出返回队列,当没有到达根目录时
        res.pop();
      } else if (allowAboveRoot) {
       //allowAboveRoot 为真时,插入'..'
        res.push('..');
      }
    } else {
     // 非 '.' 和'..'直接插入返回队列。 
      res.push(p);
    }
  }
  // 返回路径数组
  return res;
}

// returns an array with empty elements removed from either end of the input
// array or the original array if no elements need to be removed
//返回带有从头和尾空元素的队列。如果没有空元素,直接返回原来的队列
// 比如说 ['undefined', 1, 2, 'undefined', 3, 'undefined'] 会返回
// [1, 2, 'undefined', 3]
function trimArray(arr) {
   
  // 确定队列的最后一个索引
  var lastIndex = arr.length - 1;
  var start = 0;
  //确定队列中从开始位置的第一个非空元素
  for (; start <= lastIndex; start++) {
    if (arr[start])
      break;
  }
 //确定队列中从结束位置的第一个非空元素
  var end = lastIndex;
  for (; end >= 0; end--) {
    if (arr[end])
      break;
  }
  // 如果没有空元素的情况,直接返回原来的数组
  if (start === 0 && end === lastIndex)
    return arr;
  // 处理异常情况  
  if
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值