mechanical-tolerance-calculator 1.1.9 → 1.2.1

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 +66 -30
  2. package/package.json +1 -1
  3. package/test.js +3 -1
package/index.js CHANGED
@@ -379,28 +379,27 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
379
379
  largestMeasurement - smallestMeasurement,
380
380
  );
381
381
 
382
- let mostFarMeasurement = largestMeasurement;
383
382
  const nominals = {};
384
383
  let count = 0;
385
- let withInSpecs = [];
384
+ // let withInSpecs = [];
386
385
  const results = measurements.map((measurement) => {
387
386
  const result = processIndividualMeasurement(
388
387
  camcoStandardTolerances.type,
389
388
  measurement,
390
389
  camcoStandardTolerances,
391
390
  );
392
- withInSpecs.push(result.meets_specification.meetsSpec);
391
+ // withInSpecs.push(result.meets_specification.meetsSpec);
393
392
  const nominal = result.nominal;
394
393
 
395
394
  // count occurrences
396
395
  nominals[nominal] = (nominals[nominal] || 0) + 1;
397
396
 
398
- if (
399
- Math.abs(result.nominal - result.measurement) >
400
- Math.abs(result.nominal - mostFarMeasurement)
401
- ) {
402
- mostFarMeasurement = result.measurement;
403
- }
397
+ // if (
398
+ // Math.abs(result.nominal - result.measurement) >
399
+ // Math.abs(result.nominal - mostFarMeasurement)
400
+ // ) {
401
+ // mostFarMeasurement = result.measurement;
402
+ // }
404
403
 
405
404
  return result;
406
405
  });
@@ -410,6 +409,12 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
410
409
  let mostOccuredNominal = Object.keys(nominals).find(
411
410
  (nominal) => nominals[nominal] === countOfMostOccuredNominal,
412
411
  );
412
+ let mostFarMeasurement = measurements.reduce((farthest, current) => {
413
+ return Math.abs(current - mostOccuredNominal) >
414
+ Math.abs(farthest - mostOccuredNominal)
415
+ ? current
416
+ : farthest;
417
+ });
413
418
 
414
419
  const baseSpec = results.find(
415
420
  (result) => result.nominal === parseInt(mostOccuredNominal),
@@ -422,14 +427,23 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
422
427
  largestMeasurement,
423
428
  smallestMeasurement,
424
429
  baseITValue,
430
+ baseSpec.IT_grade,
425
431
  );
426
432
 
427
- const meetsSpec = withInSpecs.every((v) => v === true);
433
+ const meetsSpec = results.every((r) => {
434
+ const value = r.measurement;
435
+ return (
436
+ value >= baseSpec.computed_specification_bounds.lowerBound &&
437
+ value <= baseSpec.computed_specification_bounds.upperBound
438
+ );
439
+ });
440
+
428
441
  const specMeetingReason = generateReasonForSpecs(
429
442
  meetsSpec,
430
443
  mostFarMeasurement,
431
444
  baseSpec.computed_specification_bounds.lowerBound,
432
445
  baseSpec.computed_specification_bounds.upperBound,
446
+ baseSpec.specification,
433
447
  );
434
448
 
435
449
  const isOverSized =
@@ -437,50 +451,72 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
437
451
  const isWithinSizeRange =
438
452
  mostFarMeasurement <= baseSpec.computed_specification_bounds.upperBound &&
439
453
  mostFarMeasurement >= baseSpec.computed_specification_bounds.lowerBound;
440
- const outcome =
441
- (isWithinSizeRange
442
- ? `${materialType} is acceptable in size`
443
- : isOverSized
444
- ? `${materialType} is over-sized`
445
- : `${materialType} is under-sized`) +
446
- (isWithinSizeRange && meetsIT
447
- ? `, and `
448
- : !meetsIT && isWithinSizeRange
449
- ? `, but `
450
- : `, and `) +
451
- (meetsIT ? `meets IT tolerance.` : `doesn't meet IT tolerance.`);
454
+
455
+ const outcome1 = isWithinSizeRange
456
+ ? `${materialType} is acceptable in size.`
457
+ : isOverSized
458
+ ? `${materialType} is over-sized.`
459
+ : `${materialType} is under-sized.`;
460
+
461
+ const outcome2 =
462
+ isWithinSizeRange && meetsIT
463
+ ? "And, it meets IT tolerance."
464
+ : !isWithinSizeRange && meetsIT
465
+ ? "However, it meets IT tolerance."
466
+ : `${!isWithinSizeRange ? "And, " : "But, "}it fails IT tolerance.`;
467
+
468
+ // const outcome2 = meetsIT ? ""
469
+
470
+ // (isWithinSizeRange && meetsIT
471
+ // ? `, and `
472
+ // : !meetsIT && isWithinSizeRange
473
+ // ? `, but `
474
+ // : `, and `) +
475
+ // (meetsIT ? `meets IT tolerance.` : `doesn't meet IT tolerance.`);
476
+
477
+ const final_compliance = meetsIT === true && meetsSpec === true;
478
+
479
+ const outcome3 = final_compliance
480
+ ? "Finally, it meets final compliance and is acceptable to use."
481
+ : "Finally, it doesn't meet final compliance and is not acceptable to use.";
452
482
  return {
453
483
  ...baseSpec,
454
484
  measurement: measurements,
455
485
  meets_specification: { meetsSpec, reason: specMeetingReason },
456
486
  meets_IT_Tolerance: { meetsIT, reason: itMeetingReason },
457
- meets_final_compliance: meetsIT === true && meetsSpec === true,
458
- generalized_outcome: outcome,
487
+ meets_final_compliance: final_compliance,
488
+ generalized_outcome: outcome1 + " " + outcome2 + " " + outcome3,
459
489
  };
460
490
  }
461
491
 
462
- function generateReasonForSpecs(spec, measurement, base1, base2) {
492
+ function generateReasonForSpecs(spec, measurement, base1, base2, specType) {
463
493
  if (spec === true) {
464
494
  return `${parseToFixedThreeString(
465
495
  measurement,
466
- )} falls between ${base1} and ${base2}`;
496
+ )} falls between ${base1} and ${base2}. So, the material meets ${specType} specification.`;
467
497
  }
468
498
  return `${parseToFixedThreeString(
469
499
  measurement,
470
- )} doesn't fall between ${base1} and ${base2}`;
500
+ )} doesn't fall between ${base1} and ${base2}. So, the material doesn't meet ${specType} specification.`;
471
501
  }
472
502
 
473
- function generateReasonForTolerances(spec, measurement1, measurement2, base) {
503
+ function generateReasonForTolerances(
504
+ spec,
505
+ measurement1,
506
+ measurement2,
507
+ base,
508
+ toleranceType,
509
+ ) {
474
510
  if (spec === true) {
475
511
  return `The difference between ${parseToFixedThreeString(
476
512
  measurement1,
477
513
  )} and ${parseToFixedThreeString(
478
514
  measurement2,
479
- )} is less than or equal to ${base}.`;
515
+ )} is less than or equal to ${base}. So, it meets ${toleranceType} Tolerance.`;
480
516
  }
481
517
  return `The difference between ${parseToFixedThreeString(
482
518
  measurement1,
483
- )} and ${parseToFixedThreeString(measurement2)} is greater than ${base}.`;
519
+ )} and ${parseToFixedThreeString(measurement2)} is greater than ${base}. So, it doesn't meet ${toleranceType} Tolerance.`;
484
520
  }
485
521
 
486
522
  function validateMeasurements(measurements) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mechanical-tolerance-calculator",
3
- "version": "1.1.9",
3
+ "version": "1.2.1",
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": {
package/test.js CHANGED
@@ -1,4 +1,6 @@
1
1
  const { checkMultipleMeasurementsFor } = require("./index");
2
2
  // console.log(checkMultipleMeasurementsFor("housing", [100.04, 100.05]));
3
3
  // console.log(checkMultipleMeasurementsFor("housing", [100.04, 100.05, 95.06]));
4
- console.log(checkMultipleMeasurementsFor("shaft", [98.99, 98.98]));
4
+ console.log(
5
+ checkMultipleMeasurementsFor("housing", [100, 32, 32, 1, 100, 100]),
6
+ );