microsoft/qdk
Publicmirrored fromhttps://github.com/microsoft/qdkAvailable
source/npm/qsharp/src/katas.ts
93lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | import { default as katasContent } from "./katas-content.generated.js"; |
| 5 | |
| 6 | export type Example = { |
| 7 | type: "example"; |
| 8 | id: string; |
| 9 | code: string; |
| 10 | }; |
| 11 | |
| 12 | export type TextContent = { |
| 13 | type: "text-content"; |
| 14 | content: string; |
| 15 | }; |
| 16 | |
| 17 | export type ContentItem = Example | TextContent; |
| 18 | |
| 19 | export type Solution = { |
| 20 | type: "solution"; |
| 21 | id: string; |
| 22 | code: string; |
| 23 | }; |
| 24 | |
| 25 | export type ExplainedSolutionItem = ContentItem | Solution; |
| 26 | |
| 27 | export type ExplainedSolution = { |
| 28 | type: "explained-solution"; |
| 29 | items: ExplainedSolutionItem[]; |
| 30 | }; |
| 31 | |
| 32 | export type Exercise = { |
| 33 | type: "exercise"; |
| 34 | id: string; |
| 35 | title: string; |
| 36 | description: TextContent; |
| 37 | sourceIds: string[]; |
| 38 | placeholderCode: string; |
| 39 | explainedSolution: ExplainedSolution; |
| 40 | }; |
| 41 | |
| 42 | export type Answer = { |
| 43 | type: "answer"; |
| 44 | items: ContentItem[]; |
| 45 | }; |
| 46 | |
| 47 | export type Question = { |
| 48 | type: "question"; |
| 49 | description: TextContent; |
| 50 | answer: Answer; |
| 51 | }; |
| 52 | |
| 53 | export type LessonItem = ContentItem | Question; |
| 54 | |
| 55 | export type Lesson = { |
| 56 | type: "lesson"; |
| 57 | id: string; |
| 58 | title: string; |
| 59 | items: LessonItem[]; |
| 60 | }; |
| 61 | |
| 62 | export type KataSection = Exercise | Lesson; |
| 63 | |
| 64 | export type Kata = { |
| 65 | id: string; |
| 66 | title: string; |
| 67 | sections: KataSection[]; |
| 68 | published: boolean; |
| 69 | }; |
| 70 | |
| 71 | export async function getAllKatas( |
| 72 | options: { includeUnpublished?: boolean } = { includeUnpublished: false }, |
| 73 | ): Promise<Kata[]> { |
| 74 | return katasContent.katas.filter( |
| 75 | (k) => options.includeUnpublished || k.published, |
| 76 | ) as Kata[]; |
| 77 | } |
| 78 | |
| 79 | export async function getKata(id: string): Promise<Kata> { |
| 80 | const katas = await getAllKatas({ includeUnpublished: true }); |
| 81 | return ( |
| 82 | katas.find((k) => k.id === id) || |
| 83 | Promise.reject(`Failed to get kata with id: ${id}`) |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | export async function getExerciseSources( |
| 88 | exercise: Exercise, |
| 89 | ): Promise<string[]> { |
| 90 | return katasContent.globalCodeSources |
| 91 | .filter((source) => exercise.sourceIds.indexOf(source.id) > -1) |
| 92 | .map((source) => source.code); |
| 93 | } |
| 94 | |