index.js 4.61 KB
Newer Older
liuxin committed
1 2
export default class Utils {
    static toDepthObject(obj) {
fukai committed
3
        var target = {};
liuxin committed
4
        for (var key in obj) {
fukai committed
5 6
            var keyArr = key.split("_"); //以下划线或分割
            var tempobj = target;
liuxin committed
7 8
            for (var i = 0; i < keyArr.length - 1; i++) {
                if (tempobj[keyArr[i]])
fukai committed
9
                    tempobj = tempobj[keyArr[i]];
liuxin committed
10
                else {
fukai committed
11 12
                    tempobj[keyArr[i]] = {};
                    tempobj = tempobj[keyArr[i]];
liuxin committed
13
                }
fukai committed
14 15
            }
            //给叶子元素赋值
liuxin committed
16
            tempobj[keyArr[keyArr.length - 1]] = obj[key];
fukai committed
17 18 19
        }
        return target;
    }
liuxin committed
20 21 22 23 24
    static flatObject(obj, target, pkey) {
        target = target || {};
        for (var key in obj) {
            var nkey = pkey ? pkey + "_" + key : key;
            if (!obj[key])
25
                target[nkey] = obj[key];
liuxin committed
26
            else if (obj[key] instanceof Array)
27
                target[nkey] = obj[key];
liuxin committed
28 29 30
            else if (obj[key] instanceof Date)
                target[nkey] = obj[key];
            else if (typeof obj[key] == "object")
潘际乾 committed
31
                if (nkey === "trncorco_trnstm" || nkey === "trncorco_selinr") {
32 33 34 35
                    target[nkey] = obj[key];
                } else {
                    Utils.flatObject(obj[key], target, nkey); //递归赋值
                }
liuxin committed
36 37 38 39
            else
                target[nkey] = obj[key];
        }
        return target;
fukai committed
40
    }
liuxin committed
41
    static copyValueFromVO(model, vo) {
fukai committed
42
        var target = model;
liuxin committed
43
        for (var key in vo) {
fukai committed
44 45
            var keyArr = key.split("_"); //以下划线或分割
            var tempobj = target;
liuxin committed
46 47
            for (var i = 0; i < keyArr.length - 1; i++) {
                if (tempobj[keyArr[i]])
fukai committed
48
                    tempobj = tempobj[keyArr[i]];
liuxin committed
49
                else {
fukai committed
50 51
                    tempobj = null;
                    break;
liuxin committed
52
                }
fukai committed
53 54
            }
            //给叶子元素赋值
liuxin committed
55
            if (tempobj) {
fukai committed
56
                let leafProp = keyArr[keyArr.length - 1];
liuxin committed
57 58
                if (tempobj.hasOwnProperty(leafProp))
                    tempobj[leafProp] = vo[key];
fukai committed
59 60 61 62
            }
        }
        return target;
    }
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

    static showXml(str){
        var text = str
    
        //去掉多余的空格
        text = '\n' + text.replace(/(<\w+)(\s.*?>)/g,function($0, name, props)
        {
            return name + ' ' + props.replace(/\s+(\w+=)/g," $1");
        }).replace(/>\s*?</g,">\n<");
    
        //把注释编码
        text = text.replace(/\n/g,'\r').replace(/<!--(.+?)-->/g,function($0, text)
        {
            var ret = '<!--' + escape(text) + '-->';
            return ret;
        }).replace(/\r/g,'\n');
    
        //调整格式
        var rgx = /\n(<(([^\?]).+?)(?:\s|\s*?>|\s*?(\/)>)(?:.*?(?:(?:(\/)>)|(?:<(\/)\2>)))?)/mg;
        var nodeStack = [];
        var output = text.replace(rgx,function($0,all,name,isBegin,isCloseFull1,isCloseFull2 ,isFull1,isFull2){
            var isClosed = (isCloseFull1 == '/') || (isCloseFull2 == '/' ) || (isFull1 == '/') || (isFull2 == '/');
            var prefix = '';
            if(isBegin == '!')
            {
                prefix = Utils.getPrefix(nodeStack.length);
            }
            else 
            {
                if(isBegin != '/')
                {
                    prefix = Utils.getPrefix(nodeStack.length);
                    if(!isClosed)
                    {
                        nodeStack.push(name);
                    }
                }
                else
                {
                    nodeStack.pop();
                    prefix = Utils.getPrefix(nodeStack.length);
                }
    
            }
                var ret =  '\n' + prefix + all;
                return ret;
        });
    
        var prefixSpace = -1;
        var outputText = output.substring(1);
    
        //把注释还原并解码,调格式
        outputText = outputText.replace(/\n/g,'\r').replace(/(\s*)<!--(.+?)-->/g,function($0, prefix,  text)
        {
            if(prefix.charAt(0) == '\r')
                prefix = prefix.substring(1);
            text = unescape(text).replace(/\r/g,'\n');
            var ret = '\n' + prefix + '<!--' + text.replace(/^\s*/mg, prefix ) + '-->';
            return ret;
        });
        //alert(outputText);
        
        outputText=	outputText.replace(/\s+$/g,'').replace(/\r/g,'\r\n');
        
        
        return outputText
        
    
    }
    
    static getPrefix(prefixIndex)
    {
        var span = '    ';
        var output = [];
        for(var i = 0 ; i < prefixIndex; ++i)
        {
            output.push(span);
        }
       
        return output.join('');
    }
fukai committed
144
}