@roeeyn/challenge-generator
Version:
Fetches a code challenge from the backend, and creates the necessary files to run locally.
50 lines (49 loc) • 1.86 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.cleanCliOptions = void 0;
const models_1 = require("../models");
const validateKeys = (cleanedCliOptions) => {
const numericKeys = ["minDifficulty", "maxDifficulty"];
const programmingLanguageKeys = ["programmingLanguage"];
for (const key in cleanedCliOptions) {
const currentValue = cleanedCliOptions[key];
if (currentValue === undefined ||
currentValue === null ||
currentValue === "") {
throw new Error(`${key} is empty`);
}
else if (numericKeys.includes(key)) {
if (isNaN(currentValue) || currentValue < 0) {
throw new Error(`${key} must be a number greater than 0`);
}
}
else if (key === "tags") {
const newArray = currentValue.split(",");
const isValid = newArray
.map((tag) => tag.trim())
.every((tag) => /^[a-zA-Z]+$/.test(tag));
if (!Array.isArray(newArray) || !isValid) {
throw new Error(`${key} must be an array of only words`);
}
cleanedCliOptions["tags"] = newArray;
}
else if (programmingLanguageKeys.includes(key)) {
if (Object.values(models_1.ProgrammingLanguage).indexOf(currentValue) === -1) {
throw new Error(`${key} must be an array`);
}
}
}
return cleanedCliOptions;
};
const cleanCliOptions = (cliOptions) => {
const keptKeys = Object.keys(cliOptions).filter((key) => {
return !!cliOptions[key];
});
for (const key of Object.keys(cliOptions)) {
if (!keptKeys.includes(key)) {
delete cliOptions[key];
}
}
return validateKeys(cliOptions);
};
exports.cleanCliOptions = cleanCliOptions;
;