Skip to content

Commit

Permalink
refactor: Introduce helpers and simplify iterations.
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuel committed Aug 22, 2024
1 parent 16385f1 commit c215b77
Showing 1 changed file with 30 additions and 39 deletions.
69 changes: 30 additions & 39 deletions src/AbstractCmi5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ function _toResultScore(s?: ResultScore | number): ResultScore | undefined {
: (s as ResultScore);
}

function isNumericExact(candidate: unknown): candidate is NumericExact {
return ("exact" in candidate);
}

function isNumericRange(candidate: unknown): candidate is NumericRange {
return (("min" in candidate) && ("max" in candidate));
}

function numericCriteriaToString(criteria: NumericExact | NumericRange | unknown) {
if (isNumericExact(criteria)) {
return String(criteria.exact);
} else if (isNumericRange(criteria)) {
const { min, max } = criteria;
return `${min}:${max}`;
} else {
return `:`;
}
}

/**
* Experience API cmi5 Profile (Quartz - 1st Edition)
* Reference: https://github.com/AICC/CMI-5_Spec_Current/blob/quartz/cmi5_spec.md
Expand Down Expand Up @@ -514,21 +533,15 @@ export default class AbstractCmi5 {
return this.interaction(
testId,
questionId,
Object.keys(answers)
.map((key) => {
return `${key}[.]${answers[key]}`;
})
.join("[,]"),
Object.entries(answers).map(([k, v]) => `${k}[.]${v}`).join("[,]"),
{
type: "http://adlnet.gov/expapi/activities/cmi.interaction",
interactionType: "matching",
...(correctAnswers
? {
correctResponsesPattern: [
Object.keys(correctAnswers)
.map((key) => {
return `${key}[.]${correctAnswers[key]}`;
})
Object.entries(correctAnswers)
.map(([key, val]) => `${key}[.]${val}`)
.join("[,]"),
],
}
Expand Down Expand Up @@ -559,30 +572,17 @@ export default class AbstractCmi5 {
return this.interaction(
testId,
questionId,
Object.keys(answers)
.map((key) => {
return `${key}[.]${answers[key]}`;
})
Object.entries(answers)
.map(([k, v]) => `${k}[.]${v}`)
.join("[,]"),
{
type: "http://adlnet.gov/expapi/activities/cmi.interaction",
interactionType: "performance",
...(correctAnswers
? {
correctResponsesPattern: [
Object.keys(correctAnswers)
.map((key) => {
const exact: string = correctAnswers[key].exact
? correctAnswers[key].exact.toString()
: "";
const min: string = correctAnswers[key].min
? correctAnswers[key].min.toString()
: "";
const max: number = correctAnswers[key].max
? correctAnswers[key].max.toString()
: "";
return `${key}[.]${exact ? exact : min + ":" + max}`;
})
Object.entries(correctAnswers)
.map(([k, v]) => `${k}[.]${numericCriteriaToString(v)}`)
.join("[,]"),
],
}
Expand Down Expand Up @@ -642,26 +642,17 @@ export default class AbstractCmi5 {
duration?: Period,
objective?: ObjectiveActivity
): AxiosPromise<string[]> {
const correctAnswerObj = correctAnswer
? { correctResponsesPattern: [numericCriteriaToString(correctAnswer)] }
: {}
return this.interaction(
testId,
questionId,
answer.toString(),
{
type: "http://adlnet.gov/expapi/activities/cmi.interaction",
interactionType: "numeric",
...(correctAnswer
? {
correctResponsesPattern: [
`${
(correctAnswer as NumericExact).exact
? (correctAnswer as NumericExact).exact
: (correctAnswer as NumericRange).min +
":" +
(correctAnswer as NumericRange).max
}`,
],
}
: {}),
...correctAnswerObj,
...(name ? { name } : {}),
...(description ? { description } : {}),
},
Expand Down

0 comments on commit c215b77

Please sign in to comment.