mechanical-tolerance-calculator 1.0.8 → 1.1.0
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.
- package/index.js +49 -21
- 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
|
|
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(
|
|
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 -
|
|
114
|
-
return Math.floor(
|
|
126
|
+
if (standardNominal - validatedMeasurement >= THRESHOLD) {
|
|
127
|
+
return Math.floor(validatedMeasurement);
|
|
115
128
|
}
|
|
116
|
-
return Math.ceil(
|
|
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(
|
|
135
|
+
const standardNominal = Math.floor(validatedMeasurement);
|
|
123
136
|
|
|
124
|
-
return
|
|
125
|
-
? Math.ceil(
|
|
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(
|
|
143
|
+
return Math.round(validatedMeasurement);
|
|
131
144
|
}
|
|
132
145
|
|
|
133
146
|
const MATERIAL_TYPE_CONFIG = {
|
|
@@ -172,7 +185,8 @@ function calculateUncomputedBounds(nominal, spec) {
|
|
|
172
185
|
}
|
|
173
186
|
|
|
174
187
|
function checkMeetsSpecification(measurement, bounds) {
|
|
175
|
-
const
|
|
188
|
+
const validatedMeasurement = validateMeasurement(measurement);
|
|
189
|
+
const measure = parseStringFloat(validatedMeasurement);
|
|
176
190
|
const upper = parseStringFloat(bounds.upperBound);
|
|
177
191
|
const lower = parseStringFloat(bounds.lowerBound);
|
|
178
192
|
|
|
@@ -189,8 +203,12 @@ function processMeasurement(materialType, measurement, tolerances) {
|
|
|
189
203
|
};
|
|
190
204
|
}
|
|
191
205
|
|
|
206
|
+
const validatedMeasurement = validateMeasurement(measurement);
|
|
192
207
|
// Calculate nominal diameter
|
|
193
|
-
const nominal = parseNominalFromMeasurement(
|
|
208
|
+
const nominal = parseNominalFromMeasurement(
|
|
209
|
+
validatedMeasurement,
|
|
210
|
+
materialType
|
|
211
|
+
);
|
|
194
212
|
|
|
195
213
|
// Find matching specification
|
|
196
214
|
const matchedSpec = findMatchingSpec(
|
|
@@ -247,9 +265,10 @@ function processMeasurement(materialType, measurement, tolerances) {
|
|
|
247
265
|
}
|
|
248
266
|
|
|
249
267
|
function processOneMeasurement(materialType, measurement, tolerances) {
|
|
268
|
+
const validatedMeasurement = validateMeasurement(measurement);
|
|
250
269
|
const processedMeasurement = processMeasurement(
|
|
251
270
|
materialType,
|
|
252
|
-
|
|
271
|
+
validatedMeasurement,
|
|
253
272
|
tolerances
|
|
254
273
|
);
|
|
255
274
|
return {
|
|
@@ -265,7 +284,8 @@ function checkOneMeasurementFor(materialType, measurement) {
|
|
|
265
284
|
return camcoStandardTolerances;
|
|
266
285
|
}
|
|
267
286
|
|
|
268
|
-
|
|
287
|
+
const validatedMeasurement = validateMeasurement(measurement);
|
|
288
|
+
if (typeof measurement !== "number" || isNaN(validatedMeasurement)) {
|
|
269
289
|
return {
|
|
270
290
|
error: true,
|
|
271
291
|
message: "Invalid measurement value",
|
|
@@ -274,7 +294,7 @@ function checkOneMeasurementFor(materialType, measurement) {
|
|
|
274
294
|
|
|
275
295
|
return processOneMeasurement(
|
|
276
296
|
camcoStandardTolerances.type,
|
|
277
|
-
|
|
297
|
+
validatedMeasurement,
|
|
278
298
|
camcoStandardTolerances
|
|
279
299
|
);
|
|
280
300
|
}
|
|
@@ -333,9 +353,10 @@ function parseStringFloat(value) {
|
|
|
333
353
|
return isNaN(parsed) ? 0 : parsed;
|
|
334
354
|
}
|
|
335
355
|
function processIndividualMeasurement(materialType, measurement, tolerances) {
|
|
356
|
+
const validatedMeasurement = validateMeasurement(measurement);
|
|
336
357
|
const processedMeasurement = processMeasurement(
|
|
337
358
|
materialType,
|
|
338
|
-
|
|
359
|
+
validatedMeasurement,
|
|
339
360
|
tolerances
|
|
340
361
|
);
|
|
341
362
|
return processedMeasurement;
|
|
@@ -431,27 +452,32 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
|
|
|
431
452
|
}
|
|
432
453
|
|
|
433
454
|
function generateReasonForSpecs(spec, measurement, base1, base2) {
|
|
455
|
+
const validatedMeasurement = validateMeasurement(measurement);
|
|
434
456
|
if (spec === true) {
|
|
435
457
|
return `${parseToFixedThreeString(
|
|
436
|
-
|
|
458
|
+
validatedMeasurement
|
|
437
459
|
)} falls between ${base1} and ${base2}`;
|
|
438
460
|
}
|
|
439
461
|
return `${parseToFixedThreeString(
|
|
440
|
-
|
|
462
|
+
validatedMeasurement
|
|
441
463
|
)} doesn't fall between ${base1} and ${base2}`;
|
|
442
464
|
}
|
|
443
465
|
|
|
444
466
|
function generateReasonForTolerances(spec, measurement1, measurement2, base) {
|
|
467
|
+
const validatedMeasurement1 = validateMeasurement(measurement1);
|
|
468
|
+
const validatedMeasurement2 = validateMeasurement(measurement2);
|
|
445
469
|
if (spec === true) {
|
|
446
470
|
return `The difference between ${parseToFixedThreeString(
|
|
447
|
-
|
|
471
|
+
validatedMeasurement1
|
|
448
472
|
)} and ${parseToFixedThreeString(
|
|
449
|
-
|
|
473
|
+
validatedMeasurement2
|
|
450
474
|
)} is less than or equal to ${base}.`;
|
|
451
475
|
}
|
|
452
476
|
return `The difference between ${parseToFixedThreeString(
|
|
453
|
-
|
|
454
|
-
)} and ${parseToFixedThreeString(
|
|
477
|
+
validatedMeasurement1
|
|
478
|
+
)} and ${parseToFixedThreeString(
|
|
479
|
+
validatedMeasurement2
|
|
480
|
+
)} is greater than ${base}.`;
|
|
455
481
|
}
|
|
456
482
|
|
|
457
483
|
function validateMeasurements(measurements) {
|
|
@@ -466,6 +492,8 @@ function validateMeasurements(measurements) {
|
|
|
466
492
|
error: "Measurements array cannot be empty",
|
|
467
493
|
};
|
|
468
494
|
}
|
|
495
|
+
|
|
496
|
+
measurements.forEach((a) => validateMeasurement(a));
|
|
469
497
|
return null;
|
|
470
498
|
}
|
|
471
499
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mechanical-tolerance-calculator",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
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": {
|