import _ from 'lodash';

export const checkoutType = (item: any) => {
    const toString = Object.prototype.toString;
    const str = toString.call(item);
    const splitItem = str.split(' ')[1];
    return splitItem.replace(']', '').toLowerCase();
};

// 保留两位小数，并千分位分隔
export function thousandSeparatorFixed(num: number, len: number = 2) {
    if (isNaN(num)) {
        return num;
    }
    let newNum = num.toFixed(len);
    return newNum.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}

export function getStrLeng(str: string) {
    return str.replace(/[\u0391-\uFFE5]/g, 'aa').length;
}
/**
 *
 * @param len           随机树位数  默认6位
 * @param type          随机数集类型：n纯数字 l纯小写字母 u纯大写字母 lu大小写字母 默认数字字母
 * @param extra         随机数集扩展  可加其他字符
 * @returns {string}    返回随机字符串
 */

export function getRandom(len = 6, type, extra) {
    let num = '0123456789';
    let letter = 'abcdefghigklmnopqrstuvwxyz';
    let upLetter = letter.toUpperCase();
    let str = num;
    let result = '';
    switch (type) {
        case 'n':
            str = num;
            break;
        case 'l':
            str = letter;
            break;
        case 'u':
            str = upLetter;
            break;
        case 'lu':
            str = letter + upLetter;
            break;
        default:
            str = num + letter + upLetter;
            break;
    }
    if (extra && typeof extra === 'string') {
        str += extra;
    }
    for (let i = 0; i < len; i++) {
        result += str[Math.floor(Math.random() * str.length)]
    }
    return result;
}
