mechanical-tolerance-calculator 1.0.6 → 1.0.8

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 +106 -37
  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,21 @@ 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
+ );
223
+
224
+ const outcome =
225
+ measurement > computedBounds.upperBound
226
+ ? `${materialType} is over-sized.`
227
+ : measurement >= computedBounds.lowerBound &&
228
+ measurement <= computedBounds.upperBound
229
+ ? `${materialType} is in acceptable size.`
230
+ : `${materialType} is under-sized.`;
206
231
 
207
232
  return {
208
233
  measurement: parseStringFloat(measurement),
@@ -213,7 +238,11 @@ function processMeasurement(materialType, measurement, tolerances) {
213
238
  uncomputed_specification_bounds: uncomputedBounds,
214
239
  matched_spec: matchedSpec,
215
240
 
216
- meets_specification: { meetsSpec, reason: specMeetingReason },
241
+ meets_specification: {
242
+ meetsSpec,
243
+ reason: specMeetingReason,
244
+ concludedReason: outcome,
245
+ },
217
246
  };
218
247
  }
219
248
 
@@ -325,15 +354,26 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
325
354
  largestMeasurement - smallestMeasurement
326
355
  );
327
356
 
357
+ let mostFarMeasurement = largestMeasurement;
328
358
  const nominals = {};
329
359
  let count = 0;
360
+ let withInSpecs = [];
330
361
  const results = measurements.map((measurement) => {
331
362
  const result = processIndividualMeasurement(
332
363
  camcoStandardTolerances.type,
333
364
  measurement,
334
365
  camcoStandardTolerances
335
366
  );
367
+ withInSpecs.push(result.meets_specification.meetsSpec);
336
368
  nominals[result.nominal] = count++;
369
+
370
+ if (
371
+ Math.abs(result.nominal - result.measurement) >
372
+ Math.abs(result.nominal - mostFarMeasurement)
373
+ ) {
374
+ mostFarMeasurement = result.measurement;
375
+ }
376
+
337
377
  return result;
338
378
  });
339
379
 
@@ -342,7 +382,6 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
342
382
  let mostOccuredNominal = Object.keys(nominals).find(
343
383
  (nominal) => nominals[nominal] === countOfMostOccuredNominal
344
384
  );
345
- console.log(mostOccuredNominal);
346
385
 
347
386
  const baseSpec = results.find(
348
387
  (result) => result.nominal === parseInt(mostOccuredNominal)
@@ -350,40 +389,71 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
350
389
  const baseITValue = baseSpec.matched_spec[baseSpec.IT_grade];
351
390
 
352
391
  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(
392
+ const itMeetingReason = generateReasonForTolerances(
393
+ meetsIT,
367
394
  largestMeasurement,
368
- baseSpec?.computed_specification_bounds
395
+ smallestMeasurement,
396
+ baseITValue
397
+ );
398
+
399
+ const meetsSpec = withInSpecs.every((v) => v === true);
400
+ const specMeetingReason = generateReasonForSpecs(
401
+ meetsSpec,
402
+ mostFarMeasurement,
403
+ baseSpec.computed_specification_bounds.lowerBound,
404
+ baseSpec.computed_specification_bounds.upperBound
369
405
  );
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}`;
406
+
407
+ const isOverSized =
408
+ mostFarMeasurement > baseSpec.computed_specification_bounds.upperBound;
409
+ const isWithinSizeRange =
410
+ mostFarMeasurement <= baseSpec.computed_specification_bounds.upperBound &&
411
+ mostFarMeasurement >= baseSpec.computed_specification_bounds.lowerBound;
412
+ const outcome =
413
+ (isWithinSizeRange
414
+ ? `${materialType} is acceptable in size`
415
+ : isOverSized
416
+ ? `${materialType} is over-sized`
417
+ : `${materialType} is under-sized`) +
418
+ (isWithinSizeRange && meetsIT
419
+ ? `, and `
420
+ : !meetsIT && isWithinSizeRange
421
+ ? `, but `
422
+ : `, and `) +
423
+ (meetsIT ? `meets IT tolerance.` : `doesn't meet IT tolerance.`);
379
424
  return {
425
+ ...baseSpec,
426
+ measurement: measurements,
380
427
  meets_specification: { meetsSpec, reason: specMeetingReason },
381
428
  meets_IT_Tolerance: { meetsIT, reason: itMeetingReason },
382
429
  meets_final_compliance: meetsIT && baseSpec?.meets_specification?.meetsSpec,
383
- ...baseSpec,
384
430
  };
385
431
  }
386
432
 
433
+ function generateReasonForSpecs(spec, measurement, base1, base2) {
434
+ if (spec === true) {
435
+ return `${parseToFixedThreeString(
436
+ measurement
437
+ )} falls between ${base1} and ${base2}`;
438
+ }
439
+ return `${parseToFixedThreeString(
440
+ measurement
441
+ )} doesn't fall between ${base1} and ${base2}`;
442
+ }
443
+
444
+ function generateReasonForTolerances(spec, measurement1, measurement2, base) {
445
+ if (spec === true) {
446
+ return `The difference between ${parseToFixedThreeString(
447
+ measurement1
448
+ )} and ${parseToFixedThreeString(
449
+ measurement2
450
+ )} is less than or equal to ${base}.`;
451
+ }
452
+ return `The difference between ${parseToFixedThreeString(
453
+ measurement1
454
+ )} and ${parseToFixedThreeString(measurement2)} is greater than ${base}.`;
455
+ }
456
+
387
457
  function validateMeasurements(measurements) {
388
458
  if (!Array.isArray(measurements)) {
389
459
  return {
@@ -402,7 +472,6 @@ function validateMeasurements(measurements) {
402
472
  module.exports = {
403
473
  getAllTolerancesFor,
404
474
  getCamcoStandardTolerancesFor,
405
- parseNominalFromMeasurement,
406
475
  checkOneMeasurementFor,
407
476
  checkMultipleMeasurementsFor,
408
477
  };
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.8",
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": {