* Extract unique top-level path segments, sorted by (length asc, then alpha asc). * Handles both Unix (/) and Windows (\) path separators. * Mirrors FileIndex::compute_top_level_entries in lib.rs.
( paths: string[], limit: number, )
| 334 | * Mirrors FileIndex::compute_top_level_entries in lib.rs. |
| 335 | */ |
| 336 | function computeTopLevelEntries( |
| 337 | paths: string[], |
| 338 | limit: number, |
| 339 | ): SearchResult[] { |
| 340 | const topLevel = new Set<string>() |
| 341 | |
| 342 | for (const p of paths) { |
| 343 | // Split on first / or \ separator |
| 344 | let end = p.length |
| 345 | for (let i = 0; i < p.length; i++) { |
| 346 | const c = p.charCodeAt(i) |
| 347 | if (c === 47 || c === 92) { |
| 348 | end = i |
| 349 | break |
| 350 | } |
| 351 | } |
| 352 | const segment = p.slice(0, end) |
| 353 | if (segment.length > 0) { |
| 354 | topLevel.add(segment) |
| 355 | if (topLevel.size >= limit) break |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | const sorted = Array.from(topLevel) |
| 360 | sorted.sort((a, b) => { |
| 361 | const lenDiff = a.length - b.length |
| 362 | if (lenDiff !== 0) return lenDiff |
| 363 | return a < b ? -1 : a > b ? 1 : 0 |
| 364 | }) |
| 365 | |
| 366 | return sorted.slice(0, limit).map(path => ({ path, score: 0.0 })) |
| 367 | } |
| 368 | |
| 369 | export default FileIndex |
| 370 | export type { FileIndex as FileIndexType } |