The file whose path shares the longest directory prefix with `from`.
(files: string[], from: string)
| 1063 | |
| 1064 | /** The file whose path shares the longest directory prefix with `from`. */ |
| 1065 | function nearest(files: string[], from: string): string | null { |
| 1066 | if (files.length <= 1) return files[0] ?? null; |
| 1067 | const dirs = from.split('/'); |
| 1068 | let best: string | null = null; |
| 1069 | let bestScore = -1; |
| 1070 | for (const f of files) { |
| 1071 | const parts = f.split('/'); |
| 1072 | let score = 0; |
| 1073 | while (score < parts.length && score < dirs.length && parts[score] === dirs[score]) score++; |
| 1074 | if (score > bestScore) { |
| 1075 | best = f; |
| 1076 | bestScore = score; |
| 1077 | } |
| 1078 | } |
| 1079 | return best; |
| 1080 | } |
| 1081 | |
| 1082 | // --------------------------------------------------------------------------- |
| 1083 | // Declared types |
no outgoing calls
no test coverage detected