Home  >  Article  >  Web Front-end  >  Instructions for using the path.normalize method in node.js_node.js

Instructions for using the path.normalize method in node.js_node.js

WBOY
WBOYOriginal
2016-05-16 16:28:202319browse

Method description:

Output path string in canonical format.

Grammar:

Copy code The code is as follows:

path.normalize(p)

Since this method belongs to the path module, the path module needs to be introduced before use (var path= require(“path”) )

Example:

Copy code The code is as follows:

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

Source code:

Copy code The code is as follows:

// 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).join('\');

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;
};

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn