mechanical-tolerance-calculator 1.0.7 → 1.0.9

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 +55 -10
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -96,38 +96,51 @@ function returnTolerancesFor(executableMaterialType, spec = "") {
96
96
  };
97
97
  }
98
98
 
99
+ function isValidMeasurement(measurement) {
100
+ const num = Number(measurement);
101
+ return !isNaN(num) && num >= 0 && num < 1000;
102
+ }
103
+
104
+ function validateMeasurement(measurement) {
105
+ const isMeasurementValid = isValidMeasurement(measurement);
106
+ if (!isMeasurementValid) {
107
+ return { error: "Measurement must be between 0 to 1000." };
108
+ }
109
+ return measurement;
110
+ }
111
+
99
112
  function parseNominalFromMeasurement(
100
113
  measurement,
101
114
  materialType,
102
115
  THRESHOLD = 0.9
103
116
  ) {
104
- const lowerNominal = Math.floor(measurement);
117
+ const validatedMeasurement = validateMeasurement(measurement);
105
118
 
106
119
  // For shafts: upper_deviation is 0, so measurement ≤ nominal
107
120
  // Therefore, nominal must be ceiling of measurement
108
121
  if (materialType === "shafts") {
109
- const standardNominal = Math.ceil(measurement); //a standard shaft will always have measurements less than the nominal
122
+ const standardNominal = Math.ceil(validatedMeasurement); //a standard shaft will always have measurements less than the nominal
110
123
 
111
124
  //however, in some cases, we get shafts going beyond the upper deviation
112
125
  //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);
126
+ if (standardNominal - validatedMeasurement >= THRESHOLD) {
127
+ return Math.floor(validatedMeasurement);
115
128
  }
116
- return Math.ceil(measurement);
129
+ return Math.ceil(validatedMeasurement);
117
130
  }
118
131
 
119
132
  // For bores: lower_deviation is 0, so measurement ≥ nominal
120
133
  // Therefore, nominal must be floor of measurement
121
134
  if (materialType === "housingBores" || materialType === "shellBores") {
122
- const standardNominal = Math.floor(measurement);
135
+ const standardNominal = Math.floor(validatedMeasurement);
123
136
 
124
- return measurement - standardNominal >= THRESHOLD
125
- ? Math.ceil(measurement)
137
+ return validatedMeasurement - standardNominal >= THRESHOLD
138
+ ? Math.ceil(validatedMeasurement)
126
139
  : standardNominal;
127
140
  }
128
141
 
129
142
  // Default: round to nearest
130
- return Math.round(measurement);
143
+ return Math.round(validatedMeasurement);
131
144
  }
132
145
 
133
146
  const MATERIAL_TYPE_CONFIG = {
@@ -221,6 +234,14 @@ function processMeasurement(materialType, measurement, tolerances) {
221
234
  computedBounds.upperBound
222
235
  );
223
236
 
237
+ const outcome =
238
+ measurement > computedBounds.upperBound
239
+ ? `${materialType} is over-sized.`
240
+ : measurement >= computedBounds.lowerBound &&
241
+ measurement <= computedBounds.upperBound
242
+ ? `${materialType} is in acceptable size.`
243
+ : `${materialType} is under-sized.`;
244
+
224
245
  return {
225
246
  measurement: parseStringFloat(measurement),
226
247
  nominal,
@@ -230,7 +251,11 @@ function processMeasurement(materialType, measurement, tolerances) {
230
251
  uncomputed_specification_bounds: uncomputedBounds,
231
252
  matched_spec: matchedSpec,
232
253
 
233
- meets_specification: { meetsSpec, reason: specMeetingReason },
254
+ meets_specification: {
255
+ meetsSpec,
256
+ reason: specMeetingReason,
257
+ concludedReason: outcome,
258
+ },
234
259
  };
235
260
  }
236
261
 
@@ -391,6 +416,24 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
391
416
  baseSpec.computed_specification_bounds.lowerBound,
392
417
  baseSpec.computed_specification_bounds.upperBound
393
418
  );
419
+
420
+ const isOverSized =
421
+ mostFarMeasurement > baseSpec.computed_specification_bounds.upperBound;
422
+ const isWithinSizeRange =
423
+ mostFarMeasurement <= baseSpec.computed_specification_bounds.upperBound &&
424
+ mostFarMeasurement >= baseSpec.computed_specification_bounds.lowerBound;
425
+ const outcome =
426
+ (isWithinSizeRange
427
+ ? `${materialType} is acceptable in size`
428
+ : isOverSized
429
+ ? `${materialType} is over-sized`
430
+ : `${materialType} is under-sized`) +
431
+ (isWithinSizeRange && meetsIT
432
+ ? `, and `
433
+ : !meetsIT && isWithinSizeRange
434
+ ? `, but `
435
+ : `, and `) +
436
+ (meetsIT ? `meets IT tolerance.` : `doesn't meet IT tolerance.`);
394
437
  return {
395
438
  ...baseSpec,
396
439
  measurement: measurements,
@@ -436,6 +479,8 @@ function validateMeasurements(measurements) {
436
479
  error: "Measurements array cannot be empty",
437
480
  };
438
481
  }
482
+
483
+ measurements.forEach((a) => validateMeasurement(a));
439
484
  return null;
440
485
  }
441
486
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mechanical-tolerance-calculator",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
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": {