Signal-Desktop/ts/util/normalizeUuid.ts

27 lines
713 B
TypeScript
Raw Normal View History

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