freeCodeCamp/challengeTitles.js
Kristofer Koishigawa 5e53ebded9 fix(seed): Simplify Unique Titles Test (#17056)
Originally the test would check for the index of a title in an array of
unique challenge titles. However, the index of the title within the
array isn't important for this test, so I simplified the code using
@Bouncey's suggestion in PR #17035.

Also changed grammar for the error that's thrown when a challenge
title isn't a valid string.

BREAKING CHANGE: None
2018-04-13 15:15:40 +01:00

27 lines
727 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}, but got a(n) ${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 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;