( from: string, to: string, stripExtension: boolean = false, )
| 31 | } |
| 32 | |
| 33 | export function relativePath( |
| 34 | from: string, |
| 35 | to: string, |
| 36 | stripExtension: boolean = false, |
| 37 | ) { |
| 38 | const normalized = from.replace(/\\/g, '/') |
| 39 | const cleanedFrom = normalized.startsWith('./') |
| 40 | ? normalized.slice(2) |
| 41 | : normalized |
| 42 | const cleanedTo = to.startsWith('./') ? to.slice(2) : to |
| 43 | |
| 44 | const fromSegments = cleanedFrom.split('/') |
| 45 | const toSegments = cleanedTo.split('/') |
| 46 | |
| 47 | fromSegments.pop() |
| 48 | toSegments.pop() |
| 49 | |
| 50 | let commonIndex = 0 |
| 51 | while ( |
| 52 | commonIndex < fromSegments.length && |
| 53 | commonIndex < toSegments.length && |
| 54 | fromSegments[commonIndex] === toSegments[commonIndex] |
| 55 | ) { |
| 56 | commonIndex++ |
| 57 | } |
| 58 | |
| 59 | const upLevels = fromSegments.length - commonIndex |
| 60 | const downLevels = toSegments.slice(commonIndex) |
| 61 | const target = stripExtension ? to.replace(extnamePath(to), '') : to |
| 62 | |
| 63 | if (upLevels === 0 && downLevels.length === 0) { |
| 64 | return `./${basenamePath(target)}` |
| 65 | } else if (upLevels === 0 && downLevels.length > 0) { |
| 66 | return `./${downLevels.join('/')}/${basenamePath(target)}` |
| 67 | } else { |
| 68 | const relative = [...Array(upLevels).fill('..'), ...downLevels].join('/') |
| 69 | return `${relative}/${basenamePath(target)}` |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | export function isDemoFilePath(path?: string): boolean { |
| 74 | if (!path) return false |
nothing calls this directly
no test coverage detected