forked from Peer5/ShareFest
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
68 lines (52 loc) · 1.61 KB
/
util.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
var DICTIONARY = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split('');
function generateID(seed) {
/* Given a seed, this function will return a 'shortened' URL */
if (typeof(seed) === 'undefined') seed = new Date().getTime();
return encode(seed);
}
function getSeedFromID(id) {
/* Given a 'shortened' URL, it will return the seed */
if (typeof(seed) === 'undefined') return -1;
return decode(id);
}
function encode(i) {
/* Generates a 'shortened' URL */
if (i == 0) return DICTIONARY[0];
var result = '';
var base = DICTIONARY.length;
while (i > 0) {
result += DICTIONARY[i % base];
i = Math.floor(i / base);
}
return result;
}
function decode(i) {
/* Returns the original input from a 'shortened' URL */
var i = 0;
var base = DICTIONARY.length;
input.split("").forEach(function (c) {
i = i * base + DICTIONARY.indexOf(c);
});
return i;
}
exports.makeid = function makeid() {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < 5; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
exports.getRandomK = function (array, k) {
var shuffled = exports.shuffle(array);
return shuffled.slice(0, k);
};
exports.shuffle = function (array) {
var tmp, current, top = array.length;
if (top) while (--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
return array;
}