mechanical-tolerance-calculator 1.1.0 → 1.1.2
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 +43 -36
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -114,33 +114,34 @@ function parseNominalFromMeasurement(
|
|
|
114
114
|
materialType,
|
|
115
115
|
THRESHOLD = 0.9
|
|
116
116
|
) {
|
|
117
|
-
|
|
118
|
-
|
|
117
|
+
if (!isValidMeasurement(measurement)) {
|
|
118
|
+
return { error: "Measurement must be between 0 to 1000." };
|
|
119
|
+
}
|
|
119
120
|
// For shafts: upper_deviation is 0, so measurement ≤ nominal
|
|
120
121
|
// Therefore, nominal must be ceiling of measurement
|
|
121
122
|
if (materialType === "shafts") {
|
|
122
|
-
const standardNominal = Math.ceil(
|
|
123
|
+
const standardNominal = Math.ceil(measurement); //a standard shaft will always have measurements less than the nominal
|
|
123
124
|
|
|
124
125
|
//however, in some cases, we get shafts going beyond the upper deviation
|
|
125
126
|
//so, we work with a threshold of 0.10 (meaning, a shaft can only go upto 0.10 of it's upper deviation)
|
|
126
|
-
if (standardNominal -
|
|
127
|
-
return Math.floor(
|
|
127
|
+
if (standardNominal - measurement >= THRESHOLD) {
|
|
128
|
+
return Math.floor(measurement);
|
|
128
129
|
}
|
|
129
|
-
return Math.ceil(
|
|
130
|
+
return Math.ceil(measurement);
|
|
130
131
|
}
|
|
131
132
|
|
|
132
133
|
// For bores: lower_deviation is 0, so measurement ≥ nominal
|
|
133
134
|
// Therefore, nominal must be floor of measurement
|
|
134
135
|
if (materialType === "housingBores" || materialType === "shellBores") {
|
|
135
|
-
const standardNominal = Math.floor(
|
|
136
|
+
const standardNominal = Math.floor(measurement);
|
|
136
137
|
|
|
137
|
-
return
|
|
138
|
-
? Math.ceil(
|
|
138
|
+
return measurement - standardNominal >= THRESHOLD
|
|
139
|
+
? Math.ceil(measurement)
|
|
139
140
|
: standardNominal;
|
|
140
141
|
}
|
|
141
142
|
|
|
142
143
|
// Default: round to nearest
|
|
143
|
-
return Math.round(
|
|
144
|
+
return Math.round(measurement);
|
|
144
145
|
}
|
|
145
146
|
|
|
146
147
|
const MATERIAL_TYPE_CONFIG = {
|
|
@@ -185,8 +186,10 @@ function calculateUncomputedBounds(nominal, spec) {
|
|
|
185
186
|
}
|
|
186
187
|
|
|
187
188
|
function checkMeetsSpecification(measurement, bounds) {
|
|
188
|
-
|
|
189
|
-
|
|
189
|
+
if (!isValidMeasurement(measurement)) {
|
|
190
|
+
return { error: "Measurement must be between 0 to 1000." };
|
|
191
|
+
}
|
|
192
|
+
const measure = parseStringFloat(measurement);
|
|
190
193
|
const upper = parseStringFloat(bounds.upperBound);
|
|
191
194
|
const lower = parseStringFloat(bounds.lowerBound);
|
|
192
195
|
|
|
@@ -194,6 +197,9 @@ function checkMeetsSpecification(measurement, bounds) {
|
|
|
194
197
|
}
|
|
195
198
|
|
|
196
199
|
function processMeasurement(materialType, measurement, tolerances) {
|
|
200
|
+
if (!isValidMeasurement(measurement)) {
|
|
201
|
+
return { error: "Measurement must be between 0 to 1000." };
|
|
202
|
+
}
|
|
197
203
|
const config = MATERIAL_TYPE_CONFIG[materialType];
|
|
198
204
|
|
|
199
205
|
if (!config) {
|
|
@@ -203,12 +209,8 @@ function processMeasurement(materialType, measurement, tolerances) {
|
|
|
203
209
|
};
|
|
204
210
|
}
|
|
205
211
|
|
|
206
|
-
const validatedMeasurement = validateMeasurement(measurement);
|
|
207
212
|
// Calculate nominal diameter
|
|
208
|
-
const nominal = parseNominalFromMeasurement(
|
|
209
|
-
validatedMeasurement,
|
|
210
|
-
materialType
|
|
211
|
-
);
|
|
213
|
+
const nominal = parseNominalFromMeasurement(measurement, materialType);
|
|
212
214
|
|
|
213
215
|
// Find matching specification
|
|
214
216
|
const matchedSpec = findMatchingSpec(
|
|
@@ -265,10 +267,12 @@ function processMeasurement(materialType, measurement, tolerances) {
|
|
|
265
267
|
}
|
|
266
268
|
|
|
267
269
|
function processOneMeasurement(materialType, measurement, tolerances) {
|
|
268
|
-
|
|
270
|
+
if (!isValidMeasurement(measurement)) {
|
|
271
|
+
return { error: "Measurement must be between 0 to 1000." };
|
|
272
|
+
}
|
|
269
273
|
const processedMeasurement = processMeasurement(
|
|
270
274
|
materialType,
|
|
271
|
-
|
|
275
|
+
measurement,
|
|
272
276
|
tolerances
|
|
273
277
|
);
|
|
274
278
|
return {
|
|
@@ -278,14 +282,16 @@ function processOneMeasurement(materialType, measurement, tolerances) {
|
|
|
278
282
|
}
|
|
279
283
|
|
|
280
284
|
function checkOneMeasurementFor(materialType, measurement) {
|
|
285
|
+
if (!isValidMeasurement(measurement)) {
|
|
286
|
+
return { error: "Measurement must be between 0 to 1000." };
|
|
287
|
+
}
|
|
281
288
|
const camcoStandardTolerances = getCamcoStandardTolerancesFor(materialType);
|
|
282
289
|
|
|
283
290
|
if (camcoStandardTolerances.error) {
|
|
284
291
|
return camcoStandardTolerances;
|
|
285
292
|
}
|
|
286
293
|
|
|
287
|
-
|
|
288
|
-
if (typeof measurement !== "number" || isNaN(validatedMeasurement)) {
|
|
294
|
+
if (typeof measurement !== "number" || isNaN(measurement)) {
|
|
289
295
|
return {
|
|
290
296
|
error: true,
|
|
291
297
|
message: "Invalid measurement value",
|
|
@@ -294,7 +300,7 @@ function checkOneMeasurementFor(materialType, measurement) {
|
|
|
294
300
|
|
|
295
301
|
return processOneMeasurement(
|
|
296
302
|
camcoStandardTolerances.type,
|
|
297
|
-
|
|
303
|
+
measurement,
|
|
298
304
|
camcoStandardTolerances
|
|
299
305
|
);
|
|
300
306
|
}
|
|
@@ -353,10 +359,12 @@ function parseStringFloat(value) {
|
|
|
353
359
|
return isNaN(parsed) ? 0 : parsed;
|
|
354
360
|
}
|
|
355
361
|
function processIndividualMeasurement(materialType, measurement, tolerances) {
|
|
356
|
-
|
|
362
|
+
if (!isValidMeasurement(measurement)) {
|
|
363
|
+
return { error: "Measurement must be between 0 to 1000." };
|
|
364
|
+
}
|
|
357
365
|
const processedMeasurement = processMeasurement(
|
|
358
366
|
materialType,
|
|
359
|
-
|
|
367
|
+
measurement,
|
|
360
368
|
tolerances
|
|
361
369
|
);
|
|
362
370
|
return processedMeasurement;
|
|
@@ -452,32 +460,27 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
|
|
|
452
460
|
}
|
|
453
461
|
|
|
454
462
|
function generateReasonForSpecs(spec, measurement, base1, base2) {
|
|
455
|
-
const validatedMeasurement = validateMeasurement(measurement);
|
|
456
463
|
if (spec === true) {
|
|
457
464
|
return `${parseToFixedThreeString(
|
|
458
|
-
|
|
465
|
+
measurement
|
|
459
466
|
)} falls between ${base1} and ${base2}`;
|
|
460
467
|
}
|
|
461
468
|
return `${parseToFixedThreeString(
|
|
462
|
-
|
|
469
|
+
measurement
|
|
463
470
|
)} doesn't fall between ${base1} and ${base2}`;
|
|
464
471
|
}
|
|
465
472
|
|
|
466
473
|
function generateReasonForTolerances(spec, measurement1, measurement2, base) {
|
|
467
|
-
const validatedMeasurement1 = validateMeasurement(measurement1);
|
|
468
|
-
const validatedMeasurement2 = validateMeasurement(measurement2);
|
|
469
474
|
if (spec === true) {
|
|
470
475
|
return `The difference between ${parseToFixedThreeString(
|
|
471
|
-
|
|
476
|
+
measurement1
|
|
472
477
|
)} and ${parseToFixedThreeString(
|
|
473
|
-
|
|
478
|
+
measurement2
|
|
474
479
|
)} is less than or equal to ${base}.`;
|
|
475
480
|
}
|
|
476
481
|
return `The difference between ${parseToFixedThreeString(
|
|
477
|
-
|
|
478
|
-
)} and ${parseToFixedThreeString(
|
|
479
|
-
validatedMeasurement2
|
|
480
|
-
)} is greater than ${base}.`;
|
|
482
|
+
measurement1
|
|
483
|
+
)} and ${parseToFixedThreeString(measurement2)} is greater than ${base}.`;
|
|
481
484
|
}
|
|
482
485
|
|
|
483
486
|
function validateMeasurements(measurements) {
|
|
@@ -493,7 +496,11 @@ function validateMeasurements(measurements) {
|
|
|
493
496
|
};
|
|
494
497
|
}
|
|
495
498
|
|
|
496
|
-
measurements.forEach((a) =>
|
|
499
|
+
measurements.forEach((a) => {
|
|
500
|
+
if (!isValidMeasurement(a)) {
|
|
501
|
+
return { error: "Measurement must be between 0 to 1000." };
|
|
502
|
+
}
|
|
503
|
+
});
|
|
497
504
|
return null;
|
|
498
505
|
}
|
|
499
506
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mechanical-tolerance-calculator",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
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": {
|