mechanical-tolerance-calculator 1.0.5 → 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 +92 -45
  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),
@@ -225,7 +242,7 @@ function processOneMeasurement(materialType, measurement, tolerances) {
225
242
  );
226
243
  return {
227
244
  ...processedMeasurement,
228
- meets_IT_tolerance: processedMeasurement.meet_specification,
245
+ meets_IT_tolerance: processedMeasurement.meets_specification.meetsSpec,
229
246
  };
230
247
  }
231
248
 
@@ -303,21 +320,13 @@ function parseStringFloat(value) {
303
320
  // Return 0 if parsing fails (NaN)
304
321
  return isNaN(parsed) ? 0 : parsed;
305
322
  }
306
- function processIndividualMeasurement(
307
- materialType,
308
- measurement,
309
- tolerances,
310
- meetsIT,
311
- ITMeetingReason
312
- ) {
323
+ function processIndividualMeasurement(materialType, measurement, tolerances) {
313
324
  const processedMeasurement = processMeasurement(
314
325
  materialType,
315
326
  measurement,
316
327
  tolerances
317
328
  );
318
- return {
319
- ...processedMeasurement,
320
- };
329
+ return processedMeasurement;
321
330
  }
322
331
 
323
332
  function checkMultipleMeasurementsFor(materialType, measurements) {
@@ -333,48 +342,88 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
333
342
  largestMeasurement - smallestMeasurement
334
343
  );
335
344
 
336
- const ITs = {
337
- IT5: 0,
338
- IT6: 0,
339
- IT7: 0,
340
- IT8: 0,
341
- IT9: 0,
342
- };
343
-
345
+ let mostFarMeasurement = largestMeasurement;
346
+ const nominals = {};
347
+ let count = 0;
348
+ let withInSpecs = [];
344
349
  const results = measurements.map((measurement) => {
345
350
  const result = processIndividualMeasurement(
346
351
  camcoStandardTolerances.type,
347
352
  measurement,
348
353
  camcoStandardTolerances
349
354
  );
350
-
351
- ITs[result.IT_grade]++;
355
+ withInSpecs.push(result.meets_specification.meetsSpec);
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
+ }
352
364
 
353
365
  return result;
354
366
  });
355
367
 
356
- const baseSpec = results[0];
368
+ let countOfMostOccuredNominal = Math.max(...Object.values(nominals));
369
+
370
+ let mostOccuredNominal = Object.keys(nominals).find(
371
+ (nominal) => nominals[nominal] === countOfMostOccuredNominal
372
+ );
373
+
374
+ const baseSpec = results.find(
375
+ (result) => result.nominal === parseInt(mostOccuredNominal)
376
+ );
357
377
  const baseITValue = baseSpec.matched_spec[baseSpec.IT_grade];
358
378
 
359
379
  const meetsIT = ITDifference <= baseITValue;
360
- const itMeetingReason = meetsIT
361
- ? `The difference between ${parseToFixedThreeString(
362
- largestMeasurement
363
- )} and ${parseToFixedThreeString(
364
- smallestMeasurement
365
- )} is less than or equal to ${baseITValue}.`
366
- : `The difference between ${parseToFixedThreeString(
367
- largestMeasurement
368
- )} and ${parseToFixedThreeString(
369
- smallestMeasurement
370
- )} is greater than to ${baseITValue}.`;
380
+ const itMeetingReason = generateReasonForTolerances(
381
+ meetsIT,
382
+ largestMeasurement,
383
+ smallestMeasurement,
384
+ baseITValue
385
+ );
371
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
393
+ );
372
394
  return {
373
- baseSpec,
395
+ ...baseSpec,
396
+ measurement: measurements,
397
+ meets_specification: { meetsSpec, reason: specMeetingReason },
374
398
  meets_IT_Tolerance: { meetsIT, reason: itMeetingReason },
399
+ meets_final_compliance: meetsIT && baseSpec?.meets_specification?.meetsSpec,
375
400
  };
376
401
  }
377
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
+
378
427
  function validateMeasurements(measurements) {
379
428
  if (!Array.isArray(measurements)) {
380
429
  return {
@@ -387,14 +436,12 @@ function validateMeasurements(measurements) {
387
436
  error: "Measurements array cannot be empty",
388
437
  };
389
438
  }
390
-
391
439
  return null;
392
440
  }
393
441
 
394
442
  module.exports = {
395
443
  getAllTolerancesFor,
396
444
  getCamcoStandardTolerancesFor,
397
- parseNominalFromMeasurement,
398
445
  checkOneMeasurementFor,
399
446
  checkMultipleMeasurementsFor,
400
447
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mechanical-tolerance-calculator",
3
- "version": "1.0.5",
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": {