mechanical-tolerance-calculator 1.0.0 → 1.0.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.
Files changed (4) hide show
  1. package/README.md +212 -0
  2. package/Tolerances.json +1536 -0
  3. package/index.js +135 -3
  4. package/package.json +1 -1
package/index.js CHANGED
@@ -1,5 +1,137 @@
1
- function helloNPM() {
2
- return "HELLO NPM";
1
+ const tolerances = require("./Tolerances.json");
2
+
3
+ /**
4
+ * Returns all tolerances for the given material type.
5
+ * @description
6
+ * The function checks the provided material type string to determine
7
+ * which category of tolerances to return. It supports 'housing', 'shaft',
8
+ * and 'shell' as valid material types. If the input is invalid or does not
9
+ * match any known category, an error message is returned.
10
+ * @param {string} materialType
11
+ * @returns {object} An object containing the relevant tolerances or an error message.
12
+ */
13
+ function getAllTolerancesFor(materialType) {
14
+ const validatedMaterialType = validateMaterialType(materialType);
15
+
16
+ const trimmedMaterialType = validatedMaterialType.trim().toLowerCase(); // normalize input
17
+ if (trimmedMaterialType.includes("housing")) {
18
+ // includes to allow variations like "housing bore"
19
+ return returnTolerancesFor("housingBores"); // return relevant tolerances
20
+ } else if (trimmedMaterialType.includes("shaft")) {
21
+ // includes to allow variations like "shaft rod"
22
+ return returnTolerancesFor("shafts"); // return relevant tolerances
23
+ } else if (trimmedMaterialType.includes("shell")) {
24
+ // includes to allow variations like "shell bore"
25
+ return returnTolerancesFor("shellBores"); // return relevant tolerances
26
+ } else {
27
+ return {
28
+ error: `Unknown material type: ${materialType}. Valid types are 'housing', 'shaft', or 'shell'.`, // error for invalid typefac
29
+ };
30
+ }
31
+ }
32
+
33
+ /**
34
+ * Returns Camco Standard specification and tolerances for the given material type.
35
+ * @description
36
+ * The function checks the provided material type string to determine
37
+ * which category of camco standard tolerances to return. It supports 'housing', 'shaft',
38
+ * and 'shell' as valid material types. If the input is invalid or does not
39
+ * match any known category, an error message is returned.
40
+ * @param {string} materialType
41
+ * @returns {object} An object containing the relevant tolerances or an error message.
42
+ */
43
+ function getCamcoStandardTolerancesFor(materialType) {
44
+ const validatedMaterialType = validateMaterialType(materialType);
45
+
46
+ const trimmedMaterialType = validatedMaterialType.trim().toLowerCase();
47
+
48
+ if (trimmedMaterialType.includes("housing")) {
49
+ return returnTolerancesFor("housingBores", "H8");
50
+ } else if (trimmedMaterialType.includes("shell")) {
51
+ return returnTolerancesFor("shellBores", "H9");
52
+ } else if (trimmedMaterialType.includes("shaft")) {
53
+ return returnTolerancesFor("shafts", "h9");
54
+ } else {
55
+ return {
56
+ error: `Unknown material type: ${materialType}. Valid types are 'housing', 'shaft', or 'shell'.`,
57
+ };
58
+ }
59
+ }
60
+
61
+ function validateMaterialType(materialType) {
62
+ if (typeof materialType != "string") {
63
+ return { error: "Material type must be a string." };
64
+ }
65
+
66
+ if (
67
+ materialType == undefined ||
68
+ materialType == null ||
69
+ materialType.trim() === ""
70
+ ) {
71
+ return { error: "Material type is required and cannot be empty." };
72
+ }
73
+
74
+ return materialType;
75
+ }
76
+
77
+ function returnTolerancesFor(executableMaterialType, spec = "") {
78
+ if (spec) {
79
+ const allTolerances = tolerances[executableMaterialType];
80
+ if (!Object.keys(allTolerances).includes(spec)) {
81
+ return {
82
+ error: `Currently available specifications are ${Object.keys(
83
+ allTolerances
84
+ )}`,
85
+ };
86
+ }
87
+ return {
88
+ type: executableMaterialType,
89
+ specification: tolerances[executableMaterialType][spec],
90
+ };
91
+ }
92
+
93
+ return {
94
+ type: executableMaterialType,
95
+ specifications: tolerances[executableMaterialType],
96
+ };
97
+ }
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];
107
+ }
108
+ return parseInt(nominal);
109
+ }
110
+
111
+ function checkOneMeasurementFor(materialType, measurement) {
112
+ 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
+ console.log(nominal);
128
+
129
+ console.log(matchedSpec);
3
130
  }
4
131
 
5
- module.exports = helloNPM;
132
+ module.exports = {
133
+ getAllTolerancesFor,
134
+ getCamcoStandardTolerancesFor,
135
+ parseNominalFromMeasurement,
136
+ checkOneMeasurementFor,
137
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mechanical-tolerance-calculator",
3
- "version": "1.0.0",
3
+ "version": "1.0.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": {