mirror of
https://github.com/CorentinTh/it-tools.git
synced 2025-04-24 00:36:14 -04:00
29 lines
811 B
TypeScript
29 lines
811 B
TypeScript
import { shuffleString } from '@/utils/random';
|
|
|
|
export function createToken({
|
|
withUppercase = true,
|
|
withLowercase = true,
|
|
withNumbers = true,
|
|
withSymbols = false,
|
|
deniedChars = '',
|
|
length = 64,
|
|
alphabet,
|
|
}: {
|
|
withUppercase?: boolean
|
|
withLowercase?: boolean
|
|
withNumbers?: boolean
|
|
withSymbols?: boolean
|
|
deniedChars?: string
|
|
length?: number
|
|
alphabet?: string
|
|
}) {
|
|
const allAlphabet = (alphabet ?? (
|
|
(withUppercase ? 'ABCDEFGHIJKLMOPQRSTUVWXYZ' : '')
|
|
+ (withLowercase ? 'abcdefghijklmopqrstuvwxyz' : '')
|
|
+ (withNumbers ? '0123456789' : '')
|
|
+ (withSymbols ? '.,;:!?./-"\'#{([-|\\@)]=}*+' : '')
|
|
)).split('').filter(c => !(deniedChars?.includes(c))).join('');
|
|
|
|
const len = length < 1 ? 1 : length;
|
|
return shuffleString(allAlphabet.repeat(len)).substring(0, len);
|
|
}
|