@tmlmobilidade/utils
Version:
This package provides a collection of common utility functions used across projects within the organization.
28 lines (27 loc) • 859 B
JavaScript
/* * */
/**
* Creates a random string of a given length and type.
* @param length - The length of the string to generate.
* @param type - The type of characters to include in the string.
* @returns A random string of the specified length.
*/
export function generateRandomString({ length = 6, type = 'alphanumeric' } = {}) {
//
const numericSet = '0123456789';
const alphanumericSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let allowedCharacters;
switch (type) {
case 'numeric':
allowedCharacters = numericSet;
break;
default:
allowedCharacters = alphanumericSet;
break;
}
let result = '';
for (let i = 0; i < length; i++) {
result += allowedCharacters.charAt(Math.floor(Math.random() * allowedCharacters.length));
}
return result;
//
}