2018-04-11 05:23:36 +09:00
|
|
|
class ChallengeTitles {
|
|
|
|
constructor() {
|
|
|
|
this.knownTitles = [];
|
|
|
|
}
|
2019-12-09 19:36:32 +09:00
|
|
|
check(title, pathAndTitle) {
|
2018-04-11 05:23:36 +09:00
|
|
|
if (typeof title !== 'string') {
|
2018-10-23 16:21:53 +03:00
|
|
|
throw new Error(
|
|
|
|
`Expected a valid string for ${title}, but got a(n) ${typeof title}`
|
|
|
|
);
|
|
|
|
}
|
2020-06-24 16:22:14 +09:00
|
|
|
let titleToCheck = title.replace(/\s+/g, '').toLowerCase();
|
2018-10-23 16:21:53 +03:00
|
|
|
if (titleToCheck.length === 0) {
|
|
|
|
throw new Error('Expected a title length greater than 0');
|
2018-04-11 05:23:36 +09:00
|
|
|
}
|
2020-06-24 16:22:14 +09:00
|
|
|
// reassign titleToCheck if challenge is part of the project
|
|
|
|
// based curriculum
|
|
|
|
const isProjectCurriculumChallenge = title.match(/^Part\s*\d+$/);
|
|
|
|
titleToCheck = isProjectCurriculumChallenge ? pathAndTitle : titleToCheck;
|
|
|
|
const isKnown = this.knownTitles.includes(titleToCheck);
|
2018-04-13 23:15:40 +09:00
|
|
|
if (isKnown) {
|
2018-04-11 05:23:36 +09:00
|
|
|
throw new Error(`
|
2020-07-24 09:13:46 -07:00
|
|
|
All current curriculum challenges must have a unique title.
|
|
|
|
The title ${title} (at ${pathAndTitle}) is already assigned
|
|
|
|
`);
|
2018-04-11 05:23:36 +09:00
|
|
|
}
|
2020-06-24 16:22:14 +09:00
|
|
|
this.knownTitles = [...this.knownTitles, titleToCheck];
|
2018-04-11 05:23:36 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 16:21:53 +03:00
|
|
|
module.exports = ChallengeTitles;
|