(from: string, to: string)
| 521 | // to = 'C:\\orandea\\impl\\bbb' |
| 522 | // The output of the function should be: '..\\..\\impl\\bbb' |
| 523 | relative(from: string, to: string): string { |
| 524 | validateString(from, 'from'); |
| 525 | validateString(to, 'to'); |
| 526 | |
| 527 | if (from === to) { |
| 528 | return ''; |
| 529 | } |
| 530 | |
| 531 | const fromOrig = win32.resolve(from); |
| 532 | const toOrig = win32.resolve(to); |
| 533 | |
| 534 | if (fromOrig === toOrig) { |
| 535 | return ''; |
| 536 | } |
| 537 | |
| 538 | from = fromOrig.toLowerCase(); |
| 539 | to = toOrig.toLowerCase(); |
| 540 | |
| 541 | if (from === to) { |
| 542 | return ''; |
| 543 | } |
| 544 | |
| 545 | // Trim any leading backslashes |
| 546 | let fromStart = 0; |
| 547 | while (fromStart < from.length && from.charCodeAt(fromStart) === CHAR_BACKWARD_SLASH) { |
| 548 | fromStart++; |
| 549 | } |
| 550 | // Trim trailing backslashes (applicable to UNC paths only) |
| 551 | let fromEnd = from.length; |
| 552 | while (fromEnd - 1 > fromStart && from.charCodeAt(fromEnd - 1) === CHAR_BACKWARD_SLASH) { |
| 553 | fromEnd--; |
| 554 | } |
| 555 | const fromLen = fromEnd - fromStart; |
| 556 | |
| 557 | // Trim any leading backslashes |
| 558 | let toStart = 0; |
| 559 | while (toStart < to.length && to.charCodeAt(toStart) === CHAR_BACKWARD_SLASH) { |
| 560 | toStart++; |
| 561 | } |
| 562 | // Trim trailing backslashes (applicable to UNC paths only) |
| 563 | let toEnd = to.length; |
| 564 | while (toEnd - 1 > toStart && to.charCodeAt(toEnd - 1) === CHAR_BACKWARD_SLASH) { |
| 565 | toEnd--; |
| 566 | } |
| 567 | const toLen = toEnd - toStart; |
| 568 | |
| 569 | // Compare paths to find the longest common path from root |
| 570 | const length = fromLen < toLen ? fromLen : toLen; |
| 571 | let lastCommonSep = -1; |
| 572 | let i = 0; |
| 573 | for (; i < length; i++) { |
| 574 | const fromCode = from.charCodeAt(fromStart + i); |
| 575 | if (fromCode !== to.charCodeAt(toStart + i)) { |
| 576 | break; |
| 577 | } else if (fromCode === CHAR_BACKWARD_SLASH) { |
| 578 | lastCommonSep = i; |
| 579 | } |
| 580 | } |
no test coverage detected