27 lines
812 B
TypeScript
27 lines
812 B
TypeScript
import { customAlphabet } from 'nanoid';
|
||
|
||
export const letter = 'abcdefghijklmnopqrstuvwxyz';
|
||
export const number = '0123456789';
|
||
const alphanumeric = `${letter}${number}`;
|
||
export const alphanumericWithDash = `${alphanumeric}-`;
|
||
export const uuid = customAlphabet(letter);
|
||
|
||
export const nanoid = customAlphabet(alphanumeric, 10);
|
||
|
||
export const nanoidWithDash = customAlphabet(alphanumericWithDash, 10);
|
||
|
||
/**
|
||
* 创建一个随机的 id,以字母开头的字符串
|
||
* @param number
|
||
* @returns
|
||
*/
|
||
export const randomId = (number: number) => {
|
||
const _letter = uuid(1);
|
||
return `${_letter}${nanoid(number)}`;
|
||
};
|
||
|
||
export const randomLetter = (number: number = 8, opts?: { before?: string; after?: string }) => {
|
||
const { before = '', after = '' } = opts || {};
|
||
return `${before}${uuid(number)}${after}`;
|
||
};
|