mechanical-tolerance-calculator 1.2.2 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.js +37 -15
  2. package/package.json +5 -2
  3. package/test.js +6 -2
package/index.js CHANGED
@@ -538,16 +538,18 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
538
538
  processIndividualMeasurement(camcoTolerances.type, m, camcoTolerances),
539
539
  );
540
540
 
541
- // 4. Determine most common nominal and farthest measurement
541
+ // 4. Determine most common nominal
542
542
  const mostOccuredNominal = findMostOccuredNominal(results);
543
- const mostFarMeasurement = findFarthestMeasurement(
544
- measurements,
545
- mostOccuredNominal,
546
- );
547
543
 
548
544
  // 5. Base spec for the most common nominal
549
545
  const baseSpec = results.find((r) => r.nominal === mostOccuredNominal);
550
546
  const baseITValue = baseSpec.matched_spec[baseSpec.IT_grade];
547
+ // Determine the farthest measurement
548
+ const mostFarMeasurement = findFarthestMeasurement(
549
+ measurements,
550
+ mostOccuredNominal,
551
+ baseSpec.computed_specification_bounds,
552
+ );
551
553
 
552
554
  // 6. Check IT tolerance and spec compliance
553
555
  const { meetsIT, itReason } = checkITTolerance(
@@ -610,16 +612,36 @@ function findMostOccuredNominal(results) {
610
612
  );
611
613
  }
612
614
 
613
- /** Find the measurement farthest from the most common nominal */
614
- function findFarthestMeasurement(measurements, referenceNominal) {
615
- return measurements.reduce(
616
- (farthest, current) =>
617
- Math.abs(current - referenceNominal) >
618
- Math.abs(farthest - referenceNominal)
619
- ? current
620
- : farthest,
621
- measurements[0],
622
- );
615
+ /**
616
+ * Finds the measurement farthest from the nominal that is outside the allowed bounds.
617
+ * @param {number[]} measurements - Array of measurements
618
+ * @param {number} nominal - Reference nominal value
619
+ * @param {object} bounds - Computed bounds { lowerBound, upperBound }
620
+ * @returns {number|null} - The farthest measurement outside bounds, or null if all within bounds
621
+ */
622
+ function findFarthestMeasurement(measurements, nominal, bounds) {
623
+ if (!Array.isArray(measurements) || measurements.length === 0) return null;
624
+
625
+ let farthest = null;
626
+ let maxDistance = -1;
627
+
628
+ measurements.forEach((m) => {
629
+ const isOutside = m < bounds.lowerBound || m > bounds.upperBound;
630
+ if (isOutside) {
631
+ const distance = Math.abs(m - nominal);
632
+ if (distance > maxDistance) {
633
+ maxDistance = distance;
634
+ farthest = m;
635
+ }
636
+ }
637
+ });
638
+
639
+ // If no measurement is outside bounds, return the largest measurement
640
+ if (farthest === null) {
641
+ return Math.max(...measurements);
642
+ }
643
+
644
+ return farthest; // null if all measurements are within bounds
623
645
  }
624
646
 
625
647
  /** Check IT tolerance */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mechanical-tolerance-calculator",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "description": "Calculates international standard specification and tolerances for bores, round bars and metals of mechanical units. For examples; H7, H8, H9, h8, h9 specifications and IT5/IT6 tolerances.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -25,5 +25,8 @@
25
25
  "bugs": {
26
26
  "url": "https://github.com/AjayGhimire1998/Mechanical-Tolerances-Calculator/issues"
27
27
  },
28
- "homepage": "https://github.com/AjayGhimire1998/Mechanical-Tolerances-Calculator#readme"
28
+ "homepage": "https://github.com/AjayGhimire1998/Mechanical-Tolerances-Calculator#readme",
29
+ "dependencies": {
30
+ "mechanical-tolerance-calculator": "^1.2.2"
31
+ }
29
32
  }
package/test.js CHANGED
@@ -4,5 +4,9 @@ const {
4
4
  } = require("./index");
5
5
  // console.log(checkMultipleMeasurementsFor("housing", [100.04, 100.05]));
6
6
  // console.log(checkMultipleMeasurementsFor("housing", [100.04, 100.05, 95.06]));
7
- console.log(checkMultipleMeasurementsFor("housing", [99.99, 100.15, 100.2]));
8
- console.log(checkOneMeasurementFor("shaft", 199.98));
7
+ console.log(
8
+ checkMultipleMeasurementsFor(
9
+ "housing",
10
+ [120, 120.05, 119.98, 119.98, 119.97, 119.98],
11
+ ),
12
+ );