(from: string, to: string)
| 221 | }; |
| 222 | |
| 223 | const relative = function relative(from: string, to: string) { |
| 224 | assertPath(from); |
| 225 | assertPath(to); |
| 226 | |
| 227 | if (from === to) { |
| 228 | return ''; |
| 229 | } |
| 230 | |
| 231 | from = resolve(from); |
| 232 | to = resolve(to); |
| 233 | |
| 234 | if (from === to) { |
| 235 | return ''; |
| 236 | } |
| 237 | |
| 238 | // Trim any leading backslashes |
| 239 | let fromStart = 1; |
| 240 | for (; fromStart < from.length; ++fromStart) { |
| 241 | if (from.charCodeAt(fromStart) !== 47 /*/*/) { |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | const fromEnd = from.length; |
| 246 | const fromLen = fromEnd - fromStart; |
| 247 | |
| 248 | // Trim any leading backslashes |
| 249 | let toStart = 1; |
| 250 | for (; toStart < to.length; ++toStart) { |
| 251 | if (to.charCodeAt(toStart) !== 47 /*/*/) { |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | const toEnd = to.length; |
| 256 | const toLen = toEnd - toStart; |
| 257 | |
| 258 | // Compare paths to find the longest common path from root |
| 259 | const length = fromLen < toLen ? fromLen : toLen; |
| 260 | let lastCommonSep = -1; |
| 261 | let i = 0; |
| 262 | for (; i <= length; ++i) { |
| 263 | if (i === length) { |
| 264 | if (toLen > length) { |
| 265 | if (to.charCodeAt(toStart + i) === 47 /*/*/) { |
| 266 | // We get here if `from` is the exact base path for `to`. |
| 267 | // For example: from='/foo/bar'; to='/foo/bar/baz' |
| 268 | return to.slice(toStart + i + 1); |
| 269 | } else if (i === 0) { |
| 270 | // We get here if `from` is the root |
| 271 | // For example: from='/'; to='/foo' |
| 272 | return to.slice(toStart + i); |
| 273 | } |
| 274 | } else if (fromLen > length) { |
| 275 | if (from.charCodeAt(fromStart + i) === 47 /*/*/) { |
| 276 | // We get here if `to` is the exact base path for `from`. |
| 277 | // For example: from='/foo/bar/baz'; to='/foo/bar' |
| 278 | lastCommonSep = i; |
| 279 | } else if (i === 0) { |
| 280 | // We get here if `to` is the root. |
no test coverage detected
searching dependent graphs…