export const formatItems = (originData, filterObj = {}, prefix) => {
  return Object.keys(originData).reduce((all, item) => {
    if (`${filterObj[item]}` === 'true') {
      return all;
    }
    const key = prefix ? `${prefix}.${item}` : item;
    return {
      ...all,
      [key]: originData[item],
    };
  }, {});
};

// 校验是否有重复
export const hasRepeatedValue = (ary = []) => {
  if (ary.length === 0 || ary.length === 1) return false;
  return new Set(ary).size !== ary.length;
}

// 校验表单'名称',长度限制且不能全是空格
export const validateName = (name, maxLength) => {
  return (value) => {
    // 长度不得超过50,不能全是空格   
    const reg = /[^\s]/g;
    return new Promise((resolve) => {
      if (!value) {
        resolve(`${name}为必填项`);
      }
      if (!reg.test(value)) {
        resolve(`${name}不能为空`);
      }
      if (value.length > maxLength) {
        resolve(`${name}字段字符长度不得超过${maxLength}`);
      } else {
        resolve();
      }
    });
  }
}
