| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  | const { preFormatted, stopWords } = require('./formatting'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const prototypeRE = /prototype/i; | 
					
						
							|  |  |  | const prototypesRE = /prototypes/i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const removeProto = x => x !== 'prototype'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function titleCase(word) { | 
					
						
							|  |  |  |   return word[0].toUpperCase() + word.slice(1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function prototyper(str) { | 
					
						
							|  |  |  |   const formatted = str | 
					
						
							|  |  |  |     .replace(/javascript/i, '') | 
					
						
							|  |  |  |     .split('-') | 
					
						
							|  |  |  |     .map(str => { | 
					
						
							|  |  |  |       if (prototypeRE.test(str)) { | 
					
						
							|  |  |  |         if (str.length > 9) { | 
					
						
							|  |  |  |           return prototyper( | 
					
						
							| 
									
										
										
										
											2019-02-18 19:32:49 +00:00
										 |  |  |             str | 
					
						
							|  |  |  |               .trim() | 
					
						
							|  |  |  |               .split('prototype') | 
					
						
							|  |  |  |               .join('-prototype-') | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  |           ); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return str; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       return titleify(str); | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     .join(' ') | 
					
						
							|  |  |  |     .split(' '); | 
					
						
							| 
									
										
										
										
											2019-02-18 19:32:49 +00:00
										 |  |  |   const noProto = formatted.filter(removeProto).filter(x => !!x); | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  |   if (noProto.length === 2) { | 
					
						
							| 
									
										
										
										
											2019-02-18 19:32:49 +00:00
										 |  |  |     const [first, second] = noProto; | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  |     const secondLC = second.toLowerCase(); | 
					
						
							| 
									
										
										
										
											2019-02-18 19:32:49 +00:00
										 |  |  |     const finalSecond = preFormatted[secondLC] | 
					
						
							|  |  |  |       ? preFormatted[secondLC] | 
					
						
							|  |  |  |       : secondLC; | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  |     return `${titleify(first)}.prototype.${finalSecond}`; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   if (noProto.length === 1) { | 
					
						
							|  |  |  |     return prototyper( | 
					
						
							|  |  |  |       noProto[0] | 
					
						
							|  |  |  |         .toLowerCase() | 
					
						
							|  |  |  |         .split('.') | 
					
						
							|  |  |  |         .join('-') | 
					
						
							| 
									
										
										
										
											2019-02-18 19:32:49 +00:00
										 |  |  |     ); | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  |   } | 
					
						
							|  |  |  |   return titleify(str, true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function titleify(str, triedPrototyper) { | 
					
						
							|  |  |  |   if (str.match(prototypeRE) && !triedPrototyper && !prototypesRE.test(str)) { | 
					
						
							|  |  |  |     return prototyper(str); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return str | 
					
						
							|  |  |  |     .toLowerCase() | 
					
						
							|  |  |  |     .split('-') | 
					
						
							|  |  |  |     .map((word, i) => { | 
					
						
							|  |  |  |       if (!word) { | 
					
						
							|  |  |  |         return ''; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (stopWords.some(x => x === word) && i !== 0) { | 
					
						
							|  |  |  |         return word; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2019-02-18 19:32:49 +00:00
										 |  |  |       return preFormatted[word] ? preFormatted[word] : titleCase(word); | 
					
						
							| 
									
										
										
										
											2018-10-04 14:47:55 +01:00
										 |  |  |     }) | 
					
						
							|  |  |  |     .join(' '); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = titleify; |