freeCodeCamp/seed/challengeTitles.js

29 lines
734 B
JavaScript
Raw Normal View History

class ChallengeTitles {
constructor() {
this.knownTitles = [];
}
check(title) {
if (typeof title !== 'string') {
2018-06-20 13:50:16 +00:00
throw new Error(`
Expected a valid string for ${title}, but got a(n) ${typeof title}
`);
} else if (title.length === 0) {
2018-06-20 13:50:16 +00:00
throw new Error(`
Expected a title length greater than 0
`);
}
const titleToCheck = title.toLowerCase().replace(/\s+/g, '');
const isKnown = this.knownTitles.includes(titleToCheck);
if (isKnown) {
throw new Error(`
All challenges must have a unique title.
The title ${title} is already assigned
`);
}
this.knownTitles = [ ...this.knownTitles, titleToCheck ];
}
}
export default ChallengeTitles;