]> git.lizzy.rs Git - dragonblocks.git/blob - lib/dblib.js
9c1a11323797b77892f5f42c89fa9dcbf59187d3
[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
57         static htmlEntities(str)
58         {
59                 return str
60                         .replace(/&/g, '&')
61                         .replace(/</g, '&lt;')
62                         .replace(/>/g, '&gt;')
63                         .replace(/"/g, '&quot;')
64                         .replace(/\n/g, "<br>")
65                         .replace(/\t/g, "&emsp;&emsp;")
66                         .replace(/ /g, "&ensp;");
67         }
68
69         static removeTmp(src)
70         {
71                 let dst = {};
72
73                 if (src instanceof Array)
74                         dst = [];
75
76                 return dblib.copy(dst, src, key => key != "tmp");;
77         }
78
79         static replaceRecursive(src, search, replace)
80         {
81                 let dst = {};
82
83                 if (src instanceof Array)
84                         dst = [];
85
86                 dblib.copySimple(dst, src);
87
88                 for (let key in dst) {
89                         let value = dst[key];
90
91                         if (value === search)
92                                 value = replace;
93                         else if (value instanceof Object)
94                                 value = this.replaceRecursive(value, search, replace);
95                 }
96
97                 return dst;
98         }
99 }