Debian Codegraph @

path2/windows/normalize.js

raw
1 'use strict';
2
3 var isAbsolute = require('./is-absolute')
4 , normalizeUNCRoot = require('./_normalize-unc-root')
5 , splitDeviceRe = require('./_split-device-re')
6 , normalizeArray = require('../_normalize-array');
7
8 module.exports = function (path) {
9 var result = splitDeviceRe.exec(path), device = result[1] || ''
10 , isUnc = device && device.charAt(1) !== ':', isAbs = isAbsolute(path)
11 , tail = result[3], trailingSlash = /[\\\/]$/.test(tail);
12
13 // If device is a drive letter, we'll normalize to lower case.
14 if (device && device.charAt(1) === ':') {
15 device = device[0].toLowerCase() + device.substr(1);
16 }
17
18 // Normalize the tail path
19 tail = normalizeArray(tail.split(/[\\\/]+/).filter(function (p) {
20 return !!p;
21 }), !isAbs).join('\\');
22
23 if (!tail && !isAbs) {
24 tail = '.';
25 }
26 if (tail && trailingSlash) {
27 tail += '\\';
28 }
29
30 // Convert slashes to backslashes when `device` points to an UNC root.
31 // Also squash multiple slashes into a single one where appropriate.
32 if (isUnc) {
33 device = normalizeUNCRoot(device);
34 }
35
36 return device + (isAbs ? '\\' : '') + tail;
37 };