mechanical-tolerance-calculator 1.0.3 → 1.0.4

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 +172 -27
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -96,40 +96,185 @@ function returnTolerancesFor(executableMaterialType, spec = "") {
96
96
  };
97
97
  }
98
98
 
99
- function parseNominalFromMeasurement(measurement) {
100
- const nominalString = measurement.toString();
101
- let nominal = "";
102
- for (let index = 0; index < nominalString.length; index++) {
103
- if (nominalString[index] === ".") {
104
- break;
105
- }
106
- nominal += nominalString[index];
99
+ function parseNominalFromMeasurement(measurement, materialType) {
100
+ // For shafts: upper_deviation is 0, so measurement ≤ nominal
101
+ // Therefore, nominal must be ceiling of measurement
102
+ if (materialType === "shafts") {
103
+ return Math.ceil(measurement);
104
+ }
105
+
106
+ // For bores: lower_deviation is 0, so measurement ≥ nominal
107
+ // Therefore, nominal must be floor of measurement
108
+ if (materialType === "housingBores" || materialType === "shellBores") {
109
+ return Math.floor(measurement);
107
110
  }
108
- return parseInt(nominal);
111
+
112
+ // Default: round to nearest
113
+ return Math.round(measurement);
114
+ }
115
+
116
+ const MATERIAL_TYPE_CONFIG = {
117
+ shafts: {
118
+ specification: "h9",
119
+ itGrade: "IT5",
120
+ rangeMatch: (nominal, spec) =>
121
+ nominal > spec.minimum_diameter && nominal <= spec.maximum_diameter,
122
+ },
123
+ housingBores: {
124
+ specification: "H8",
125
+ itGrade: "IT6",
126
+ rangeMatch: (nominal, spec) =>
127
+ nominal >= spec.minimum_diameter && nominal < spec.maximum_diameter,
128
+ },
129
+ shellBores: {
130
+ specification: "H9",
131
+ itGrade: "IT6",
132
+ rangeMatch: (nominal, spec) =>
133
+ nominal >= spec.minimum_diameter && nominal < spec.maximum_diameter,
134
+ },
135
+ };
136
+
137
+ function findMatchingSpec(nominal, specs, rangeMatchFn) {
138
+ return specs.find((spec) => rangeMatchFn(nominal, spec)) || null;
139
+ }
140
+
141
+ function calculateComputedBounds(nominal, spec) {
142
+ return {
143
+ upperBound: parseComputedBound(nominal, spec.upper_deviation, 3),
144
+ lowerBound: parseComputedBound(nominal, spec.lower_deviation, 3),
145
+ };
146
+ }
147
+
148
+ function calculateUncomputedBounds(nominal, spec) {
149
+ return {
150
+ upperBound: parseUncomputedBound(nominal, spec.upper_deviation),
151
+ lowerBound: parseUncomputedBound(nominal, spec.lower_deviation),
152
+ };
153
+ }
154
+
155
+ function checkMeetsSpecification(measurement, bounds) {
156
+ const measure = parseStringFloat(measurement);
157
+ const upper = parseStringFloat(bounds.upperBound);
158
+ const lower = parseStringFloat(bounds.lowerBound);
159
+
160
+ return measure >= lower && measure <= upper;
161
+ }
162
+
163
+ function processMeasurement(materialType, measurement, tolerances) {
164
+ const config = MATERIAL_TYPE_CONFIG[materialType];
165
+
166
+ if (!config) {
167
+ return {
168
+ error: true,
169
+ message: `Unknown material type: ${materialType}`,
170
+ };
171
+ }
172
+
173
+ // Calculate nominal diameter
174
+ const nominal = parseNominalFromMeasurement(measurement, materialType);
175
+
176
+ // Find matching specification
177
+ const matchedSpec = findMatchingSpec(
178
+ nominal,
179
+ tolerances.specification,
180
+ config.rangeMatch
181
+ );
182
+
183
+ if (!matchedSpec) {
184
+ return {
185
+ error: true,
186
+ message: `No specification found for nominal diameter: ${nominal}`,
187
+ nominal,
188
+ };
189
+ }
190
+
191
+ // Calculate bounds
192
+ const computedBounds = calculateComputedBounds(nominal, matchedSpec);
193
+ const uncomputedBounds = calculateUncomputedBounds(nominal, matchedSpec);
194
+
195
+ // Check if measurement meets specification
196
+ const meetsSpec = checkMeetsSpecification(measurement, computedBounds);
197
+
198
+ return {
199
+ measurement: parseStringFloat(measurement),
200
+ nominal,
201
+ specification: config.specification,
202
+ IT_grade: config.itGrade,
203
+ computed_specification_bounds: computedBounds,
204
+ uncomputed_specification_bounds: uncomputedBounds,
205
+ meet_specification: meetsSpec,
206
+ meets_IT_tolerance: meetsSpec,
207
+ matched_spec: matchedSpec,
208
+ };
109
209
  }
110
210
 
111
211
  function checkOneMeasurementFor(materialType, measurement) {
112
212
  const camcoStandardTolerances = getCamcoStandardTolerancesFor(materialType);
113
- let nominal = parseNominalFromMeasurement(measurement);
114
- let matchedSpec = {};
115
- if (camcoStandardTolerances.type === "shafts") {
116
- const shaftNominal = nominal + 1;
117
- const specs = camcoStandardTolerances["specification"];
118
- Array.from(specs).forEach((spec) => {
119
- if (
120
- shaftNominal > spec.minimum_diameter &&
121
- shaftNominal <= spec.maximum_diameter
122
- ) {
123
- matchedSpec = spec;
124
- }
125
- });
126
-
127
- const check = Number(matchedSpec.upper_deviation).toFixed(3);
128
- console.log(check);
213
+
214
+ if (camcoStandardTolerances.error) {
215
+ return camcoStandardTolerances;
129
216
  }
130
- console.log(nominal);
131
217
 
132
- console.log(matchedSpec);
218
+ if (typeof measurement !== "number" || isNaN(measurement)) {
219
+ return {
220
+ error: true,
221
+ message: "Invalid measurement value",
222
+ };
223
+ }
224
+
225
+ return processMeasurement(
226
+ camcoStandardTolerances.type,
227
+ measurement,
228
+ camcoStandardTolerances
229
+ );
230
+ }
231
+
232
+ function parseComputedBound(base, value, decimalCount) {
233
+ return Number(base + parseStringFloat(value)).toFixed(decimalCount);
234
+ }
235
+
236
+ function parseUncomputedBound(value1, value2) {
237
+ if (value2.startsWith("-")) {
238
+ return (
239
+ parseToFixedThreeString(value1) +
240
+ " - " +
241
+ parseToFixedThreeString(value2.slice(1, value2.length))
242
+ );
243
+ }
244
+
245
+ return (
246
+ parseToFixedThreeString(value1) + " + " + parseToFixedThreeString(value2)
247
+ );
248
+ }
249
+
250
+ function parseToFixedThreeString(value) {
251
+ if (typeof value === "number") {
252
+ return value.toFixed(3);
253
+ }
254
+ return value;
255
+ }
256
+
257
+ /**
258
+ * Converts string float values to actual float numbers
259
+ * @param {string} value - The string representation of a float number
260
+ * @returns {number} - The parsed float number
261
+ */
262
+ function parseStringFloat(value) {
263
+ // Handle edge cases
264
+ if (value === null || value === undefined) {
265
+ return 0;
266
+ }
267
+
268
+ // If it's already a number, return it
269
+ if (typeof value === "number") {
270
+ return value;
271
+ }
272
+
273
+ // Convert string to float
274
+ const parsed = parseFloat(value);
275
+
276
+ // Return 0 if parsing fails (NaN)
277
+ return isNaN(parsed) ? 0 : parsed;
133
278
  }
134
279
 
135
280
  module.exports = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mechanical-tolerance-calculator",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
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": {