// TODO i18n
});
-export function randomStr() {
- return Math.random()
- .toString(36)
- .replace(/[^a-z]+/g, '')
- .substr(2, 10);
+const DEFAULT_ALPHABET =
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
+
+function getRandomCharFromAlphabet(alphabet: string): string {
+ return alphabet.charAt(Math.floor(Math.random() * alphabet.length));
+}
+
+export function randomStr(
+ idDesiredLength: number = 20,
+ alphabet = DEFAULT_ALPHABET
+): string {
+ /**
+ * Create n-long array and map it to random chars from given alphabet.
+ * Then join individual chars as string
+ */
+ return Array.from({ length: idDesiredLength })
+ .map(() => {
+ return getRandomCharFromAlphabet(alphabet);
+ })
+ .join('');
}
export function wsJsonToRes(msg: WebSocketJsonResponse): WebSocketResponse {