首頁 > web前端 > js教程 > 主體

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

WBOY
發布: 2016-05-16 16:28:20
原創
2317 人瀏覽過

方法說明:

輸出規範格式的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;
  };

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!