MCPcopy Index your code
hub / github.com/nodejs/node / relative

Function relative

lib/path.js:610–762  ·  view source on GitHub ↗

* 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' * @param {string} from * @param {string} to * @returns {string}

(from, to)

Source from the content-addressed store, hash-verified

608 * @returns {string}
609 */
610 relative(from, to) {
611 validateString(from, 'from');
612 validateString(to, 'to');
613
614 if (from === to)
615 return '';
616
617 const fromOrig = win32.resolve(from);
618 const toOrig = win32.resolve(to);
619
620 if (fromOrig === toOrig)
621 return '';
622
623 from = StringPrototypeToLowerCase(fromOrig);
624 to = StringPrototypeToLowerCase(toOrig);
625
626 if (from === to)
627 return '';
628
629 if (fromOrig.length !== from.length || toOrig.length !== to.length) {
630 const fromSplit = StringPrototypeSplit(fromOrig, '\\');
631 const toSplit = StringPrototypeSplit(toOrig, '\\');
632 if (fromSplit[fromSplit.length - 1] === '') {
633 fromSplit.pop();
634 }
635 if (toSplit[toSplit.length - 1] === '') {
636 toSplit.pop();
637 }
638
639 const fromLen = fromSplit.length;
640 const toLen = toSplit.length;
641 const length = fromLen < toLen ? fromLen : toLen;
642
643 let i;
644 for (i = 0; i < length; i++) {
645 if (StringPrototypeToLowerCase(fromSplit[i]) !== StringPrototypeToLowerCase(toSplit[i])) {
646 break;
647 }
648 }
649
650 if (i === 0) {
651 return toOrig;
652 } else if (i === length) {
653 if (toLen > length) {
654 return ArrayPrototypeJoin(ArrayPrototypeSlice(toSplit, i), '\\');
655 }
656 if (fromLen > length) {
657 return StringPrototypeRepeat('..\\', fromLen - 1 - i) + '..';
658 }
659 return '';
660 }
661
662 return StringPrototypeRepeat('..\\', fromLen - i) + ArrayPrototypeJoin(ArrayPrototypeSlice(toSplit, i), '\\');
663 }
664
665 // Trim any leading backslashes
666 let fromStart = 0;
667 while (fromStart < from.length &&

Callers 15

checkFilesFunction · 0.85
constructorMethod · 0.85
buildFileTreeFunction · 0.85
_transformMethod · 0.85
getTestIdFunction · 0.85
getRelativePathFunction · 0.85

Calls 2

popMethod · 0.80
resolveMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…