mechanical-tolerance-calculator 1.0.6 → 1.0.7

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 (2) hide show
  1. package/index.js +75 -36
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -96,17 +96,34 @@ function returnTolerancesFor(executableMaterialType, spec = "") {
96
96
  };
97
97
  }
98
98
 
99
- function parseNominalFromMeasurement(measurement, materialType) {
99
+ function parseNominalFromMeasurement(
100
+ measurement,
101
+ materialType,
102
+ THRESHOLD = 0.9
103
+ ) {
104
+ const lowerNominal = Math.floor(measurement);
105
+
100
106
  // For shafts: upper_deviation is 0, so measurement ≤ nominal
101
107
  // Therefore, nominal must be ceiling of measurement
102
108
  if (materialType === "shafts") {
109
+ const standardNominal = Math.ceil(measurement); //a standard shaft will always have measurements less than the nominal
110
+
111
+ //however, in some cases, we get shafts going beyond the upper deviation
112
+ //so, we work with a threshold of 0.10 (meaning, a shaft can only go upto 0.10 of it's upper deviation)
113
+ if (standardNominal - measurement >= THRESHOLD) {
114
+ return Math.floor(measurement);
115
+ }
103
116
  return Math.ceil(measurement);
104
117
  }
105
118
 
106
119
  // For bores: lower_deviation is 0, so measurement ≥ nominal
107
120
  // Therefore, nominal must be floor of measurement
108
121
  if (materialType === "housingBores" || materialType === "shellBores") {
109
- return Math.floor(measurement);
122
+ const standardNominal = Math.floor(measurement);
123
+
124
+ return measurement - standardNominal >= THRESHOLD
125
+ ? Math.ceil(measurement)
126
+ : standardNominal;
110
127
  }
111
128
 
112
129
  // Default: round to nearest
@@ -196,13 +213,13 @@ function processMeasurement(materialType, measurement, tolerances) {
196
213
 
197
214
  // Check if measurement meets specification
198
215
  const meetsSpec = checkMeetsSpecification(measurement, computedBounds);
199
- const specMeetingReason = meetsSpec
200
- ? `${parseToFixedThreeString(measurement)} falls between ${
201
- computedBounds.lowerBound
202
- } and ${computedBounds.upperBound}`
203
- : `${parseToFixedThreeString(measurement)} doesn't fall between ${
204
- computedBounds.lowerBound
205
- } and ${computedBounds.upperBound}`;
216
+
217
+ const specMeetingReason = generateReasonForSpecs(
218
+ meetsSpec,
219
+ measurement,
220
+ computedBounds.lowerBound,
221
+ computedBounds.upperBound
222
+ );
206
223
 
207
224
  return {
208
225
  measurement: parseStringFloat(measurement),
@@ -325,15 +342,26 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
325
342
  largestMeasurement - smallestMeasurement
326
343
  );
327
344
 
345
+ let mostFarMeasurement = largestMeasurement;
328
346
  const nominals = {};
329
347
  let count = 0;
348
+ let withInSpecs = [];
330
349
  const results = measurements.map((measurement) => {
331
350
  const result = processIndividualMeasurement(
332
351
  camcoStandardTolerances.type,
333
352
  measurement,
334
353
  camcoStandardTolerances
335
354
  );
355
+ withInSpecs.push(result.meets_specification.meetsSpec);
336
356
  nominals[result.nominal] = count++;
357
+
358
+ if (
359
+ Math.abs(result.nominal - result.measurement) >
360
+ Math.abs(result.nominal - mostFarMeasurement)
361
+ ) {
362
+ mostFarMeasurement = result.measurement;
363
+ }
364
+
337
365
  return result;
338
366
  });
339
367
 
@@ -342,7 +370,6 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
342
370
  let mostOccuredNominal = Object.keys(nominals).find(
343
371
  (nominal) => nominals[nominal] === countOfMostOccuredNominal
344
372
  );
345
- console.log(mostOccuredNominal);
346
373
 
347
374
  const baseSpec = results.find(
348
375
  (result) => result.nominal === parseInt(mostOccuredNominal)
@@ -350,40 +377,53 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
350
377
  const baseITValue = baseSpec.matched_spec[baseSpec.IT_grade];
351
378
 
352
379
  const meetsIT = ITDifference <= baseITValue;
353
- const itMeetingReason = meetsIT
354
- ? `The difference between ${parseToFixedThreeString(
355
- largestMeasurement
356
- )} and ${parseToFixedThreeString(
357
- smallestMeasurement
358
- )} is less than or equal to ${baseITValue}.`
359
- : `The difference between ${parseToFixedThreeString(
360
- largestMeasurement
361
- )} and ${parseToFixedThreeString(
362
- smallestMeasurement
363
- )} is greater than to ${baseITValue}.`;
364
-
365
- // Check if measurement meets specification
366
- const meetsSpec = checkMeetsSpecification(
380
+ const itMeetingReason = generateReasonForTolerances(
381
+ meetsIT,
367
382
  largestMeasurement,
368
- baseSpec?.computed_specification_bounds
383
+ smallestMeasurement,
384
+ baseITValue
385
+ );
386
+
387
+ const meetsSpec = withInSpecs.every((v) => v === true);
388
+ const specMeetingReason = generateReasonForSpecs(
389
+ meetsSpec,
390
+ mostFarMeasurement,
391
+ baseSpec.computed_specification_bounds.lowerBound,
392
+ baseSpec.computed_specification_bounds.upperBound
369
393
  );
370
- console.log(meetsSpec);
371
-
372
- const specMeetingReason = meetsSpec
373
- ? `${parseToFixedThreeString(baseSpec.measurement)} falls between ${
374
- baseSpec.computed_specification_bounds.lowerBound
375
- } and ${baseSpec.computed_specification_bounds.upperBound}`
376
- : `${parseToFixedThreeString(largestMeasurement)} doesn't fall between ${
377
- baseSpec.computed_specification_bounds.lowerBound
378
- } and ${baseSpec.computed_specification_bounds.upperBound}`;
379
394
  return {
395
+ ...baseSpec,
396
+ measurement: measurements,
380
397
  meets_specification: { meetsSpec, reason: specMeetingReason },
381
398
  meets_IT_Tolerance: { meetsIT, reason: itMeetingReason },
382
399
  meets_final_compliance: meetsIT && baseSpec?.meets_specification?.meetsSpec,
383
- ...baseSpec,
384
400
  };
385
401
  }
386
402
 
403
+ function generateReasonForSpecs(spec, measurement, base1, base2) {
404
+ if (spec === true) {
405
+ return `${parseToFixedThreeString(
406
+ measurement
407
+ )} falls between ${base1} and ${base2}`;
408
+ }
409
+ return `${parseToFixedThreeString(
410
+ measurement
411
+ )} doesn't fall between ${base1} and ${base2}`;
412
+ }
413
+
414
+ function generateReasonForTolerances(spec, measurement1, measurement2, base) {
415
+ if (spec === true) {
416
+ return `The difference between ${parseToFixedThreeString(
417
+ measurement1
418
+ )} and ${parseToFixedThreeString(
419
+ measurement2
420
+ )} is less than or equal to ${base}.`;
421
+ }
422
+ return `The difference between ${parseToFixedThreeString(
423
+ measurement1
424
+ )} and ${parseToFixedThreeString(measurement2)} is greater than ${base}.`;
425
+ }
426
+
387
427
  function validateMeasurements(measurements) {
388
428
  if (!Array.isArray(measurements)) {
389
429
  return {
@@ -402,7 +442,6 @@ function validateMeasurements(measurements) {
402
442
  module.exports = {
403
443
  getAllTolerancesFor,
404
444
  getCamcoStandardTolerancesFor,
405
- parseNominalFromMeasurement,
406
445
  checkOneMeasurementFor,
407
446
  checkMultipleMeasurementsFor,
408
447
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mechanical-tolerance-calculator",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
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": {