-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
60 lines (51 loc) · 1.19 KB
/
utils.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
if( ! Homie ){
Homie = {};
}
/**
* Homie Utilities
* @type {{randomID: Homie.Utils.randomID, remNullProps: Homie.Utils.remNullProps}}
*/
Homie.Utils = {
/**
* Generate Random ID
* @param len
* @param pattern
* @returns {string}
*/
randomID: function( len, pattern ){
let possibilities = ["abcdefghijklmnopqrstuvwxyz","ABCDEFGHIJKLMNOPQRSTUVWXYZ", "0123456789", "~!@#$%^&()_+-={}[];\',"];
let chars = "";
pattern = pattern ? pattern : "aA0";
pattern.split('').forEach(function(a){
if(!isNaN(parseInt(a))){
chars += possibilities[2];
}else if(/[a-z]/.test(a)){
chars += possibilities[0];
}else if(/[A-Z]/.test(a)){
chars += possibilities[1];
}else{
chars += possibilities[3];
}
});
len = len ? len : 30;
let result = '';
while(len--){
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
},
/**
* Remove NULL properties
* @param test
* @param recurse
*/
remNullProps: function(test, recurse){
for (let i in test) {
if ( test.hasOwnProperty(i) && test[i] === null) {
delete test[i];
} else if (recurse && typeof test[i] === 'object') {
this.remNullProps(test[i], recurse);
}
}
},
};