export function toMap(data, key, val=undefined, optional = {}) { const map = {}; // 如果 key 为 null,直接将数组中的每个元素的值都设为 val if (key === null) { data.forEach(item => { // 如果没有传入 val,则将整个 item 作为值 map[item] = val !== undefined ? val : item; }); return map; } // 遍历数据 data.forEach(item => { // 跳过 key 值为 null、undefined 或空字符串的项 if (item[key] == null || item[key] === '') { return; } // 如果 optional.type 是 String,则处理字符串转换 if (optional.type === String) { item[key] = item[key] + ''; // 转为字符串 item[key] = item[key].trim(); // 去除空格 } // 如果没有传入 val,则将 item 本身作为值 map[item[key]] = val !== undefined ? item[val] : item; }); return map; } //字符串分割数组 export function toArray(code) { // 使用正则表达式分割字符串,去除多余的空白字符或换行符 return code.split(/[,,|\n\r\s]+/).filter(Boolean); // 使用 filter 去除空元素 } export function barcodeToUpperCase(code){ if(!code) return code if (/[a-zA-Z]/.test(code)) { return code.toUpperCase(); // 强制转换为大写字母 } return code }