mechanical-tolerance-calculator 1.1.5 → 1.1.7

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 (4) hide show
  1. package/README.md +186 -113
  2. package/index.js +33 -28
  3. package/package.json +1 -1
  4. package/test.js +6 -0
package/README.md CHANGED
@@ -22,132 +22,205 @@ const { getAllTolerancesFor } = require("mechanical-tolerance-calculator");
22
22
  // ES module
23
23
  // import { getAllTolerancesFor } from "mechanical-tolerance-calculator";
24
24
 
25
- // Example: Housing Bore Toelrances
25
+ // Example: Housing Bore Tolerances
26
26
  const housingTolerances = getAllTolerancesFor("housing");
27
27
  console.log(housingTolerances["housingBoresTolerances"]);
28
+
29
+ // Example: Checking Shaft Toleranace for a Measurement
30
+ const result = checkOneMeasurementFor('shaft', 179.91);
31
+ console.log(result);
32
+
33
+ // Exmaple: Checking Housing Specification Tolerance for a Collection of Measurements
34
+ const result = checkMultipleMeasurementsFor('housing', [240.05, 240.07, 240.09, 240.05, 240.06, 240.02, 240.09]);
35
+ console.log(result);
36
+
28
37
  ```
38
+ # API Documentation
39
+
40
+ This section documents the exported public methods of the **Mechanical Tolerance Calculator** library.
29
41
 
30
42
  ---
31
43
 
32
- ## API EXACPLES
33
-
34
- ### `getAllTolerancesFor(materialType: String)`
35
-
36
- **Parameters**
37
-
38
- - `materialType` (`string`) Type of material to check tolerances for (e.g. `"housing"`, `"shaft"`, `"shell"`).
39
-
40
- **Returns**
41
-
42
- An object with the following shape (example):
43
-
44
- ```json
45
- {
46
- "type": "housing bore" | "shaft" | "shell bore",
47
- "specifications": {
48
- "H6": [
49
- {
50
- "minimum_diameter": 0,
51
- "maximum_diameter": 3,
52
- "upper_deviation": 0.006,
53
- "lower_deviation": 0,
54
- "IT6": 0.006,
55
- "IT5": 0.004
56
- },
57
- {
58
- "minimum_diameter": 3,
59
- "maximum_diameter": 6,
60
- "upper_deviation": 0.008,
61
- "lower_deviation": 0,
62
- "IT6": 0.008,
63
- "IT5": 0.005
64
- }
65
- ],
66
-
67
- "H7": [
68
- {
69
- "minimum_diameter": 0,
70
- "maximum_diameter": 3,
71
- "upper_deviation": 0.01,
72
- "lower_deviation": 0,
73
- "IT7": 0.01,
74
- "IT6": 0.006,
75
- "IT5": 0.004
76
- },
77
- {
78
- "minimum_diameter": 3,
79
- "maximum_diameter": 6,
80
- "upper_deviation": 0.012,
81
- "lower_deviation": 0,
82
- "IT7": 0.012,
83
- "IT6": 0.008,
84
- "IT5": 0.005
85
- },
86
- ]
87
- }
88
- }
44
+ ## getAllTolerancesFor(materialType: String)
45
+
46
+ Returns all available ISO/ANSI tolerance specifications for a given material type.
47
+
48
+ ### Description
49
+ Determines the material category from the provided string and returns the full set of tolerance specifications associated with that category.
50
+ Supported material types include **housing**, **shaft**, and **shell** (case-insensitive and partial matches allowed, e.g. `"housing bore"`).
51
+
52
+ ### Parameters
53
+ - **materialType** (`string`)
54
+ The type of material to retrieve tolerances for.
55
+ Valid values (or substrings):
56
+ - `"housing"`
57
+ - `"shaft"`
58
+ - `"shell"`
59
+
60
+ ### Returns
61
+ - **object**
62
+
63
+ **On success**
64
+ ```json
65
+ {
66
+ "type": "housingBores" | "shafts" | "shellBores",
67
+ "specifications": {
68
+ "H6": [ { ... } ],
69
+ "H7": [ { ... } ],
70
+ "...": [ { ... } ]
71
+ }
72
+ } ```
73
+
74
+ - **On failure**
75
+ ```json
76
+ {
77
+ "error": "Unknown material type: <value>. Valid types are 'housing', 'shaft', or 'shell'."
78
+ }
79
+ ```
80
+
81
+ ### Example
82
+ ```js
83
+ const { getAllTolerancesFor } = require("mechanical-tolerance-calculator");
84
+
85
+ const tolerances = getAllTolerancesFor("housing");
86
+ console.log(tolerances.specifications.H7);
89
87
  ```
