/**
|
|
* 判断单位是否属于同一系列
|
|
* @param {string} startUnit - 基准单位
|
|
* @param {Array<string>} units - 需要比较的单位数组
|
|
* @returns {boolean} 是否属于同一系列
|
|
*/
|
|
|
|
export const isCommonUnit = (startUnit, units) => {
|
|
if (!startUnit || !Array.isArray(units) || units.length === 0) {
|
|
return false;
|
|
}
|
|
|
|
// 提取单位分子部分(斜杠前的部分)
|
|
const extractNumerator = (unit) => {
|
|
const normalizedUnit = unit
|
|
.replace('ug', 'μg')
|
|
.replace('umol', 'μmol')
|
|
.replace('IU', 'U');
|
|
|
|
if (!normalizedUnit.includes('/')) {
|
|
return null; // 不是浓度单位
|
|
}
|
|
|
|
const [numerator] = normalizedUnit.split('/');
|
|
return numerator;
|
|
};
|
|
|
|
// 标准化分子部分,识别类别
|
|
const classifyNumerator = (numerator) => {
|
|
// 质量单位
|
|
const massUnits = ['mg', 'μg', 'ng', 'pg', 'fg', 'g'];
|
|
if (massUnits.some(massUnit => numerator.includes(massUnit))) {
|
|
return 'mass';
|
|
}
|
|
|
|
// 摩尔单位
|
|
const moleUnits = ['mol', 'mmol', 'μmol', 'nmol', 'pmol'];
|
|
if (moleUnits.some(moleUnit => numerator.includes(moleUnit))) {
|
|
return 'mole';
|
|
}
|
|
|
|
// 酶活性单位
|
|
const enzymeUnits = ['U', 'IU'];
|
|
if (enzymeUnits.some(enzymeUnit => numerator.includes(enzymeUnit))) {
|
|
return 'enzyme';
|
|
}
|
|
|
|
return 'other';
|
|
};
|
|
|
|
// 获取基准单位的分子类别
|
|
const startNumerator = extractNumerator(startUnit);
|
|
if (!startNumerator) {
|
|
console.warn(`基准单位 ${startUnit} 不是有效的浓度单位格式`);
|
|
return false;
|
|
}
|
|
|
|
const startCategory = classifyNumerator(startNumerator);
|
|
|
|
// 检查所有单位
|
|
for (const unit of units) {
|
|
const numerator = extractNumerator(unit);
|
|
|
|
if (!numerator) {
|
|
console.log(`单位 ${unit} 不是有效的浓度单位格式`);
|
|
return false;
|
|
}
|
|
|
|
const category = classifyNumerator(numerator);
|
|
|
|
if (category !== startCategory) {
|
|
console.log(`单位 ${unit} 属于 ${category} 类别,与基准 ${startCategory} 类别不同`);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
};
|