统一个环境返回值

This commit is contained in:
iqudoo
2026-05-02 11:52:06 +08:00
parent 09b80d6b78
commit 45686d28fc
2 changed files with 35 additions and 20 deletions

2
dist/index.js vendored

File diff suppressed because one or more lines are too long

View File

@@ -20,35 +20,50 @@ function getScanRestartDelay() {
} }
function parseBarcodeString(input) { function parseBarcodeString(input) {
// 常见条形码类型列表(全小写,用于不区分大小写匹配) // 标准化的类型映射表:将各种变体映射到统一标识
const commonBarcodeTypes = [ // 这样即使传入 EAN_13、EAN-13、EAN13 都能匹配成功
"CODE128", "CODE39", "CODE93", "EAN13", "EAN8", "UPC-A", "UPC-E", const normalizedTypeMap = {
"ITF", "ITF14", "CODABAR", "PDF417", "QRCODE", "DATAMATRIX", "AZTEC", // EAN 系列
"EAN-13", "EAN-8", "UPC-A", "UPC-E", "ITF-14", "CODE-128", "CODE-39", "CODE-93" "ean13": true, "ean-13": true, "ean_13": true,
].map(type => type.toLowerCase()); // 全部转为小写便于不区分大小写比较 "ean8": true, "ean-8": true, "ean_8": true,
// 检查输入是否为字符串,确保健壮性 // UPC 系列
"upca": true, "upc-a": true, "upc_a": true,
"upce": true, "upc-e": true, "upc_e": true,
// CODE 系列
"code128": true, "code-128": true, "code_128": true,
"code39": true, "code-39": true, "code_39": true,
"code93": true, "code-93": true, "code_93": true,
// 其他常见类型
"itf": true, "itf14": true, "itf-14": true, "itf_14": true,
"codabar": true,
"pdf417": true, "pdf-417": true, "pdf_417": true,
"qrcode": true, "qr-code": true, "qr_code": true,
"datamatrix": true, "data-matrix": true, "data_matrix": true,
"aztec": true
};
// 健壮性检查
if (typeof input !== 'string') { if (typeof input !== 'string') {
return input; return input;
} }
// 按第一个逗号分割(允许值内部包含逗号) // 按第一个逗号分割
const commaIndex = input.indexOf(','); const commaIndex = input.indexOf(',');
if (commaIndex === -1) { if (commaIndex === -1) {
// 没有逗号,不符合格式,返回原值
return input; return input;
} }
const possibleType = input.substring(0, commaIndex).trim(); const possibleType = input.substring(0, commaIndex).trim();
const value = input.substring(commaIndex + 1).trim(); const value = input.substring(commaIndex + 1).trim();
// 如果类型或值为空,认为不符合规范,返回原值 // 类型或值为空,返回原值
if (possibleType === "" || value === "") { if (possibleType === "" || value === "") {
return input; return input;
} }
// 检查类型是否在常见条形码类型中(不区分大小写 // 标准化类型(转小写,便于匹配
const isKnownType = commonBarcodeTypes.includes(possibleType.toLowerCase()); const normalizedType = possibleType.toLowerCase();
// 检查是否为已知的条形码类型(支持多种分隔符变体)
const isKnownType = normalizedTypeMap.hasOwnProperty(normalizedType);
if (isKnownType) { if (isKnownType) {
// 符合条件,返回条形码值
return value; return value;
} else { } else {
// 不符合条件,返回原字符串
return input; return input;
} }
} }