
/**数据处理、检查工具 */


/**数据处理 */
/**
 * @description 去除url开头、结尾“/”
 * @param url 地址
 * @returns 
 */
export function removeSlashes (url:String) {
  if("/"===url.substring(0,1)){
    url=url.substring(1,url.length)
  }
  if("/"===url.substring(url.length-1,url.length)){
    url=url.substring(0,url.length-1)
  }
  return url
}

/**
 * @description 将数组中对象数据转换为标准数据，用于下拉框显示
 * @param data 对象数组
 * @param valueShow 是否在标签中显示value
 * @param valueKey 值key，默认id
 * @param labelKey 标签key，默认name
 * @returns 
 */
export function handleSelectData (data:Array<Object>,valueShow:Boolean=true,valueKey:String='id',labelKey:String='name') {
  let arrays=new Array()
  if(undefined===data)
    return arrays;
  for (const d of data) {
    if(typeof d ==='string')
      arrays.push({ value: d, label: d })
    else
      arrays.push({ value: d[valueKey], label: valueShow?('['+d[valueKey]+']'+d[labelKey]):d[labelKey] })
  }
  return arrays
}


/**
 * @description 数字转中文大写
 * @param money 数字
 * @returns 
 */
export function convertNumberToChineseCapital (money:any){
  //汉字的数字
  let cnNums = new Array( "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" );
    //基本单位
    let cnIntRadice = new Array("", "拾", "佰", "仟");
    //对应整数部分扩展单位
    let cnIntUnits = new Array("", "万", "亿", "兆");
    //对应小数部分单位
    let cnDecUnits = new Array("角", "分", "毫", "厘");
    //整数金额时后面跟的字符
    let cnInteger = "整";
    //整型完以后的单位
    let cnIntLast = "元";
    //最大处理的数字
    let maxNum = 999999999999999.9999;
    //金额整数部分
    let integerNum;
    //金额小数部分
    let decimalNum;
    //输出的中文金额字符串
    let chineseStr = "";
    //分离金额后用的数组，预定义
    let parts;
    if (money == "") {
        return "";
    }
    money = parseFloat(money);
    if (money >= maxNum) {
        //超出最大处理数字
        return "";
    }
    if (money == 0) {
        chineseStr = cnNums[0] + cnIntLast + cnInteger;
        return chineseStr;
    }
    //转换为字符串
    money = money.toString();
    if (money.indexOf(".") == -1) {
        integerNum = money;
        decimalNum = "";
    } else {
        parts = money.split(".");
        integerNum = parts[0];
        decimalNum = parts[1].substr(0, 4);
    }
    //获取整型部分转换
    if (parseInt(integerNum, 10) > 0) {
        let zeroCount = 0;
        let IntLen = integerNum.length;
        for (let i = 0; i < IntLen; i++) {
            let n = integerNum.substr(i, 1);
            let p = IntLen - i - 1;
            let q = p / 4;
            let m = p % 4;
            if (n == "0") {
                zeroCount++;
            } else {
                if (zeroCount > 0) {
                    chineseStr += cnNums[0];
                }
                //归零
                zeroCount = 0;
                chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
            }
            if (m == 0 && zeroCount < 4) {
                chineseStr += cnIntUnits[q];
            }
        }
        chineseStr += cnIntLast;
    }
    //小数部分
    if (decimalNum != "") {
        let decLen = decimalNum.length;
        for (let i = 0; i < decLen; i++) {
            let n = decimalNum.substr(i, 1);
            if (n != "0") {
                chineseStr += cnNums[Number(n)] + cnDecUnits[i];
            }
        }
    }
    if (chineseStr == "") {
        chineseStr += cnNums[0] + cnIntLast + cnInteger;
    } else if (decimalNum == "") {
        chineseStr += cnInteger;
    }
    return chineseStr;

}


/**数据检查 */
/**
 * @description 是否是JSON字符串
 * @param data 字符串
 * @returns 
 */
export function isJsonString (value:string) {
  try {
    let temp=JSON.parse(value);
    if('number'===typeof temp)
      return false;
    } catch(e) {
        return false;
    }
  return true;
}

/**
 * @description 判断是否为整数
 * @param value
 * @returns {boolean}
 */
export function isNumber(value: string) {
  const reg = /^-?\d+$/
  return reg.test(value)
}

/**
 * @description 判断是否为浮点数
 * @param value
 * @returns {boolean}
 */
export function isDecimal(value: string) {
  const reg = /^-?\d*\.\d+$/
  return reg.test(value)
}


/**
 * @description 判断是否是大写字母
 * @param value
 * @returns {boolean}
 */
export function isUpperCase(value: string) {
  const reg = /^[A-Z]+$/
  return reg.test(value)
}

/**
 * @description 判断是否是大写字母开头
 * @param value
 * @returns {boolean}
 */
export function isAlphabets(value: string) {
  const reg = /^[A-Za-z]+$/
  return reg.test(value)
}


/**
 * @description 判断是否是手机号
 * @param value
 * @returns {boolean}
 */
export function isPhone(value: string) {
  const reg = /^1[3-9]\d{9}$/
  return reg.test(value)
}

/**
 * @description 判断是否是身份证号(第二代)
 * @param value
 * @returns {boolean}
 */
export function isIdCard(value: string) {
  const reg =/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
  return reg.test(value)
}

/**
 * @description 判断是否是邮箱
 * @param value
 * @returns {boolean}
 */
export function isEmail(value: string) {
  const reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
  return reg.test(value)
}

/**
 * @description 判断是否为邮编
 * @param value
 * @returns {boolean}
 */
export function isPostCode(value: string) {
  let reg = /^[0-9]{6}$/
  return reg.test(value)
}

/**
 * @description 判断是否为传真
 * @param value
 * @returns {boolean}
 */
export function isFax(value: string) {
  // 国家代码(2到3位)-区号(2到3位)-电话号码(7到8位)-分机号(3位)"
  let reg = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/
  return reg.test(value)
}

/**
 * @description 判断是否为固话
 * @param value
 * @returns {boolean}
 */
export function isTel(value: string) {
  const reg = /^(400|800)([0-9\\-]{7,10})|(([0-9]{4}|[0-9]{3})([- ])?)?([0-9]{7,8})(([- 转])*([0-9]{1,4}))?$/
  return reg.test(value)
}










