WMS_MAIN/app/lib/waybill_code.js

36 lines
1.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 运单号公共工具。
* 统一处理一单多件分段单号(如 JDX057941864338-1-1-)的包裹序号后缀剥离与 like 通配符转义,
* 保证「剥离后缀后主体运单号必须等值匹配」的业务规则在各扫描入口保持一致。
*/
/**
* 剥离运单号尾部的包裹序号段(一单多件分段格式:-包裹序号-总件数[-])。
* 例如 'JDX057941864338-1-1-' → 'JDX057941864338';
* 'JDX057941864338' 无分段后缀时原样返回。
*
* @param {string} waybillCode 运单号
* @return {string} 剥离分段后缀后的主体运单号
*/
function stripWaybillSegmentSuffix(waybillCode) {
return String(waybillCode || '').trim().replace(/(?:-\d+){2,}-?$/, '');
}
/**
* 转义 like 匹配中的通配符(% _ \),避免用户输入中的特殊字符扩大匹配范围。
* 用于「主体运单号 + -%」前缀匹配场景,确保主体部分被等值比较。
*
* @param {string} value 待转义的字符串
* @return {string} 转义后的字符串
*/
function escapeLikeWildcards(value) {
return String(value || '').replace(/[%_\\]/g, m => `\\${m}`);
}
module.exports = {
stripWaybillSegmentSuffix,
escapeLikeWildcards,
};