// 中文字符情况下获取达到最大长度时的序号
function getReachMaxLengthIndex(value) {
  var proportion = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
  var maxLength = arguments.length > 2 ? arguments[2] : undefined;
  var currentLen = 0;

  for (var i = 0; i < value.length; i++) {
    var str = value[i];

    if (/[^\x00-\xff]/.test(str)) {
      currentLen += proportion;
    } else {
      currentLen += 1;
    }

    if (currentLen > maxLength) {
      return i;
    }
  }
}

export function getWordLen(value) {
  var proportion = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
  var chineseCalc = arguments.length > 2 ? arguments[2] : undefined;
  if (!value) return 0;
  if (!chineseCalc) return value.length;
  var temp = new Array(proportion).fill('*').join('');
  return value.replace(/[^\x00-\xff]/g, temp).length;
}
export function getChineseCalcValue(value) {
  var proportion = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;
  var maxLength = arguments.length > 2 ? arguments[2] : undefined;
  var newValue = value.slice(0, getReachMaxLengthIndex(value, proportion, maxLength));
  return newValue;
}