首頁  >  文章  >  web前端  >  node.js中的path.normalize方法使用說明_node.js

node.js中的path.normalize方法使用說明_node.js

WBOY
WBOY原創
2016-05-16 16:28:202317瀏覽

方法說明:

輸出規範格式的path字串。

文法:

複製程式碼 程式碼如下:

path.normalize(p)

由於方法屬於path模組,使用前需引入path模組(var path= require(“path”) )

範例:

複製程式碼 程式碼如下:

path.normalize('/foo/bar//baz/asdf/quux/..')
// returns
'/foo/bar/baz/asdf'

原始碼:

複製程式碼 程式碼如下:

// windows version
  exports.normalize = function(path) {
    var result = splitDeviceRe.exec(path),
        device = result[1] || '',
        isUnc = device && device.charAt(1) !== ':',
        isAbsolute = exports.isAbsolute(path),
        tail = result[3],
        trailingSlash = /[\/]$/.test(tail);
 
    // If device is a drive letter, we'll normalize to lower case.
    if (device && device.charAt(1) === ':') {
      device = device[0].toLowerCase() device.substr(1);
    }
 
    // Normalize the tail path
    tail = normalizeArray(tail.split(/[\/] /).filter(function(p) {
      return !!p;
    }), !isAbsolute).j​​oin('\');
 
    if (!tail && !isAbsolute) {
      tail = '.';
    }
    if (tail && trailingSlash) {
      tail = '\';
    }
 
    // Convert slashes to backslashes when `device` points to an UNC root.
    // Also squash multiple slashes into a single one where appropriate.
    if (isUnc) {
      device = normalizeUNCRoot(device);
    }
 
    return device (isAbsolute ? '\' : '') tail;
  };

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn