MCPcopy Create free account
hub / github.com/Effect-TS/effect / relative

Function relative

packages/effect/src/Path.ts:521–611  ·  view source on GitHub ↗
(from, to)

Source from the content-addressed store, hash-verified

519 },
520
521 relative(from, to) {
522 if (from === to) return ""
523
524 from = posixImpl.resolve(from)
525 to = posixImpl.resolve(to)
526
527 if (from === to) return ""
528
529 // Trim any leading backslashes
530 let fromStart = 1
531 for (; fromStart < from.length; ++fromStart) {
532 if (from.charCodeAt(fromStart) !== 47 /*/*/) {
533 break
534 }
535 }
536 const fromEnd = from.length
537 const fromLen = fromEnd - fromStart
538
539 // Trim any leading backslashes
540 let toStart = 1
541 for (; toStart < to.length; ++toStart) {
542 if (to.charCodeAt(toStart) !== 47 /*/*/) {
543 break
544 }
545 }
546 const toEnd = to.length
547 const toLen = toEnd - toStart
548
549 // Compare paths to find the longest common path from root
550 const length = fromLen < toLen ? fromLen : toLen
551 let lastCommonSep = -1
552 let i = 0
553 for (; i <= length; ++i) {
554 if (i === length) {
555 if (toLen > length) {
556 if (to.charCodeAt(toStart + i) === 47 /*/*/) {
557 // We get here if `from` is the exact base path for `to`.
558 // For example: from='/foo/bar'; to='/foo/bar/baz'
559 return to.slice(toStart + i + 1)
560 } else if (i === 0) {
561 // We get here if `from` is the root
562 // For example: from='/'; to='/foo'
563 return to.slice(toStart + i)
564 }
565 } else if (fromLen > length) {
566 if (from.charCodeAt(fromStart + i) === 47 /*/*/) {
567 // We get here if `to` is the exact base path for `from`.
568 // For example: from='/foo/bar/baz'; to='/foo/bar'
569 lastCommonSep = i
570 } else if (i === 0) {
571 // We get here if `to` is the root.
572 // For example: from='/foo'; to='/'
573 lastCommonSep = 0
574 }
575 }
576 break
577 }
578 const fromCode = from.charCodeAt(fromStart + i)

Callers 3

readDirectoryFunction · 0.85
run.mjsFile · 0.85
mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected