@trivia-api/models
Version:
Models for The Trivia API.
29 lines (28 loc) • 1.1 kB
JavaScript
import { isQuestionCategory } from "../../index";
/**
* Type guard to check that argument is assignable to PublicQuestion
* @param arg
* @returns
*/
export const isQuestion = (arg) => {
if (typeof arg === "object" && arg !== null) {
if (["category", "correctAnswer"].every((key) => Object.keys(arg).includes(key))) {
if (isQuestionCategory(arg.category)) {
if (typeof arg.correctAnswer === "string") {
if (Array.isArray(arg.incorrectAnswers) &&
arg.incorrectAnswers.every((a) => typeof a === "string")) {
if (typeof arg.question === "string") {
if (Array.isArray(arg.tags) &&
arg.tags.every((a) => typeof a === "string")) {
if (arg.type === "Multiple Choice") {
return true;
}
}
}
}
}
}
}
}
return false;
};