blob: 4ed24c31a4389d758102e00408f555bedf3486a7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
module.exports = relativize
// return the shortest path between two folders.
function relativize (dest, src) {
// both of these are absolute paths.
// find the shortest relative distance.
src = src.split("/")
var abs = dest
dest = dest.split("/")
var i = 0
while (src[i] === dest[i]) i++
if (i === 1) return abs // nothing in common, leave absolute
src.splice(0, i + 1)
var dots = [0, i, "."]
for (var i = 0, l = src.length; i < l; i ++) dots.push("..")
dest.splice.apply(dest, dots)
dest = dest.join("/")
return abs.length < dest.length ? abs : dest
}
|