This article describes how to use the path. normalize method in node. js. This article describes the path. normalize method description, syntax, use examples, and implementation source code. If you need it, refer
Method description:
Output the path string in the standard format.
Syntax:
The Code is as follows:
Path. normalize (p)
Because this method belongs to the path module, You need to introduce the path module (var path = require ("path") before use "))
Example:
The Code is as follows:
Path. normalize ('/foo/bar // baz/asdf/quux /..')
// Returns
'/Foo/bar/baz/asdf'
Source code:
The Code is as follows:
// Windows version
Exports. normalize = function (path ){
Var result = splitDeviceRe.exe c (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;
};