| 1 |
'use strict'; |
| 2 |
|
| 3 |
var dirname = require('./dirname') |
| 4 |
, resolve = require('./resolve') |
| 5 |
|
| 6 |
, normalize; |
| 7 |
|
| 8 |
normalize = function (path) { |
| 9 |
if (typeof path !== 'string') { |
| 10 |
throw new TypeError('Arguments to path.common must be strings'); |
| 11 |
} |
| 12 |
var isDir = (path[path.length - 1] === '\\'); |
| 13 |
path = resolve(path); |
| 14 |
if (isDir) return path; |
| 15 |
return dirname(path); |
| 16 |
}; |
| 17 |
|
| 18 |
module.exports = function (path/*, ...pathn*/) { |
| 19 |
var i, l, pathLength, other, end, j, otherLength; |
| 20 |
|
| 21 |
path = normalize(path); |
| 22 |
l = arguments.length; |
| 23 |
pathLength = path.length; |
| 24 |
for (i = 1; i < l; ++i) { |
| 25 |
other = normalize(arguments[i]).toLowerCase(); |
| 26 |
end = 0; |
| 27 |
j = 0; |
| 28 |
otherLength = other.length; |
| 29 |
while (path[j].toLowerCase() === other[j]) { |
| 30 |
if (path[j] === '\\') end = j; |
| 31 |
++j; |
| 32 |
if (pathLength === j) { |
| 33 |
if (otherLength === j) end = j; |
| 34 |
else if (other[j] === '\\') end = j; |
| 35 |
break; |
| 36 |
} |
| 37 |
if (otherLength === j) { |
| 38 |
if (path[j] === '\\') end = j; |
| 39 |
break; |
| 40 |
} |
| 41 |
} |
| 42 |
if (!end) return null; |
| 43 |
if (end !== pathLength) { |
| 44 |
path = path.slice(0, end); |
| 45 |
if (path[path.length - 1] === ':') { |
| 46 |
path += '\\'; |
| 47 |
++end; |
| 48 |
} |
| 49 |
pathLength = end; |
| 50 |
} |
| 51 |
} |
| 52 |
return path; |
| 53 |
}; |