Files
query-awesome/query/utils/random.ts
2026-02-16 19:25:56 +08:00

45 lines
1.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { customAlphabet } from 'nanoid';
import { createId } from '@paralleldrive/cuid2';
export const letter = 'abcdefghijklmnopqrstuvwxyz';
export const number = '0123456789';
const alphanumeric = `${letter}${number}`;
export const alphanumericWithDash = `${alphanumeric}-`;
export const cuid2 = createId;
/**
* 创建一个随机的字母字符串
*/
export const uuid = customAlphabet(letter);
/**
* 创建一个随机的 id包含字母和数字
*/
export const nanoid = customAlphabet(alphanumeric, 16);
/**
* 创建一个随机的 id包含字母、数字和短横线
*/
export const nanoidWithDash = customAlphabet(alphanumericWithDash, 16);
/**
* 创建一个随机的 id以字母开头的字符串
* @param number
* @returns
*/
export const randomId = (number: number) => {
const _letter = uuid(1);
return `${_letter}${nanoid(number)}`;
};
/**
* 创建一个随机的字母字符串
* @param number
* @param opts
* @returns
*/
export const randomLetter = (number: number = 8, opts?: { before?: string; after?: string }) => {
const { before = '', after = '' } = opts || {};
return `${before}${uuid(number)}${after}`;
};