import short from "short-uuid" import { useOf } from "../../cache" const useTranslator = useOf(() => short(short.constants.flickrBase58, { consistentLength: false, }) ) /** 把 UUID 转换为短的形式 * @example * uuidToShort("c2d7cf0e-7d97-457e-bf61-6fe2dafc8adc") // "3neAo7bG44mcTkraf2puyx" */ export function uuidToShort(uuid: string) { return useTranslator().fromUUID(uuid) } /** 把短形式的 UUID 还原为正规 UUID * @example * uuidFromShort("3neAo7bG44mcTkraf2puyx") // "c2d7cf0e-7d97-457e-bf61-6fe2dafc8adc" */ export function uuidFromShort(shortUUID: string) { return useTranslator().toUUID(shortUUID) } /** * 校验短形式的 UUID 是否合法 * @example * validateShortUUID("3neAo7bG44mcTkraf2puyx") // true * validateShortUUID("invalid-short-uuid") // false */ export function validateShortUUID(shortUUID: string) { return useTranslator().validate(shortUUID) }