-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.js
72 lines (66 loc) · 1.9 KB
/
utilities.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
var _ = require('lodash');
_.includes = _.contains || _.includes;
var randomString = function (len, chars) {
len = len || 1;
var s = chars || "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var ticket = "";
for (var x = 0; x < len; x++) {
ticket += s.charAt(Math.floor(Math.random() * s.length));
}
return ticket;
};
var uGenerator = function () {
var self = this;
self.generated = [];
self.genUnique = function (len, chars) {
var s = chars || "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var ticket = "";
for (var x = 0; x < len; x++) {
ticket += s.charAt(Math.floor(Math.random() * s.length));
}
if(self.generated.includes(ticket)){
return self.genUnique(len,chars);
}else {
self.generated.push(ticket);
return ticket;
}
}
};
var tempGenerator = function () {
var self = this;
self.generated = [];
self.genUnique = function (len, chars) {
var s = chars || "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var ticket = "";
for (var x = 0; x < len; x++) {
ticket += s.charAt(Math.floor(Math.random() * s.length));
}
if(self.generated.includes(ticket)){
return self.genUnique(len,chars);
}else {
self.generated.push(ticket);
return ticket;
}
};
self.removeGen = function(d){
var i = self.generated.indexOf(d);
if(i>-1){
self.generated.splice(i,1);
}
}
};
var roundHTMLColor = function(color,prec){
var colors = [parseInt(color.substring(1,3),16),parseInt(color.substring(3,5),16),parseInt(color.substring(5,7),16)];
var new_color = '#';
for(var x in colors){
var c = colors[x];
var new_c = (Math.floor(c/prec)*prec).toString(16);
new_c = new_c.length == 1 ? '0'+new_c : new_c;
new_color += new_c;
}
return new_color;
};
module.exports.uGenerator = uGenerator;
module.exports.randomString = randomString;
module.exports.roundHTMLColor = roundHTMLColor;
module.exports.tempGenerator = tempGenerator;