UNPKG

ph-dev-tools

Version:
61 lines (52 loc) 1.49 kB
import {EntityCandidate} from "../parser/EntityCandidate"; /** * Created by Papa on 4/27/2016. */ export function resolveRelativeEntityPath( from:EntityCandidate, // to:EntityCandidate // ):string { return resolveRelativePath(from.path, to.path); } export function resolveRelativePath( fromPath:string, // toPath:string // ):string { fromPath = normalizePath(fromPath); toPath = normalizePath(toPath); let fromFragments = fromPath.split('/'); let toFragments = toPath.split('/'); let numCommonFragments = 0; for(let i = 0; i < fromFragments.length; i++) { let fromFragment = fromFragments[i]; let toFragment = toFragments[i]; if(fromFragment !== toFragment) { break; } numCommonFragments = i + 1; } let numFromPathDiffDirectories = fromFragments.length - numCommonFragments - 1; let toPathDiffNodes = toFragments.slice(numCommonFragments); let relativePath = ''; if(numFromPathDiffDirectories == 0) { relativePath = './'; } else { for(let i = 0; i < numFromPathDiffDirectories; i++) { relativePath += '../'; } } for(let i = 0; i < toPathDiffNodes.length; i++) { relativePath += toPathDiffNodes[i]; if(i < toPathDiffNodes.length - 1) { relativePath += '/'; } } return relativePath; } export function normalizePath( path:string ):string { let forwardSlashedPath = path.replace(/\\/g, '/'); let lowercasePath = forwardSlashedPath.toLowerCase(); return lowercasePath; }