]> git.lizzy.rs Git - dragonblocks.git/blobdiff - lib/dblib.js
Abstract MapDisplay from Map
[dragonblocks.git] / lib / dblib.js
index ec85f88b03cac2a192a7a26afe94c0c4a0fb0a0e..9c1a11323797b77892f5f42c89fa9dcbf59187d3 100644 (file)
@@ -1,70 +1,99 @@
-// Library for Dragonblocks
-class dblib{
-       static center(elem){
+class dblib
+{
+       static center(elem)
+       {
                elem.style.left = elem.parentNode.clientWidth / 2 - parseInt(elem.clientWidth) / 2 + "px";
        }
-       static centerVertical(elem){
+
+       static centerVertical(elem)
+       {
                let parent = elem.parentNode;
                if (parent == document.body)
                        parent = document.documentElement;
                elem.style.top = parent.clientHeight / 2 - parseInt(elem.clientHeight) / 2 + "px";
        }
-       static random(min, max){
+
+       static random(min, max)
+       {
                return Math.floor(min + Math.random() * (max - min + 1));
        }
-       static humanFormat(str){
+
+       static humanFormat(str)
+       {
                str = str.replace("_", " ");
                str = str[0].toUpperCase() + str.slice(1, str.length);
                return str;
        }
-       static copy(dest, src){
-               for(let prop in src){
-                       if(src[prop] instanceof Array){
-                               dest[prop] = [];
-                               this.copy(dest[prop], src[prop]);
-                       }
-                       else if(src[prop] instanceof Function){
-                               dest[prop] = src[prop];
-                       }
-                       else if(src[prop] instanceof Object){
-                               dest[prop] = {};
-                               this.copy(dest[prop], src[prop]);
-                       }
-                       else{
-                               dest[prop] = src[prop];
+
+       static copy(dst, src, condition)
+       {
+               for (let key in src){
+                       let value = src[key];
+
+                       if (condition && ! condition(key, value)) {
+                               console.log(key, value);
+                               continue;
                        }
+
+                       if (value instanceof Array)
+                               this.copy(dst[key] = [], value, condition);
+                       else if (value instanceof Function)
+                               dst[key] = value;
+                       else if (value instanceof Object)
+                               this.copy(dst[key] = {}, value, condition);
+                       else
+                               dst[key] = value;
                }
+
+               return dst;
        }
-       static copySimple(dest, src){
-               for(let prop in src){
-                       dest[prop] = src[prop];
-               }
+
+       static copySimple(dst, src)
+       {
+               for (let key in src)
+                       dst[key] = src[key];
        }
-       static htmlEntities(str){
-               return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/\n/g, "<br>").replace(/\t/g, "&emsp;&emsp;").replace(/ /g, "&ensp;");
+
+       static htmlEntities(str)
+       {
+               return str
+                       .replace(/&/g, '&amp;')
+                       .replace(/</g, '&lt;')
+                       .replace(/>/g, '&gt;')
+                       .replace(/"/g, '&quot;')
+                       .replace(/\n/g, "<br>")
+                       .replace(/\t/g, "&emsp;&emsp;")
+                       .replace(/ /g, "&ensp;");
        }
-       static removeTmp(src){
-               let obj = {};
-               if(src instanceof Array)
-                       obj = [];
-               dblib.copy(obj, src);
-               for(let prop in obj){
-                       if(obj[prop].tmp)
-                               delete obj[prop].tmp
-               }
-               return obj;
+
+       static removeTmp(src)
+       {
+               let dst = {};
+
+               if (src instanceof Array)
+                       dst = [];
+
+               return dblib.copy(dst, src, key => key != "tmp");;
        }
-       static replaceRecursive(src, search, replace){
-               let obj = {};
-               if(src instanceof Array)
-                       obj = [];
-               dblib.copySimple(obj, src);
-               for(let prop in obj){
-                       if(obj[prop] === search)
-                               obj[prop] = replace;
-                       else if(obj[prop] instanceof Object)
-                               obj[prop] = this.replaceRecursive(obj[prop], search, replace);
+
+       static replaceRecursive(src, search, replace)
+       {
+               let dst = {};
+
+               if (src instanceof Array)
+                       dst = [];
+
+               dblib.copySimple(dst, src);
+
+               for (let key in dst) {
+                       let value = dst[key];
+
+                       if (value === search)
+                               value = replace;
+                       else if (value instanceof Object)
+                               value = this.replaceRecursive(value, search, replace);
                }
-               return obj;
+
+               return dst;
        }
 }