Path. normalize method in node. js instructions for use _ node. js

Source: Internet
Author: User
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;
};

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.