17 lines
514 B
TypeScript
17 lines
514 B
TypeScript
export const genShortId = (length = 16) => {
|
|
const chars =
|
|
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
const array = new Uint8Array(length);
|
|
crypto.getRandomValues(array);
|
|
return Array.from(array, (byte) => chars[byte % chars.length]).join('');
|
|
};
|
|
|
|
export const genUuid = () => {
|
|
return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, (c: any) =>
|
|
(
|
|
c ^
|
|
((crypto.getRandomValues(new Uint8Array(1))[0] ?? 0) & (15 >> (c / 4)))
|
|
).toString(16),
|
|
);
|
|
};
|