-
Notifications
You must be signed in to change notification settings - Fork 20
/
CloverID.js
75 lines (66 loc) · 1.63 KB
/
CloverID.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* A utility class to create Clover compatible identifiers, and guids.
*
* @constructor
*/
function CloverID() {
}
CloverID.ID_LENGTH = 13;
// http://www.crockford.com/wrmg/base32.html
/**
* The legal set of characters used to generate a clover id.
* @type {string[]}
*/
CloverID.BASE_32_DIGITS = [
'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'A', 'B',
'C', 'D', 'E', 'F', 'G', 'H',
'J', 'K', 'M', 'N', 'P', 'Q',
'R', 'S', 'T', 'V', 'W', 'X',
'Y', 'Z'
];
/**
* @returns {string} a clover compatible ID.
*/
CloverID.getNewId = function() {
var id = "";
for (var index = 0; index < CloverID.ID_LENGTH; index++) {
id += CloverID.BASE_32_DIGITS[Math.floor(Math.random() * CloverID.BASE_32_DIGITS.length)];
}
return id;
};
/**
*
* @param {string} id - a string id to test
* @returns {boolean} true if the id is a clover compatible ID.
*/
CloverID.isValidBase32Id = function(id){
if (id == null || id.length != CloverID.ID_LENGTH) {
return false;
}
for (var i = 0; i < id.length; i++) {
if (-1 == CloverID.BASE_32_DIGITS.indexOf(id.charAt(i))) {
return false;
}
}
return true;
}
/**
*
* @returns {string} a guid - see https://en.wikipedia.org/wiki/Globally_unique_identifier
*/
CloverID.guid = function() {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
s4() + '-' + s4() + s4() + s4();
};
//
// Expose the module.
//
if ('undefined' !== typeof module) {
module.exports = CloverID;
}