-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (48 loc) · 1.02 KB
/
index.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* Expose `rndid`.
*/
exports = module.exports = rndid;
/**
* Default ID length.
*/
exports.defaultLength = 7;
/**
* Return a guaranteed unique id of the provided
* `length`, optionally prefixed with `prefix`.
*
* If no length is provided, will use
* `rndid.defaultLength`.
*
* @api private
* @param {String} [prefix]
* @param {Number} [length]
* @return {String}
*/
function rndid(prefix, length) {
if ('number' == typeof prefix)
length = prefix, prefix = '';
length = length || exports.defaultLength;
var id = (prefix || '') + str(length);
if (document.getElementById(id)) return rndid(prefix, length);
return id;
}
/**
* Generate a random alpha-char.
*
* @api private
* @return {String}
*/
function character() {
return String.fromCharCode(Math.floor(Math.random() * 25) + 97);
}
/**
* Generate a random alpha-string of `len` characters.
*
* @api private
* @param {Number} len
* @return {String}
*/
function str(len) {
for (var i = 0, s = ''; i < len; i++) s += character();
return s;
}