( contents: FullCourseContent[], ids: number[], )
| 1 | import { ChildCourseContent, FullCourseContent } from '@/db/course'; |
| 2 | |
| 3 | export default function findContentById( |
| 4 | contents: FullCourseContent[], |
| 5 | ids: number[], |
| 6 | ): |
| 7 | | { |
| 8 | folder: true; |
| 9 | value: ChildCourseContent[]; |
| 10 | } |
| 11 | | { |
| 12 | folder: false; |
| 13 | value: ChildCourseContent; |
| 14 | } |
| 15 | | null { |
| 16 | if (ids.length === 0) return { folder: true, value: contents }; |
| 17 | |
| 18 | const currentId = ids[0]; |
| 19 | const remainingIds = ids.slice(1); |
| 20 | |
| 21 | const foundContent = contents.find((content) => content.id === currentId); |
| 22 | |
| 23 | if (!foundContent) { |
| 24 | return null; |
| 25 | } else if (remainingIds.length === 0) { |
| 26 | if (foundContent.type === 'folder') { |
| 27 | return { folder: true, value: foundContent.children ?? [] }; // [Object] |
| 28 | } |
| 29 | |
| 30 | return { folder: false, value: foundContent }; |
| 31 | } |
| 32 | |
| 33 | return findContentById(foundContent?.children || [], remainingIds); |
| 34 | } |
no outgoing calls
no test coverage detected