dataType.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. export function toMap(data, key, val=undefined, optional = {}) {
  2. const map = {};
  3. // 如果 key 为 null,直接将数组中的每个元素的值都设为 val
  4. if (key === null) {
  5. data.forEach(item => {
  6. // 如果没有传入 val,则将整个 item 作为值
  7. map[item] = val !== undefined ? val : item;
  8. });
  9. return map;
  10. }
  11. // 遍历数据
  12. data.forEach(item => {
  13. // 跳过 key 值为 null、undefined 或空字符串的项
  14. if (item[key] == null || item[key] === '') {
  15. return;
  16. }
  17. // 如果 optional.type 是 String,则处理字符串转换
  18. if (optional.type === String) {
  19. item[key] = item[key] + ''; // 转为字符串
  20. item[key] = item[key].trim(); // 去除空格
  21. }
  22. // 如果没有传入 val,则将 item 本身作为值
  23. map[item[key]] = val !== undefined ? item[val] : item;
  24. });
  25. return map;
  26. }
  27. //字符串分割数组
  28. export function toArray(code) {
  29. // 使用正则表达式分割字符串,去除多余的空白字符或换行符
  30. return code.split(/[,,|\n\r\s]+/).filter(Boolean); // 使用 filter 去除空元素
  31. }
  32. export function barcodeToUpperCase(code){
  33. if(!code) return code
  34. if (/[a-zA-Z]/.test(code)) {
  35. return code.toUpperCase(); // 强制转换为大写字母
  36. }
  37. return code
  38. }