forked from svt/bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom.js
33 lines (31 loc) · 901 Bytes
/
random.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// SPDX-FileCopyrightText: 2022 Sveriges Television AB
//
// SPDX-License-Identifier: MIT
const DEFAULT_CHARACTER_MAP = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
/**
* Generate a random string of
* characters in the map
*
* This function is NOT
* cryptographically strong
*
* @param { Number } length The length of the returned string
* @param { String } map A string of characters to include
* in the returned string
* @returns { String }
*//**
* Generate a random string
*
* This function is NOT
* cryptographically strong
*
* @param { Number } length The length of the returned string
* @returns { String }
*/
function string (length = 10, map = DEFAULT_CHARACTER_MAP) {
return Array
.from({ length }, () => Math.round(Math.random() * map.length))
.map(val => map[val % map.length])
.join('')
}
exports.string = string