90
88
 
91
- - `type`: `"housing bore"` or `"shaft"` or `"shell bore"` inferred from the materialType parameter.
92
- - `specifications`: ISO or ANSI fit/tolerance designation associated with the entry (e.g., H7, h6, etc.).
89
+ ## checkOneMeasurementFor(materialType: String, measurement: Number)
90
+
91
+ Checks whether a single measurement complies with the WA standard tolerance and IT grade for the given material type.
92
+
93
+ ### Description
94
+ - Uses WA standard specifications:
95
+ - Housing → H8 / IT6
96
+ - Shell → H9 / IT6
97
+ - Shaft → h9 / IT5
98
+ - Infers the nominal size from the measurement.
99
+ - Calculates upper and lower bounds.
100
+ - Evaluates whether the measurement meets specification and IT tolerance.
101
+
102
+ ### Parameters
103
+ - **materialType** (`string`)
104
+ The type of material to check tolerance and specification for.
105
+ Valid values (or substrings):
106
+ - `"housing"`
107
+ - `"shaft"`
108
+ - `"shell"`
109
+ - **measurement** (`number`)
110
+ The measured diameter (must be between 0 and 1000).
111
+
112
+ ### Returns
113
+ - **object**
114
+
115
+ **On success**
116
+ ```json
117
+ {
118
+ "measurement": 24.982,
119
+ "nominal": 25,
120
+ "specification": "h9",
121
+ "IT_grade": "IT5",
122
+ "computed_specification_bounds": {
123
+ "upperBound": "25.000",
124
+ "lowerBound": "24.970"
125
+ },
126
+ "uncomputed_specification_bounds": {
127
+ "upperBound": "25.000 + 0.000",
128
+ "lowerBound": "25.000 - 0.030"
129
+ },
130
+ "matched_spec": { ... },
131
+ "meets_specification": {
132
+ "meetsSpec": true,
133
+ "reason": "24.982 falls between 24.970 and 25.000",
134
+ "concludedReason": "shaft is in acceptable size."
135
+ },
136
+ "meets_IT_tolerance": true
137
+ }
138
+ ```
93
139
 
94
- - `maximum_diameter`: The upper bound of the nominal diameter range (in millimetres) for which the tolerance values apply.
95
- - `minimum_diameter`: The upper bound of the nominal diameter range (in millimetres) for which the tolerance values apply.
96
- - `upper_deviation` / `lower_deviation`: The positive / negative deviation limit from the basic size (in millimetres).
97
- - `IT5` / `IT6` / `IT8`, etc: International Tolerance (IT) grades defining standard tolerance magnitudes for each grade level.
140
+ - **On failure**
141
+ ```json
142
+ {
143
+ "error": "Measurement must be between 0 to 1000."
144
+ }
145
+ ```
98
146
 
