|
|
@ -165,6 +165,64 @@ if (isWindows) { |
|
|
|
return exports.normalize(joined); |
|
|
|
}; |
|
|
|
|
|
|
|
// path.relative(from, to)
|
|
|
|
// it will solve the relative path from 'from' to 'to', for instance:
|
|
|
|
// from = 'C:\\orandea\\test\\aaa'
|
|
|
|
// to = 'C:\\orandea\\impl\\bbb'
|
|
|
|
// The output of the function should be: '..\\..\\impl\\bbb'
|
|
|
|
// windows version
|
|
|
|
exports.relative = function(from, to) { |
|
|
|
from = exports.resolve(from); |
|
|
|
to = exports.resolve(to); |
|
|
|
|
|
|
|
// windows is not case sensitive
|
|
|
|
var lowerFrom = from.toLowerCase(); |
|
|
|
var lowerTo = to.toLowerCase(); |
|
|
|
|
|
|
|
function trim(arr) { |
|
|
|
var start = 0; |
|
|
|
for(;start < arr.length; start++) { |
|
|
|
if (arr[start] !== '') break; |
|
|
|
} |
|
|
|
|
|
|
|
var end = arr.length - 1; |
|
|
|
for(;end >= 0; end--) { |
|
|
|
if (arr[end] !== '') break; |
|
|
|
} |
|
|
|
|
|
|
|
if (start > end) return []; |
|
|
|
return arr.slice(start, end - start + 1); |
|
|
|
} |
|
|
|
|
|
|
|
var fromParts = trim(from.split('\\')); |
|
|
|
var toParts = trim(to.split('\\')); |
|
|
|
|
|
|
|
var lowerFromParts = trim(lowerFrom.split('\\')); |
|
|
|
var lowerToParts = trim(lowerTo.split('\\')); |
|
|
|
|
|
|
|
var length = Math.min(lowerFromParts.length, lowerToParts.length); |
|
|
|
var samePartsLength = length; |
|
|
|
for(var i = 0; i < length; i++) { |
|
|
|
if (lowerFromParts[i] !== lowerToParts[i]) { |
|
|
|
samePartsLength = i; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (samePartsLength == 0) { |
|
|
|
return to; |
|
|
|
} |
|
|
|
|
|
|
|
var outputParts = []; |
|
|
|
for(var i = samePartsLength; i < lowerFromParts.length; i++) { |
|
|
|
outputParts.push('..'); |
|
|
|
} |
|
|
|
|
|
|
|
outputParts = outputParts.concat(toParts.slice(samePartsLength)); |
|
|
|
|
|
|
|
return outputParts.join('\\'); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
} else /* posix */ { |
|
|
|
|
|
|
@ -232,6 +290,51 @@ if (isWindows) { |
|
|
|
return p && typeof p === 'string'; |
|
|
|
}).join('/')); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// path.relative(from, to)
|
|
|
|
// posix version
|
|
|
|
exports.relative = function(from, to) { |
|
|
|
from = exports.resolve(from).substr(1); |
|
|
|
to = exports.resolve(to).substr(1); |
|
|
|
|
|
|
|
function trim(arr) { |
|
|
|
var start = 0; |
|
|
|
for(;start < arr.length; start++) { |
|
|
|
if (arr[start] !== '') break; |
|
|
|
} |
|
|
|
|
|
|
|
var end = arr.length - 1; |
|
|
|
for(;end >= 0; end--) { |
|
|
|
if (arr[end] !== '') break; |
|
|
|
} |
|
|
|
|
|
|
|
if (start > end) return []; |
|
|
|
return arr.slice(start, end - start + 1); |
|
|
|
} |
|
|
|
|
|
|
|
var fromParts = trim(from.split('/')); |
|
|
|
var toParts = trim(to.split('/')); |
|
|
|
|
|
|
|
var length = Math.min(fromParts.length, toParts.length); |
|
|
|
var samePartsLength = length; |
|
|
|
for(var i = 0; i < length; i++) { |
|
|
|
if (fromParts[i] !== toParts[i]) { |
|
|
|
samePartsLength = i; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
var outputParts = []; |
|
|
|
for(var i = samePartsLength; i < fromParts.length; i++) { |
|
|
|
outputParts.push('..'); |
|
|
|
} |
|
|
|
|
|
|
|
outputParts = outputParts.concat(toParts.slice(samePartsLength)); |
|
|
|
|
|
|
|
return outputParts.join('/'); |
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|