| 
									
										
										
										
											2018-04-11 05:23:36 +09:00
										 |  |  | import _ from 'lodash'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ChallengeTitles { | 
					
						
							|  |  |  |   constructor() { | 
					
						
							|  |  |  |     this.knownTitles = []; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   check(title) { | 
					
						
							|  |  |  |     if (typeof title !== 'string') { | 
					
						
							| 
									
										
										
										
											2018-04-13 23:15:40 +09:00
										 |  |  |       throw new Error(`Expected a valid string for ${title}, but got a(n) ${typeof title}`); | 
					
						
							| 
									
										
										
										
											2018-04-11 05:23:36 +09:00
										 |  |  |     } else if (title.length === 0) { | 
					
						
							|  |  |  |       throw new Error(`Expected a title length greater than 0`); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     const titleToCheck = title.toLowerCase().replace(/\s+/g, ''); | 
					
						
							| 
									
										
										
										
											2018-04-13 23:15:40 +09:00
										 |  |  |     const isKnown = this.knownTitles.includes(titleToCheck); | 
					
						
							|  |  |  |     if (isKnown) { | 
					
						
							| 
									
										
										
										
											2018-04-11 05:23:36 +09:00
										 |  |  |       throw new Error(`
 | 
					
						
							|  |  |  |     All challenges must have a unique title. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     The title ${title} is already assigned | 
					
						
							|  |  |  |     `);
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     this.knownTitles = [ ...this.knownTitles, titleToCheck ]; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default ChallengeTitles; |