99
- ---
147
+ ### Example
148
+ ```js
149
+ const { checkOneMeasurementFor } = require("mechanical-tolerance-calculator");
100
150
 
101
- ## Example Output
102
-
103
- ```json
104
- {
105
- "type": "housing bore",
106
- "specifications": {
107
- "H6": [
108
- {
109
- "minimum_diameter": 0,
110
- "maximum_diameter": 3,
111
- "upper_deviation": 0.006,
112
- "lower_deviation": 0,
113
- "IT6": 0.006,
114
- "IT5": 0.004
115
- },
116
- {
117
- "minimum_diameter": 3,
118
- "maximum_diameter": 6,
119
- "upper_deviation": 0.008,
120
- "lower_deviation": 0,
121
- "IT6": 0.008,
122
- "IT5": 0.005
123
- }
124
- ],
125
-
126
- "H7": [
127
- {
128
- "minimum_diameter": 0,
129
- "maximum_diameter": 3,
130
- "upper_deviation": 0.01,
131
- "lower_deviation": 0,
132
- "IT7": 0.01,
133
- "IT6": 0.006,
134
- "IT5": 0.004
135
- },
136
- {
137
- "minimum_diameter": 3,
138
- "maximum_diameter": 6,
139
- "upper_deviation": 0.012,
140
- "lower_deviation": 0,
141
- "IT7": 0.012,
142
- "IT6": 0.008,
143
- "IT5": 0.005
144
- }
145
- ]
146
- }
147
- }
151
+ const result = checkOneMeasurementFor("shaft", 179.91);
152
+ console.log(result.meets_IT_tolerance);
148
153
  ```
149
154
 
150
- ---
155
+ ## checkMultipleMeasurementsFor(materialType: String, measurements: Numbers[])
156
+
157
+ Evaluates multiple measurements against WA standard tolerances and IT limits as a group.
158
+
159
+ ### Description
160
+ - Validates all measurements.
161
+ - Computes:
162
+ - Nominal size distribution
163
+ - Maximum and minimum measurements
164
+ - IT difference (max − min)
165
+ - Determines:
166
+ - Whether all measurements meet specification
167
+ - Whether the IT tolerance band is satisfied
168
+ - Overall compliance status
169
+
170
+ ### Parameters
171
+ - **materialType** (`string`)
172
+ The type of material to check tolerance and specification for.
173
+ Valid values (or substrings):
174
+ - `"housing"`
175
+ - `"shaft"`
176
+ - `"shell"`
177
+ - **measurement** (`number[]`)
178
+ An array of measured diameters (each between 0 and 1000).
179
+
180
+ ### Returns
181
+ - **object**
182
+
183
+ **On success**
184
+ ```json
185
+ {
186
+ "measurement": [24.982, 24.990, 24.975],
187
+ "nominal": 25,
188
+ "specification": "h9",
189
+ "IT_grade": "IT5",
190
+ "computed_specification_bounds": {
191
+ "upperBound": "25.000",
192
+ "lowerBound": "24.970"
193
+ },
194
+ "matched_spec": { ... },
195
+ "meets_specification": {
196
+ "meetsSpec": true,
197
+ "reason": "24.990 falls between 24.970 and 25.000"
198
+ },
199
+ "meets_IT_Tolerance": {
200
+ "meetsIT": true,
201
+ "reason": "The difference between 24.990 and 24.975 is less than or equal to 0.021."
202
+ },
203
+ "meets_final_compliance": true
204
+ }
205
+ ```
206
+
207
+ - **On Validation Error**
208
+ ```json
209
+ {
210
+ "error": "Some measurements are invalid.",
211
+ "details": [
212
+ { "index": 1, "value": -5, "error": "Invalid measurement: must be 0–1000" }
213
+ ]
214
+ }
215
+ ```
216
+
217
+ ### Example
218
+ ```js
219
+ const { checkMultipleMeasurementsFor } = require("mechanical-tolerance-calculator");
220
+
221
+ const result = checkMultipleMeasurementsFor('housing', [240.05, 240.07, 240.09, 240.05, 240.06, 240.02, 240.09]);
222
+ console.log(result.meets_final_compliance);
223
+ ```
151
224
 
152
225
  ## Features
153
226
 
package/index.js CHANGED
@@ -80,7 +80,7 @@ function returnTolerancesFor(executableMaterialType, spec = "") {
80
80
  if (!Object.keys(allTolerances).includes(spec)) {
81
81
  return {
82
82
  error: `Currently available specifications are ${Object.keys(
83
- allTolerances
83
+ allTolerances,
84
84
  )}`,
85
85
  };
86
86
  }
@@ -97,14 +97,14 @@ function returnTolerancesFor(executableMaterialType, spec = "") {
97
97
  }
98
98
 
99
99
  function isValidMeasurement(measurement) {
100
- const num = Number(measurement);
100
+ const num = parseFloat(measurement);
101
101
  return !isNaN(num) && num >= 0 && num < 1000;
102
102
  }
103
103
 
104
104
  function parseNominalFromMeasurement(
105
105
  measurement,
106
106
  materialType,
107
- THRESHOLD = 0.9
107
+ THRESHOLD = 0.9,
108
108
  ) {
109
109
  if (!isValidMeasurement(measurement)) {
110
110
  return { error: "Measurement must be between 0 to 1000." };
@@ -208,7 +208,7 @@ function processMeasurement(materialType, measurement, tolerances) {
208
208
  const matchedSpec = findMatchingSpec(
209
209
  nominal,
210
210
  tolerances.specification,
211
- config.rangeMatch
211
+ config.rangeMatch,
212
212
  );
213
213
 
214
214
  if (!matchedSpec) {
@@ -230,16 +230,16 @@ function processMeasurement(materialType, measurement, tolerances) {
230
230
  meetsSpec,
231
231
  measurement,
232
232
  computedBounds.lowerBound,
233
- computedBounds.upperBound
233
+ computedBounds.upperBound,
234
234
  );
235
235
 
236
236
  const outcome =
237
237
  measurement > computedBounds.upperBound
238
238
  ? `${materialType} is over-sized.`
239
239
  : measurement >= computedBounds.lowerBound &&
240
- measurement <= computedBounds.upperBound
241
- ? `${materialType} is in acceptable size.`
242
- : `${materialType} is under-sized.`;
240
+ measurement <= computedBounds.upperBound
241
+ ? `${materialType} is in acceptable size.`
242
+ : `${materialType} is under-sized.`;
243
243
 
244
244
  return {
245
245
  measurement: parseStringFloat(measurement),
@@ -265,7 +265,7 @@ function processOneMeasurement(materialType, measurement, tolerances) {
265
265
  const processedMeasurement = processMeasurement(
266
266
  materialType,
267
267
  measurement,
268
- tolerances
268
+ tolerances,
269
269
  );
270
270
  return {
271
271
  ...processedMeasurement,
@@ -293,7 +293,7 @@ function checkOneMeasurementFor(materialType, measurement) {
293
293
  return processOneMeasurement(
294
294
  camcoStandardTolerances.type,
295
295
  measurement,
296
- camcoStandardTolerances
296
+ camcoStandardTolerances,
297
297
  );
298
298
  }
299
299
 
@@ -357,7 +357,7 @@ function processIndividualMeasurement(materialType, measurement, tolerances) {
357
357
  const processedMeasurement = processMeasurement(
358
358
  materialType,
359
359
  measurement,
360
- tolerances
360
+ tolerances,
361
361
  );
362
362
  return processedMeasurement;
363
363
  }
@@ -389,7 +389,7 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
389
389
  let largestMeasurement = Math.max(...measurements);
390
390
  let smallestMeasurement = Math.min(...measurements);
391
391
  let ITDifference = parseToFixedThreeString(
392
- largestMeasurement - smallestMeasurement
392
+ largestMeasurement - smallestMeasurement,
393
393
  );
394
394
 
395
395
  let mostFarMeasurement = largestMeasurement;
@@ -400,10 +400,13 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
400
400
  const result = processIndividualMeasurement(
401
401
  camcoStandardTolerances.type,
402
402
  measurement,
403
- camcoStandardTolerances
403
+ camcoStandardTolerances,
404
404
  );
405
405
  withInSpecs.push(result.meets_specification.meetsSpec);
406
- nominals[result.nominal] = count++;
406
+ const nominal = result.nominal;
407
+
408
+ // count occurrences
409
+ nominals[nominal] = (nominals[nominal] || 0) + 1;
407
410
 
408
411
  if (
409
412
  Math.abs(result.nominal - result.measurement) >
@@ -414,15 +417,17 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
414
417
 
415
418
  return result;
416
419
  });
420
+ console.log("nominals: " + JSON.stringify(nominals));
421
+ console.log("within spec: " + withInSpecs);
417
422
 
418
423
  let countOfMostOccuredNominal = Math.max(...Object.values(nominals));
419
424
 
420
425
  let mostOccuredNominal = Object.keys(nominals).find(
421
- (nominal) => nominals[nominal] === countOfMostOccuredNominal
426
+ (nominal) => nominals[nominal] === countOfMostOccuredNominal,
422
427
  );
423
428
 
424
429
  const baseSpec = results.find(
425
- (result) => result.nominal === parseInt(mostOccuredNominal)
430
+ (result) => result.nominal === parseInt(mostOccuredNominal),
426
431
  );
427
432
  const baseITValue = baseSpec.matched_spec[baseSpec.IT_grade];
428
433
 
@@ -431,7 +436,7 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
431
436
  meetsIT,
432
437
  largestMeasurement,
433
438
  smallestMeasurement,
434
- baseITValue
439
+ baseITValue,
435
440
  );
436
441
 
437
442
  const meetsSpec = withInSpecs.every((v) => v === true);
@@ -439,7 +444,7 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
439
444
  meetsSpec,
440
445
  mostFarMeasurement,
441
446
  baseSpec.computed_specification_bounds.lowerBound,
442
- baseSpec.computed_specification_bounds.upperBound
447
+ baseSpec.computed_specification_bounds.upperBound,
443
448
  );
444
449
 
445
450
  const isOverSized =
@@ -451,44 +456,44 @@ function checkMultipleMeasurementsFor(materialType, measurements) {
451
456
  (isWithinSizeRange
452
457
  ? `${materialType} is acceptable in size`
453
458
  : isOverSized
454
- ? `${materialType} is over-sized`
455
- : `${materialType} is under-sized`) +
459
+ ? `${materialType} is over-sized`
460
+ : `${materialType} is under-sized`) +
456
461
  (isWithinSizeRange && meetsIT
457
462
  ? `, and `
458
463
  : !meetsIT && isWithinSizeRange
459
- ? `, but `
460
- : `, and `) +
464
+ ? `, but `
465
+ : `, and `) +
461
466
  (meetsIT ? `meets IT tolerance.` : `doesn't meet IT tolerance.`);
