-
Notifications
You must be signed in to change notification settings - Fork 1
/
hashid.js
39 lines (33 loc) · 1.03 KB
/
hashid.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
'use strict'
// Taken from: https://gist.github.com/fiznool/73ee9c7a11d1ff80b81c
/**
* The default alphabet is 25 numbers and lowercase letters.
* Any numbers that look like letters and vice versa are removed:
* 1 l, 0 o.
* Also the following letters are not present, to prevent any
* expletives: cfhistu
*/
var ALPHABET =
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
var ALPHABET_LENGTH = ALPHABET.length
// Governs the length of the ID.
// With an alphabet of 25 chars,
// a length of 8 gives us 25^8 or
// 152,587,890,625 possibilities.
// Should be enough...
var ID_LENGTH = 8
var HashID = {}
/**
* Returns a randomly-generated friendly ID.
* Note that the friendly ID is not guaranteed to be
* unique to any other ID generated by this same method,
* so it is up to you to check for uniqueness.
* @return {String} friendly ID.
*/
HashID.generate = function () {
var rtn = ''
for (var i = 0; i < ID_LENGTH; i++) {
rtn += ALPHABET.charAt(Math.floor(Math.random() * ALPHABET_LENGTH))
}
return rtn
}