class StringUtil{
    constructor() {
    }
    
    static getQueryString(name){
        let obj = StringUtil.searchToObj(window.location.search);
        return obj[name];
    }
    
    static getQueryObj(){
        let obj = StringUtil.searchToObj(window.location.search);
        return obj;
    }
    
    static searchToObj(search){
        let result = {};
        if(search){
            let index = search.lastIndexOf("?");
            if(index!=-1){
                search = search.substring(index+1, search.length);
            }
            if(search){
                let list = search.split("&");
                if(list.length>0){
                    list.map((item, i)=>{
                        if(item){
                            let arr = item.split("=");
                            if(arr.length>1){
                                result[arr[0]] = arr[1];
                            }
                        }
                    });
                }
            }
        }
        return result;
    }
    
    static objToSearch(obj){
        let list = [];
        if(obj){
            for(let key in obj){
                list.push(key+"="+obj[key]);
            }
        }
        return list.join("&");
    }
    static getSearch(url){
        let result = "";
        if(url){
            let index = url.lastIndexOf("?");
            if(index!=-1){
                result = url.substring(index+1, url.length);
            }
        }
        return result;
    }
    
    static appendSearch(url, search){
        let result = url;
        if(url && search){
            let index = url.lastIndexOf("?");
            if(index!=-1){
                if(StringUtil.isString(search)){
                    search = StringUtil.searchToObj(search);
                }
                let _path = url.substring(0, index);
                let _oldSearch = url.substring(index+1, url.length);
                _oldSearch = StringUtil.searchToObj(_oldSearch);
                search = Object.assign({}, _oldSearch, search);
                result = _path + "?" + StringUtil.objToSearch(search);
            }
            else{
                if(StringUtil.isString(search)){
                    result = url + "?" + search;
                }
                else if(StringUtil.isObject(search)){
                    result = url + "?" + StringUtil.objToSearch(search);
                }
            }
        }
        return result;
    }
    
    static checkValidateMin(str){
        var result = true;
        if(str && (str.indexOf("\\")!=-1 || str.indexOf("\"")!=-1)){
            result = false;
        }
        return result;
    }
    
    static checkValidate(str){
        var result = true;
        if(str && (/[^a-zA-z_0-9\u4e00-\u9fa5]/g.test(str))){
            result = false;
        }
        return result;
    }
    
    static trim(str){
        if(str){
            let newStr = str.replace(/(^\s*)|(\s*$)/g, "");
            return newStr;
        }
        return "";
    }
    
    static ltrim(str){
        if(str){
            let newStr = str.replace(/(^\s*)/g,"");
            return newStr;
        }
        return "";
    }
    
    static rtrim(str){
        if(str){
            let newStr = str.replace(/(\s*$)/g,"");
            return newStr;
        }
        return "";
    }
    
    static getLength(str) {
        let len = 0;
        let strLen = str.length;
        for(let i = 0; i < strLen; i++) {
            let charCode = str.charCodeAt(i);
            if(charCode >= 0 && charCode <= 128) {
                len += 1;
            }else {
                len += 2;
            }
        }
        return len;
    }
    
    static realLength(str){
        let len = 0;
        if(str){
            len = str.replace(/[^\x00-\xff]/g, "**").length; 
        }
        return len;
    }
    
    static realSubstring(str, n){
        if(str){
            if(StringUtil.realLength(str)<=n){
                return str;
            }
            let m = Math.floor(n/2);
            let len = str.length;
            for(let i=m;i<=len;i++){
                let temp = str.substr(0,i);
                let _len = StringUtil.realLength(temp);
                if(_len>n){
                    return str.substr(0,i-1);
                }
            }
        }
        return str;
    }
    
    static isArray(object){
        if(Array.isArray){
            return Array.isArray(object);
        }
        else{
            return object && typeof object === 'object' && Array == object.constructor;
        }
    }
    
    static isString(object){
        return object && typeof object === "string" && String == object.constructor;
    }
    
    static isObject(object){
        return object && typeof object === "object" && Object == object.constructor;
    }
    
    static isFunction(object){
        return object && typeof object === "function" && Function == object.constructor;
    }
}
export default StringUtil;