462
467
  return {
463
468
  ...baseSpec,
464
469
  measurement: measurements,
465
470
  meets_specification: { meetsSpec, reason: specMeetingReason },
466
471
  meets_IT_Tolerance: { meetsIT, reason: itMeetingReason },
467
- meets_final_compliance: meetsIT && baseSpec?.meets_specification?.meetsSpec,
472
+ meets_final_compliance: meetsIT === true && meetsSpec === true,
468
473
  };
469
474
  }
470
475
 
471
476
  function generateReasonForSpecs(spec, measurement, base1, base2) {
472
477
  if (spec === true) {
473
478
  return `${parseToFixedThreeString(
474
- measurement
479
+ measurement,
475
480
  )} falls between ${base1} and ${base2}`;
476
481
  }
477
482
  return `${parseToFixedThreeString(
478
- measurement
483
+ measurement,
479
484
  )} doesn't fall between ${base1} and ${base2}`;
480
485
  }
481
486
 
482
487
  function generateReasonForTolerances(spec, measurement1, measurement2, base) {
483
488
  if (spec === true) {
484
489
  return `The difference between ${parseToFixedThreeString(
485
- measurement1
490
+ measurement1,
486
491
  )} and ${parseToFixedThreeString(
487
- measurement2
492
+ measurement2,
488
493
  )} is less than or equal to ${base}.`;
489
494
  }
490
495
  return `The difference between ${parseToFixedThreeString(
491
- measurement1
496
+ measurement1,
492
497
  )} and ${parseToFixedThreeString(measurement2)} is greater than ${base}.`;
493
498
  }
494
499
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mechanical-tolerance-calculator",
3
- "version": "1.1.5",
3
+ "version": "1.1.7",
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 ADDED
@@ -0,0 +1,6 @@
1
+ const { checkMultipleMeasurementsFor } = require("./index");
2
+ console.log(checkMultipleMeasurementsFor("housing", [100.04, 100.05]));
3
+ console.log(checkMultipleMeasurementsFor("housing", [100.04, 100.05, 95.06]));
4
+ console.log(
5
+ checkMultipleMeasurementsFor("housing", [100.04, 100.05, 95.06, 80.09]),
6
+ );