it-tools/src/tools/token-generator/token-generator.service.ts

30 lines
811 B
TypeScript
Raw Normal View History

2022-04-04 00:25:29 +02:00
import { shuffleString } from '@/utils/random';
export function createToken({
withUppercase = true,
withLowercase = true,
withNumbers = true,
withSymbols = false,
deniedChars = '',
2022-04-04 00:25:29 +02:00
length = 64,
alphabet,
2022-04-04 00:25:29 +02:00
}: {
withUppercase?: boolean
withLowercase?: boolean
withNumbers?: boolean
withSymbols?: boolean
deniedChars?: string
length?: number
alphabet?: string
2022-04-04 00:25:29 +02:00
}) {
2024-08-03 15:03:43 +02:00
const allAlphabet = (alphabet ?? (
(withUppercase ? 'ABCDEFGHIJKLMOPQRSTUVWXYZ' : '')
+ (withLowercase ? 'abcdefghijklmopqrstuvwxyz' : '')
+ (withNumbers ? '0123456789' : '')
+ (withSymbols ? '.,;:!?./-"\'#{([-|\\@)]=}*+' : '')
)).split('').filter(c => !(deniedChars?.includes(c))).join('');
2022-04-04 00:25:29 +02:00
2024-09-22 19:03:02 +02:00
const len = length < 1 ? 1 : length;
return shuffleString(allAlphabet.repeat(len)).substring(0, len);
2022-04-04 00:25:29 +02:00
}