const tools = {}

//存储cookie
tools.setCookie = (name, value) => {
    var Days = 30; /* 设置cookie保存时间 */
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}


//获取cookie
tools.getCookie = name => {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg)) {
        return unescape(arr[2]);
    } else {
        /* 如果没有参数，那么就获取本域下的所有cookie */
        return document.cookie;
    }
}

tools.delObjKey = (obj, key) => {
    if (obj.hasOwnProperty(key)) {
        delete obj[key]
    }
}

tools.deepCopy = (obj) => {
    let temp = obj.constructor === Array ? [] : {}
    for (let val in obj) {
        temp[val] = typeof obj[val] == 'object' ? tools.deepCopy(obj[val]) : obj[val]
    }
    return temp
}

//判断watch监听的值是否相同
tools.isSimilarly = (newValue, oldValue) => {
    if(typeof newValue === typeof oldValue){
        if(newValue === oldValue){
            return true
        } else {
            let flag = false
            try {
                Object.keys(newValue).forEach((item, index, arr) => {
                    if(oldValue.hasOwnProperty(item) && newValue[item] === oldValue[item]){
                        if(index == arr.length-1){
                            flag = true
                        }
                    } else {
                        throw new Error()
                    }
                })
            } catch (error) {
                return false
            }
            return flag
        }
    } else {
        return false
    }
}

export default tools