freeCodeCamp/challengeTitles.js
Kristofer Koishigawa 5fa098ea0d test(seed): Add Test for Unique Challenge Titles (#17035)
Due to the recent problem with two challenges having the same title, I
added a check for unique titles to the test suite so that hopefully
won't happen again.

Addresses a comment in issue #16906, which was recently closed.

BREAKING CHANGE: None

Closes #16906
2018-04-10 21:23:36 +01:00

27 lines
760 B
JavaScript

import _ from 'lodash';
class ChallengeTitles {
constructor() {
this.knownTitles = [];
}
check(title) {
if (typeof title !== 'string') {
throw new Error(`Expected a valid string for ${title}, got ${typeof title}`);
} else if (title.length === 0) {
throw new Error(`Expected a title length greater than 0`);
}
const titleToCheck = title.toLowerCase().replace(/\s+/g, '');
const titleIndex = _.findIndex(this.knownTitles, existing => titleToCheck === existing);
if (titleIndex !== -1) {
throw new Error(`
All challenges must have a unique title.
The title ${title} is already assigned
`);
}
this.knownTitles = [ ...this.knownTitles, titleToCheck ];
}
}
export default ChallengeTitles;