(superblocks: T[], { challengeId }: { challengeId?: string } = {})
| 55 | * @returns {Array<Object>} Filtered superblocks containing only the matching challenge |
| 56 | */ |
| 57 | export function filterByChallengeId< |
| 58 | T extends { blocks: { challengeOrder: { id: string }[] }[] } |
| 59 | >(superblocks: T[], { challengeId }: { challengeId?: string } = {}): T[] { |
| 60 | if (!challengeId) { |
| 61 | return superblocks; |
| 62 | } |
| 63 | |
| 64 | const findChallengeIndex = (challengeOrder: { id: string }[], id: string) => |
| 65 | challengeOrder.findIndex(challenge => challenge.id === id); |
| 66 | |
| 67 | const filterChallengeOrder = ( |
| 68 | challengeOrder: { id: string }[], |
| 69 | id: string |
| 70 | ) => { |
| 71 | const index = findChallengeIndex(challengeOrder, id); |
| 72 | if (index === -1) return []; |
| 73 | |
| 74 | return challengeOrder.slice(index, index + 2); |
| 75 | }; |
| 76 | |
| 77 | return superblocks |
| 78 | .map(superblock => ({ |
| 79 | ...superblock, |
| 80 | blocks: superblock.blocks |
| 81 | .map(block => ({ |
| 82 | ...block, |
| 83 | challengeOrder: filterChallengeOrder( |
| 84 | block.challengeOrder, |
| 85 | challengeId |
| 86 | ) |
| 87 | })) |
| 88 | .filter( |
| 89 | block => block.challengeOrder && block.challengeOrder.length > 0 |
| 90 | ) |
| 91 | })) |
| 92 | .filter(superblock => superblock.blocks.length > 0); |
| 93 | } |
| 94 | |
| 95 | export interface Filter { |
| 96 | superBlock?: string; |
no test coverage detected