]> git.lizzy.rs Git - dragonblocks.git/blob - lib/dblib.js
Implement per-map sky
[dragonblocks.git] / lib / dblib.js
1 class dblib
2 {
3         static center(elem)
4         {
5                 elem.style.left = elem.parentNode.clientWidth / 2 - parseInt(elem.clientWidth) / 2 + "px";
6         }
7
8         static centerVertical(elem)
9         {
10                 let parent = elem.parentNode;
11                 if (parent == document.body)
12                         parent = document.documentElement;
13                 elem.style.top = parent.clientHeight / 2 - parseInt(elem.clientHeight) / 2 + "px";
14         }
15
16         static random(min, max)
17         {
18                 return Math.floor(min + Math.random() * (max - min + 1));
19         }
20
21         static humanFormat(str)
22         {
23                 str = str.replace("_", " ");
24                 str = str[0].toUpperCase() + str.slice(1, str.length);
25                 return str;
26         }
27
28         static copy(dst, src, condition)
29         {
30                 for (let key in src){
31                         let value = src[key];
32
33                         if (condition && ! condition(key, value)) {
34                                 console.log(key, value);
35                                 continue;
36                         }
37
38                         if (value instanceof Array)
39                                 this.copy(dst[key] = [], value, condition);
40                         else if (value instanceof Function)
41                                 dst[key] = value;
42                         else if (value instanceof Object)
43                                 this.copy(dst[key] = {}, value, condition);
44                         else
45                                 dst[key] = value;
46                 }
47
48                 return dst;
49         }
50
51         static copySimple(dst, src)
52         {
53                 for (let key in src)
54                         dst[key] = src[key];
55
56                 return dst;
57         }
58
59         static htmlEntities(str)
60         {
61                 return str
62                         .replace(/&/g, '&')
63                         .replace(/</g, '&lt;')
64                         .replace(/>/g, '&gt;')
65                         .replace(/"/g, '&quot;')
66                         .replace(/\n/g, "<br>")
67                         .replace(/\t/g, "&emsp;&emsp;")
68                         .replace(/ /g, "&ensp;");
69         }
70
71         static removeTmp(src)
72         {
73                 let dst = {};
74
75                 if (src instanceof Array)
76                         dst = [];
77
78                 return dblib.copy(dst, src, key => key != "tmp");;
79         }
80
81         static replaceRecursive(src, search, replace)
82         {
83                 let dst = {};
84
85                 if (src instanceof Array)
86                         dst = [];
87
88                 dblib.copySimple(dst, src);
89
90                 for (let key in dst) {
91                         let value = dst[key];
92
93                         if (value === search)
94                                 value = replace;
95                         else if (value instanceof Object)
96                                 value = this.replaceRecursive(value, search, replace);
97
98                         dst[key] = value;
99                 }
100
101                 return dst;
102         }
103 }