path.normalize(path)


path.normalize() 方法规范化给定的 path,解析 '..''.' 片段。

当找到多个连续的路径段分隔字符时(例如 POSIX 上的 /、Windows 上的 \/),则它们将被替换为单个平台特定的路径段分隔符(POSIX 上的 /、Windows 上的 \)。 尾部的分隔符会保留。

如果 path 是零长度的字符串,则返回 '.',表示当前工作目录。

例如,在 POSIX 上:

path.normalize('/foo/bar//baz/asdf/quux/..');
// 返回: '/foo/bar/baz/asdf'

在 Windows 上:

path.normalize('C:\\temp\\\\foo\\bar\\..\\');
// 返回: 'C:\\temp\\foo\\'

由于 Windows 识别多种路径分隔符,因此这些分隔符都将被替换为 Windows 首选的分隔符(\):

path.win32.normalize('C:////temp\\\\/\\/\\/foo/bar');
// 返回: 'C:\\temp\\foo\\bar'

如果 path 不是字符串,则抛出 TypeError

The path.normalize() method normalizes the given path, resolving '..' and '.' segments.

When multiple, sequential path segment separation characters are found (e.g. / on POSIX and either \ or / on Windows), they are replaced by a single instance of the platform-specific path segment separator (/ on POSIX and \ on Windows). Trailing separators are preserved.

If the path is a zero-length string, '.' is returned, representing the current working directory.

For example, on POSIX:

path.normalize('/foo/bar//baz/asdf/quux/..');
// Returns: '/foo/bar/baz/asdf'

On Windows:

path.normalize('C:\\temp\\\\foo\\bar\\..\\');
// Returns: 'C:\\temp\\foo\\'

Since Windows recognizes multiple path separators, both separators will be replaced by instances of the Windows preferred separator (\):

path.win32.normalize('C:////temp\\\\/\\/\\/foo/bar');
// Returns: 'C:\\temp\\foo\\bar'

A TypeError is thrown if path is not a string.