Home > Web Front-end > JS Tutorial > 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
Release: 2016-05-16 16:28:20
Original
2358 people have browsed it

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template