2019-11-11 19:52:27 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								const invalidCharError = {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  valid: false,
							 | 
						
					
						
							
								
									
										
										
										
											2020-12-16 02:02:52 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  error: 'contains invalid characters'
							 | 
						
					
						
							
								
									
										
										
										
											2019-11-11 19:52:27 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								};
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								const validationSuccess = { valid: true, error: null };
							 | 
						
					
						
							
								
									
										
										
										
											2020-12-16 02:02:52 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								const usernameTooShort = { valid: false, error: 'is too short' };
							 | 
						
					
						
							
								
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								const usernameIsHttpStatusCode = {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  valid: false,
							 | 
						
					
						
							
								
									
										
										
										
											2020-12-16 02:02:52 -06:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  error: 'is a reserved error code'
							 | 
						
					
						
							
								
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								};
							 | 
						
					
						
							
								
									
										
										
										
											2021-05-20 02:04:00 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								const usernameUpperCase = { valid: false, error: 'must be lowercase' };
							 | 
						
					
						
							
								
									
										
										
										
											2019-11-11 19:52:27 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								const isNumeric = num => !isNaN(num);
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								const validCharsRE = /^[a-zA-Z0-9\-_+]*$/;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								const isHttpStatusCode = str =>
							 | 
						
					
						
							
								
									
										
										
										
											2021-03-11 00:31:46 +05:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  isNumeric(str) && parseInt(str, 10) >= 100 && parseInt(str, 10) <= 599;
							 | 
						
					
						
							
								
									
										
										
										
											2021-05-20 02:04:00 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								const isUsernameLowercase = str => {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  return str === str.toLowerCase();
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								};
							 | 
						
					
						
							
								
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								const isValidUsername = str => {
							 | 
						
					
						
							
								
									
										
										
										
											2019-11-11 19:52:27 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  if (!validCharsRE.test(str)) return invalidCharError;
							 | 
						
					
						
							
								
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  if (str.length < 3) return usernameTooShort;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  if (isHttpStatusCode(str)) return usernameIsHttpStatusCode;
							 | 
						
					
						
							
								
									
										
										
										
											2021-05-20 02:04:00 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  if (!isUsernameLowercase(str)) return usernameUpperCase;
							 | 
						
					
						
							
								
									
										
										
										
											2019-11-11 19:52:27 +01:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  return validationSuccess;
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								};
							 | 
						
					
						
							
								
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								module.exports = {
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  isNumeric,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  isHttpStatusCode,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  isValidUsername,
							 | 
						
					
						
							
								
									
										
										
										
											2021-05-20 02:04:00 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  isUsernameLowercase,
							 | 
						
					
						
							
								
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  validationSuccess,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  usernameTooShort,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  usernameIsHttpStatusCode,
							 | 
						
					
						
							
								
									
										
										
										
											2021-05-20 02:04:00 -05:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								  invalidCharError,
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								  usernameUpperCase
							 | 
						
					
						
							
								
									
										
										
										
											2019-11-27 10:49:17 +05:30
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								};
							 |