
/**
 * 判断手机号
 */
function isPoneAvailable(str) {
    var myreg=/^[1][3,4,5,7,8,9][0-9]{9}$/;
    if (!myreg.test(str)) {
        return false;
    } else {
        return true;
    }
}

/**
 * 判断数据是数组还是对象
 */
function isArrayOrObject(data){
    let callType = Object.prototype.toString.call(data)
    let type = false
    if(callType === '[object Array]'){
        type = 'Array'
    }else if(callType  === '[object Object]'){
        type = 'Object'
    }
    return type
}

/**
 * 拷贝对象
 */
function cloneObject(obj){
    let newObj = new Object();
    let keys = Object.keys(obj);
    domap(keys,key=>{
        let item = obj[key];
        let itemCallType = isArrayOrObject(item)
        if(itemCallType === 'Array'){
            let arrItem = cloneArray(item)
            newObj[key] = arrItem
        }else if(itemCallType === 'Object'){
            let objItem = cloneObject(item)
            newObj[key] = objItem
        }else{
            newObj[key] = item
        }
    })
    return newObj
}

/**
 * 拷贝数组
 */
function cloneArray(arr){
    let newArr = new Array();
    domap(arr,item=>{
        let itemCallType = isArrayOrObject(item)
        if(itemCallType === 'Array'){
            let arrItem = cloneArray(item)
            newArr.push(arrItem)
        }else if(itemCallType === 'Object'){
            let objItem = cloneObject(item)
            newArr.push(objItem)
        }else{
            newArr.push(item)
        }
    })
    
    return newArr
}

/**
 * 深度克隆数据
 */
function cloneData(data){
    let dataCallType = isArrayOrObject(data);
    let newData = dataCallType === 'Array' ? cloneArray(data) : cloneObject(data);
    return newData
}

/**
 * 删除数组
 * @param {Array}
 * @param {Number}
 */
function deleteArray(data,index){
    let arr = new Array();
    domap(data,(item,i)=>{
        if(i !== index){
            arr.push(item)
        }
    })
    return arr
}

/**
 * 删除对象
 */
function deleteObject(obj,key){
    let keys = Object.keys(obj);
    let newObj = {};
    domap(keys,k=>{
        if(k!== key){
            newObj[k] = obj[k]
        }
    })
    return newObj
}

/**
 * 监听是否滚动到底部
 */
function getIsScrolltolower(el){
    var isScrolltolower = false;
    
    if(el){
        if(el.scrollTop + el.offsetHeight >= el.scrollHeight){
            isScrolltolower = true
        }
        
    }else{
        if(window.scrollY + window.innerHeight >= document.getElementsByTagName('body')[0].scrollHeight){
            isScrolltolower = true
        }
    }

    return isScrolltolower
}

/*
*获取合适的大小
*containerArea 容器宽高
*contentArea 内容宽高
*/
function getSuitableArea(containerArea,contentArea){
    
    let scale = contentArea.width / contentArea.height,w,h,
        containerScale = containerArea.width / containerArea.height;

    if(scale > containerScale){
        w = containerArea.width;
        h = contentArea.height*(containerArea.width/contentArea.width)
        
    }else{
        h = containerArea.height;
        w = contentArea.width*(containerArea.height/contentArea.height)
    }

    return {
        width:w,
        height:h
    }
}

const letters = [];
for (var i = 65; i < 91; i++) {
    letters.push(String.fromCharCode(i));
}

/**
 * 根据下标返回字母
 * @param {Number}
 */
function getLetter(i) {
    return letters[i]
}
/**
 * 根据子母返回下标
 * @param {Number}
 */
function getLetterIndex(letter){
    var index = 0;
    letters.map((ltr,i)=>{
        if(ltr == letter){
            index = i
        }
    })
    return index
}

/**
 * 获取url参数
 */
function getParams(){
    let locationSearch = location.search
    let params = {}
    if(locationSearch){
        let splitArr = locationSearch.split('?')[1]
        if(splitArr){
            splitArr = splitArr.split('&')
        }
        
        splitArr.map(str=>{
            let strSplit = str.split('=')
            params[strSplit[0]] = strSplit[1]
        })
        
    }
    return params
}
export {
    getIsScrolltolower,
    isArrayOrObject,
    cloneObject,
    cloneArray,
    cloneData,
    deleteArray,
    deleteObject,
    getSuitableArea,
    isPoneAvailable,
    getParams,
    getLetter,
    getLetterIndex
}