// 是否手机端
export const isMobile = () => {
    const stauts = navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i);
    return !!stauts;
}

// 变量的类型是否正确
export const typeCorrect = (value: any, type: 'array' | 'object' = 'array') => {
    // 从原型链上判断类型
    const typeStr = Object.prototype.toString.call(value);
    if (type === 'array') {
        return typeStr === '[object Array]';
    }
}

// 获取路由中的路径，参数等
export const getRoute = () => {
    return window.location;
}

// 表单必填项校验
export const formValidate = (fields: string[], data: any) => {
    for (let i = 0; i < fields.length; i++) {
        const item = fields[i];
        // 字符
        if (typeof data[item] === 'string' && !data[item]) {
            return false;
        }
        // 数组
        if (Array.isArray(data[item]) && !data[item].length) {
            return false;
        }
    }
    return true;
}

// 判断两个对象里的值是否相等
export const isEqual = (object1: any, object2: any) => {
    const keys1 = Object.keys(object1);
    const keys2 = Object.keys(object2);
    return true;
}


// 过滤对象中的空值
export const filterObjectField = (obj: any) => {
    const newObj = {};
    for (const key in obj) {
        if (obj[key] !== '' && obj[key] !== null && obj[key] !== undefined) {
            newObj[key] = obj[key];
        }
    }
    return newObj;
}

// 过滤对象中不要的字段
export const filterObjectFieldByKeys = (obj: any, keys: string[]) => {
    const newObj = {};
    for (const key in obj) {
        if (!keys.includes(key)) {
            newObj[key] = obj[key];
        }
    }
    return newObj;
}

// 合并去重(string数组)
export const mergeArray = (list: string[], value: string) => {

}

/**
 * 将数组中的某一项置顶
 */
export const ArrayItemSticky = (list: any, index: number) => {
    let newlist = [list[index]];
    if (index === 0) {
        return list;
    } else {
        for (let i = 0; i < list.length; i++) {
            if (i !== index) {
                newlist.push(list[i]);
            }
        }
        return newlist;
    }
};

/**
 * 将数组中的某一项移动到指定位置
 * @param list
 * @param oldindex -- 移动之前item的下标
 * @param newIndex -- 需要移动到哪个下标
 */
export const ArraySpecifiedPosition = (
    list: any,
    oldindex: number,
    newIndex: number
) => {
    let obj = list[oldindex];
    // 旧的item去掉
    list.splice(oldindex, 1);
    list.splice(newIndex, 0, obj);
    return list;
};
