| 
									
										
										
										
											2021-06-22 07:46:42 -07:00
										 |  |  | // Copyright 2021 Signal Messenger, LLC
 | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-only
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-02 13:41:29 -08:00
										 |  |  | import type { UUIDStringType } from '../types/UUID'; | 
					
						
							| 
									
										
										
										
											2021-10-26 15:59:08 -07:00
										 |  |  | import { isValidUuid } from '../types/UUID'; | 
					
						
							| 
									
										
										
										
											2022-02-14 11:36:32 -08:00
										 |  |  | import type { LoggerType } from '../types/Logging'; | 
					
						
							|  |  |  | import * as log from '../logging/log'; | 
					
						
							| 
									
										
										
										
											2021-06-22 07:46:42 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-14 11:36:32 -08:00
										 |  |  | export function normalizeUuid( | 
					
						
							|  |  |  |   uuid: string, | 
					
						
							|  |  |  |   context: string, | 
					
						
							|  |  |  |   logger: Pick<LoggerType, 'warn'> = log | 
					
						
							|  |  |  | ): UUIDStringType { | 
					
						
							| 
									
										
										
										
											2022-02-02 13:41:29 -08:00
										 |  |  |   const result = uuid.toLowerCase(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-14 11:36:32 -08:00
										 |  |  |   if (!isValidUuid(uuid) || !isValidUuid(result)) { | 
					
						
							|  |  |  |     logger.warn( | 
					
						
							|  |  |  |       `Normalizing invalid uuid: ${uuid} to ${result} in context "${context}"` | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Cast anyway we don't want to throw here
 | 
					
						
							|  |  |  |     return result as unknown as UUIDStringType; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-06-22 07:46:42 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-02 13:41:29 -08:00
										 |  |  |   return result; | 
					
						
							| 
									
										
										
										
											2021-06-22 07:46:42 -07:00
										 |  |  | } |