2021-06-22 14:46:42 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-02-02 21:41:29 +00:00
|
|
|
import type { UUIDStringType } from '../types/UUID';
|
2021-10-26 22:59:08 +00:00
|
|
|
import { isValidUuid } from '../types/UUID';
|
2022-02-14 19:36:32 +00:00
|
|
|
import type { LoggerType } from '../types/Logging';
|
|
|
|
import * as log from '../logging/log';
|
2021-06-22 14:46:42 +00:00
|
|
|
|
2022-02-14 19:36:32 +00:00
|
|
|
export function normalizeUuid(
|
|
|
|
uuid: string,
|
|
|
|
context: string,
|
|
|
|
logger: Pick<LoggerType, 'warn'> = log
|
|
|
|
): UUIDStringType {
|
2022-02-02 21:41:29 +00:00
|
|
|
const result = uuid.toLowerCase();
|
|
|
|
|
2022-02-14 19:36:32 +00: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 14:46:42 +00:00
|
|
|
|
2022-02-02 21:41:29 +00:00
|
|
|
return result;
|
2021-06-22 14:46:42 +00:00
|
|
|
}
|