(payload: unknown)
| 214 | |
| 215 | /** Eine Antwort von `/api/tree` lesen: falten, aussortieren, sortieren. */ |
| 216 | export function readTreeLevel(payload: unknown): TreeLevel { |
| 217 | const raw = isRecord(payload) ? payload : {}; |
| 218 | const rawChildren = Array.isArray(raw['children']) ? raw['children'] : []; |
| 219 | |
| 220 | const byPath = new Map<string, TreeChild>(); |
| 221 | let foldedDuplicates = 0; |
| 222 | let droppedNonPaths = 0; |
| 223 | |
| 224 | for (const entry of rawChildren) { |
| 225 | if (!isRecord(entry)) { |
| 226 | continue; |
| 227 | } |
| 228 | const name = typeof entry['name'] === 'string' ? entry['name'] : ''; |
| 229 | const path = typeof entry['path'] === 'string' ? entry['path'] : ''; |
| 230 | if (!namesAPath({ name, path })) { |
| 231 | droppedNonPaths += 1; |
| 232 | continue; |
| 233 | } |
| 234 | const child: TreeChild = { |
| 235 | name, |
| 236 | path, |
| 237 | kind: entry['kind'] === 'dir' ? 'dir' : 'file', |
| 238 | symbols: asNumber(entry['symbols']), |
| 239 | files: asNumber(entry['files']), |
| 240 | }; |
| 241 | const missed = asNumber(entry['missed']); |
| 242 | if (missed > 0) { |
| 243 | child.missed = missed; |
| 244 | } |
| 245 | |
| 246 | const existing = byPath.get(path); |
| 247 | if (existing === undefined) { |
| 248 | byPath.set(path, child); |
| 249 | continue; |
| 250 | } |
| 251 | // Derselbe Pfad zweimal: der Ordner ist die Wahrheit, die Zahlen des |
| 252 | // Ordners auch. Der Folder-Knoten steuert nur sich selbst bei. |
| 253 | foldedDuplicates += 1; |
| 254 | if (existing.kind === 'file' && child.kind === 'dir') { |
| 255 | byPath.set(path, child); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | return { |
| 260 | path: typeof raw['path'] === 'string' ? raw['path'] : '', |
| 261 | files: asNumber(raw['files']), |
| 262 | symbols: asNumber(raw['symbols']), |
| 263 | children: [...byPath.values()].sort(compareChildren), |
| 264 | foldedDuplicates, |
| 265 | droppedNonPaths, |
| 266 | }; |
| 267 | } |
| 268 | |
| 269 | /** Der Pfad des Ordners ueber diesem Pfad. Leer heisst Projektwurzel. */ |
| 270 | export function parentPath(path: string): string { |
no test coverage detected