The file whose path shares the longest directory prefix with `from`.
(files: string[], from: string)
| 2599 | |
| 2600 | /** The file whose path shares the longest directory prefix with `from`. */ |
| 2601 | function nearest(files: string[], from: string): string | null { |
| 2602 | if (files.length <= 1) return files[0] ?? null; |
| 2603 | const dirs = from.split('/'); |
| 2604 | let best: string | null = null; |
| 2605 | let bestScore = -1; |
| 2606 | for (const f of files) { |
| 2607 | const parts = f.split('/'); |
| 2608 | let score = 0; |
| 2609 | while (score < parts.length && score < dirs.length && parts[score] === dirs[score]) score++; |
| 2610 | if (score > bestScore) { |
| 2611 | best = f; |
| 2612 | bestScore = score; |
| 2613 | } |
| 2614 | } |
| 2615 | return best; |
| 2616 | } |
| 2617 | |
| 2618 | // --------------------------------------------------------------------------- |
| 2619 | // Declared types |
no outgoing calls
no test coverage detected