@aliretail/react-materials-components
Version:
89 lines (70 loc) • 2.08 kB
JavaScript
;
exports.__esModule = true;
exports.mergeRegionCodes = void 0;
/**
* 合并地区
* 由于逐级异步加载地区会导致非一级地区在二次打开时上级地区无法被识别,选择上级地区时会出现上级地区和下级tag同时存在
* @param codes 由地区行政编码组成的数组列表
* @return 合并后的地区行政编码的数组列表
*/
var mergeRegionCodes = function mergeRegionCodes(codes) {
if (codes === void 0) {
codes = [];
}
var isProvince = function isProvince(code) {
return code.toString().length === 6 && code % 10000 === 0;
};
var isCity = function isCity(code) {
return code.toString().length === 6 && code % 100 === 0;
};
var calculateRelationship = function calculateRelationship(a, b) {
if (a === b) {
return -1;
}
if (isProvince(a) || isProvince(b)) {
if (b.toString().substr(0, 2) === a.toString().substr(0, 2)) {
return isProvince(a) ? -1 : 1;
}
return 0;
}
if (isCity(a) || isCity(b)) {
if (b.toString().substr(0, 4) === a.toString().substr(0, 4)) {
return isCity(a) ? -1 : 1;
}
return 0;
}
if (a.toString().length !== b.toString().length) {
if (b.toString().substr(0, 6) === a.toString().substr(0, 6)) {
return a.toString().length === 6 ? -1 : 1;
}
return 0;
}
return 0;
};
var result = [];
codes.forEach(function (code) {
var ret = 0;
if (result.length > 0) {
var needToPopCodes = [];
result.forEach(function (rCode) {
var rRet = calculateRelationship(rCode, code);
if (rRet !== 0) {
ret = rRet;
if (rRet === 1) {
needToPopCodes.push(rCode);
}
}
});
if (needToPopCodes.length > 0) {
result = result.filter(function (rCode) {
return needToPopCodes.indexOf(rCode) === -1;
});
}
}
if (ret !== -1) {
result.push(code);
}
});
return result;
};
exports.mergeRegionCodes = mergeRegionCodes;