华西海圻ELN前端工程
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.1 KiB

  1. /**
  2. * 判断单位是否属于同一系列
  3. * @param {string} startUnit - 基准单位
  4. * @param {Array<string>} units - 需要比较的单位数组
  5. * @returns {boolean} 是否属于同一系列
  6. */
  7. export const isCommonUnit = (startUnit, units) => {
  8. if (!startUnit || !Array.isArray(units) || units.length === 0) {
  9. return false;
  10. }
  11. // 提取单位分子部分(斜杠前的部分)
  12. const extractNumerator = (unit) => {
  13. const normalizedUnit = unit
  14. .replace('ug', 'μg')
  15. .replace('umol', 'μmol')
  16. .replace('IU', 'U');
  17. if (!normalizedUnit.includes('/')) {
  18. return null; // 不是浓度单位
  19. }
  20. const [numerator] = normalizedUnit.split('/');
  21. return numerator;
  22. };
  23. // 标准化分子部分,识别类别
  24. const classifyNumerator = (numerator) => {
  25. // 质量单位
  26. const massUnits = ['mg', 'μg', 'ng', 'pg', 'fg', 'g'];
  27. if (massUnits.some(massUnit => numerator.includes(massUnit))) {
  28. return 'mass';
  29. }
  30. // 摩尔单位
  31. const moleUnits = ['mol', 'mmol', 'μmol', 'nmol', 'pmol'];
  32. if (moleUnits.some(moleUnit => numerator.includes(moleUnit))) {
  33. return 'mole';
  34. }
  35. // 酶活性单位
  36. const enzymeUnits = ['U', 'IU'];
  37. if (enzymeUnits.some(enzymeUnit => numerator.includes(enzymeUnit))) {
  38. return 'enzyme';
  39. }
  40. return 'other';
  41. };
  42. // 获取基准单位的分子类别
  43. const startNumerator = extractNumerator(startUnit);
  44. if (!startNumerator) {
  45. console.warn(`基准单位 ${startUnit} 不是有效的浓度单位格式`);
  46. return false;
  47. }
  48. const startCategory = classifyNumerator(startNumerator);
  49. // 检查所有单位
  50. for (const unit of units) {
  51. const numerator = extractNumerator(unit);
  52. if (!numerator) {
  53. console.log(`单位 ${unit} 不是有效的浓度单位格式`);
  54. return false;
  55. }
  56. const category = classifyNumerator(numerator);
  57. if (category !== startCategory) {
  58. console.log(`单位 ${unit} 属于 ${category} 类别,与基准 ${startCategory} 类别不同`);
  59. return false;
  60. }
  61. }
  62. return true;
  63. };