Files
freeCodeCamp/curriculum/test/utils/challengeTitles.js
Nicholas Carrigan (he/him) b36cdbafd1 chore: rename "part" to "step" (#43934)
* chore: rename part to step

* chore: update metas

* chore: more renaming

* chore: update tooling

* chore: update frontmatter

* chore(tools): title testing
2021-10-21 18:07:52 +01:00

31 lines
1023 B
JavaScript

class ChallengeTitles {
constructor() {
this.knownTitles = [];
}
check(title, pathAndTitle) {
if (typeof title !== 'string') {
throw new Error(
`Expected a valid string for ${title}, but got a(n) ${typeof title}`
);
}
let titleToCheck = title.replace(/\s+/g, '').toLowerCase();
if (titleToCheck.length === 0) {
throw new Error('Expected a title length greater than 0');
}
// reassign titleToCheck if challenge is part of the project
// based curriculum
const isProjectCurriculumChallenge = title.match(/^Step\s*\d+$/);
titleToCheck = isProjectCurriculumChallenge ? pathAndTitle : titleToCheck;
const isKnown = this.knownTitles.includes(titleToCheck);
if (isKnown) {
throw new Error(`
All current curriculum challenges must have a unique title.
The title ${title} (at ${pathAndTitle}) is already assigned
`);
}
this.knownTitles = [...this.knownTitles, titleToCheck];
}
}
module.exports = ChallengeTitles;