refactor: token generator can use a custom alphabet

This commit is contained in:
Corentin Thomasset 2022-08-24 00:09:16 +02:00
parent 737319edf1
commit 83da6b7ee9
No known key found for this signature in database
GPG key ID: DBD997E935996158

View file

@ -6,19 +6,23 @@ export function createToken({
withNumbers = true,
withSymbols = false,
length = 64,
alphabet,
}: {
withUppercase?: boolean;
withLowercase?: boolean;
withNumbers?: boolean;
withSymbols?: boolean;
length?: number;
alphabet?: string;
}) {
const alphabet = [
...(withUppercase ? 'ABCDEFGHIJKLMOPQRSTUVWXYZ' : ''),
...(withLowercase ? 'abcdefghijklmopqrstuvwxyz' : ''),
...(withNumbers ? '0123456789' : ''),
...(withSymbols ? '.,;:!?./-"\'#{([-|\\@)]=}*+' : ''),
].join('');
const allAlphabet =
alphabet ??
[
...(withUppercase ? 'ABCDEFGHIJKLMOPQRSTUVWXYZ' : ''),
...(withLowercase ? 'abcdefghijklmopqrstuvwxyz' : ''),
...(withNumbers ? '0123456789' : ''),
...(withSymbols ? '.,;:!?./-"\'#{([-|\\@)]=}*+' : ''),
].join('');
return shuffleString(alphabet.repeat(length)).substring(0, length);
return shuffleString(allAlphabet.repeat(length)).substring(0, length);
}