-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (50 loc) · 1.13 KB
/
index.js
File metadata and controls
62 lines (50 loc) · 1.13 KB
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
59
60
61
62
/*
Short-ID-Generator
by Jannik Sommerfeld
*/
var valueToChar = function(value) {
if (value > 9) {
var asciiDec = 55 + value
if (asciiDec > 90) {
asciiDec += 6;
}
return String.fromCharCode(asciiDec)
}
else {
return value.toString();
}
}
var valueToString = function(value) {
var x = value % 62
var y = Math.floor(value/62)
if (y > 0) {
return valueToString(y) + valueToChar(x)
} else {
return valueToChar(x)
}
}
exports.generate = function (minLength, maxLength) {
// Validation Check
if (minLength <= 0 || minLength > maxLength) {
console.log(new Error('Invalid Values. (short-id-gen)'))
return -1
}
// Default Length (0 params)
if (typeof minLength === 'undefined') {
minLength = 4
maxLength = 6
}
// if exact length (1 param)
if (typeof maxLength === 'undefined') {
maxLength = minLength
}
var minRandom, maxRandom, random
if (minLength == 1) {
minRandom = 0 // as opposed to 1
} else {
minRandom = Math.pow(62, minLength - 1)
}
maxRandom = Math.pow(62, maxLength) - 1
random = Math.floor(Math.random() * (maxRandom - minRandom) + minRandom)
return valueToString(random)
}