Skip to content

Commit 09d173e

Browse files
authored
Consolidate calculator logic (#10)
* Consolidate calculator logic, fix input labels * code formatting * Allow users to input floats instead of only whole numbers * Make inch measurements show up as fractions.
1 parent 5342898 commit 09d173e

File tree

9 files changed

+186
-366
lines changed

9 files changed

+186
-366
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,4 @@ $RECYCLE.BIN/
169169

170170
# End of https://www.gitignore.io/api/git,windows,notepadpp,visualstudiocode,python
171171

172+
.idea

assets/js/cinchsack.js

Lines changed: 37 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -4,101 +4,40 @@
44
Calculate fabric pattern sizes for rectangular bottom stuff sack
55
*/
66

7-
function roundToEigthInch (number) {
8-
var number = (Math.round(number * 8) / 8);
9-
return number;
10-
}
11-
12-
function patternSize(units,bL,bW,h) {
13-
if (units==1) { // METRIC
14-
var csA = 4; // cord channel height (4cm)
15-
var hemA = 1; // single fold hem allowance on each end of channel (1 cm)
16-
var sA = 1; // general seam allowance (1 cm)
17-
}
18-
else { // IMPERIAL
19-
var csA = 1.5; // cord channel height (1.5")
20-
var hemA = 0.5; // single fold hem allowance on each end of channel (0.5")
21-
var sA = 0.5; // general seam allowance (0.5")
22-
}
23-
24-
25-
// body fabric panel
26-
var fabricL = 2*bL + (2*bW) + (2*sA);
27-
28-
if (bW > (2.5*csA)) { // add material so top will close when bW is much wider than channel
29-
var fabricH = h + bW - csA + (2*sA); // adds (1/2bW - csA) to top of fabricH to make up additional width
30-
}
31-
else {
32-
var fabricH = h + (0.5*bW)+ (2*sA); // 1/2bW is the corner cut off bottom to make rectangular
33-
}
34-
35-
// cord channel
36-
var channelL = 2*bL + (2*bW) + (2*hemA); //hem on each end
37-
var channelH = 2*csA + (2*sA); // SA on top and bottom
38-
39-
// rounding dimensions for output
40-
if (units==1) {
41-
var fabricL = fabricL.toFixed(1); //trimming to closest mm
42-
var fabricH = fabricH.toFixed(1);
43-
var channelL = channelL.toFixed(1);
44-
var channelH = channelH.toFixed(1);
45-
}
46-
else {
47-
var fabricL = roundToEigthInch(fabricL); //rounding to nearest 1/8" increment
48-
var fabricH = roundToEigthInch(fabricH);
49-
var channelL = roundToEigthInch(channelL);
50-
var channelH = roundToEigthInch(channelH);
51-
}
52-
53-
return [fabricL, fabricH, channelL, channelH, sA];
54-
55-
};
56-
57-
58-
59-
$('document').ready(function () {
60-
61-
var version = "v0.1"; // initial version
62-
$('.version').html(version);
63-
64-
65-
$('.dimension').change(function () { //when any .dimension changes (input loses focus), function runs
66-
67-
var units = $( "input[type=radio][name=units]:checked" ).val(); // inches (val=0) or cm (val=1)
68-
69-
// assign variables based on input id values from html form.
70-
//Ensure object is number otherwise arithmetic is screwy
71-
var bottomLength = Number( $('#bottomLength').val() );
72-
var bottomWidth = Number( $('#bottomWidth').val() );
73-
var sackHeight = Number( $('#height').val() );
74-
75-
var halfBottomWidth = bottomWidth / 2;
76-
77-
if (bottomLength > 1 && bottomWidth > 0 && sackHeight > 1) { // html input min=1, crappy validation but works
78-
var scrap = patternSize(units, bottomLength, bottomWidth, sackHeight);
79-
var fabricL = scrap[0];
80-
var fabricH = scrap[1];
81-
var channelL = scrap[2];
82-
var channelH = scrap[3];
83-
if (units == 1) {
84-
var unitText = " cm";
85-
var sA = scrap[4];
86-
}
87-
else {
88-
var unitText = " in";
89-
var sA = scrap[4];
90-
}
91-
92-
// html id returns
93-
$('.patternLength').html(fabricL + unitText);
94-
$('.patternHeight').html(fabricH + unitText);
95-
$('.channelLength').html(channelL + unitText);
96-
$('.channelHeight').html(channelH + unitText);
97-
$('.sA').html(sA + unitText);
98-
$('.bottomWidth').html(bottomWidth + unitText);
99-
$('.halfBottomWidth').html(halfBottomWidth + unitText);
100-
$('.bottomLength').html(bottomLength + unitText);
101-
102-
}
103-
});
104-
});
7+
calculatorSetup(function () {
8+
let isMetric = getIsMetric();
9+
let fields = ['#bottomLength', '#bottomWidth', '#height'].map(getNumberFromField);
10+
11+
if (fields.some(field => field <= 0)) {
12+
return;
13+
}
14+
let [bottomLength, bottomWidth, height] = fields;
15+
16+
const { cordChannelHeight, hem, sA } = isMetric ?
17+
{ cordChannelHeight: 4, hem: 1, sA: 1 } :
18+
{ cordChannelHeight: 1.5, hem: 0.5, sA: 0.5 };
19+
20+
let patternHeight;
21+
22+
// add material so top will close when bW is much wider than channel
23+
if (bottomWidth > (2.5 * cordChannelHeight)) {
24+
// adds (1/2bW - cordChannelHeight) to top of patternHeight to make up additional width
25+
patternHeight = height + bottomWidth - cordChannelHeight + (2 * sA);
26+
} else {
27+
// 1/2bW is the corner cut off bottom to make rectangular
28+
patternHeight = height + (0.5 * bottomWidth) + (2 * sA);
29+
}
30+
31+
let outputFields = {
32+
patternLength: (2 * bottomLength) + (2 * bottomWidth) + (2 * sA),
33+
patternHeight,
34+
channelLength: (2 * bottomLength) + (2 * bottomWidth) + (2 * hem), //hem on each end
35+
channelHeight: (2 * cordChannelHeight) + (2 * sA), // SA on top and bottom
36+
sA,
37+
bottomWidth,
38+
halfBottomWidth: bottomWidth / 2,
39+
bottomLength,
40+
};
41+
42+
setCalculatedValues(isMetric, outputFields);
43+
}, 'v0.1');

assets/js/rolltopsack.js

Lines changed: 39 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -4,100 +4,42 @@
44
Calculate fabric pattern sizes for rectangular bottom stuff sack
55
*/
66

7-
function roundToEigthInch (number) {
8-
var number = (Math.round(number * 8) / 8);
9-
return number;
10-
}
11-
12-
function patternSize(units,bL,bW,h) {
13-
if (units==1) { // METRIC
14-
var rt = 12; // roll-top collar height (12 cm)
15-
var hem = 4; // double fold hem allowance at top of collar (20 mm webbing)
16-
var sA = 1.5; // general seam allowance (1.5 cm) for french seams
17-
}
18-
else { // IMPERIAL
19-
var rt = 4.5; // roll-top collar height (4.5") 3/4" webbing rolled 4 times
20-
var hem = 1.5; // double fold hem allowance at top of collar (0.75" webbing)
21-
var sA = 5/8; // general seam allowance (5/8") for french seams
22-
}
23-
24-
25-
// body fabric panel
26-
var fabricL = 2*bL + (2*bW) + (2*sA);
27-
var rollTopH = rt + hem; // roll-top height + top hem
28-
var webbingL = bL + bW + 4*hem; //adds hem on each end to fold under
29-
30-
if (bW > 3) { // add material so top will close when bW is wide
31-
var fabricH = h + bW + sA + rollTopH; // adds (1/2bW) to top of fabricH to make up additional width
32-
}
33-
else {
34-
var fabricH = h + (0.5*bW) + sA + rollTopH; // 1/2bW is the corner cut off bottom to make rectangular
35-
}
36-
37-
38-
39-
// rounding dimensions for output
40-
if (units==1) {
41-
var fabricL = fabricL.toFixed(1); //trimming to closest mm
42-
var fabricH = fabricH.toFixed(1);
43-
}
44-
else {
45-
var fabricL = roundToEigthInch(fabricL); //rounding to nearest 1/8" increment
46-
var fabricH = roundToEigthInch(fabricH);
47-
}
48-
49-
return [fabricL, fabricH, sA, hem, webbingL];
50-
51-
};
52-
53-
54-
55-
$('document').ready(function () {
56-
57-
var version = "v0.1"; // initial version
58-
$('.version').html(version);
59-
60-
61-
$('.dimension').change(function () { //when any .dimension changes (input loses focus), function runs
62-
63-
var units = $( "input[type=radio][name=units]:checked" ).val(); // inches (val=0) or cm (val=1)
64-
65-
// assign variables based on input id values from html form.
66-
//Ensure object is number otherwise arithmetic is screwy
67-
var bottomLength = Number( $('#bottomLength').val() );
68-
var bottomWidth = Number( $('#bottomWidth').val() );
69-
var sackHeight = Number( $('#height').val() );
70-
71-
var halfBottomWidth = bottomWidth / 2; //used to size bottom corner within instructions
72-
var halfTop = bottomLength + bottomWidth; //used to size length of webbing along roll-top
73-
74-
if (bottomLength > 1 && bottomWidth > 0 && sackHeight > 1) { // html input min=1, crappy validation but works
75-
var scrap = patternSize(units, bottomLength, bottomWidth, sackHeight);
76-
var fabricL = scrap[0];
77-
var fabricH = scrap[1];
78-
if (units == 1) {
79-
var unitText = " cm";
80-
var sA = scrap[2];
81-
var hem = scrap[3];
82-
}
83-
else {
84-
var unitText = " in";
85-
var sA = "5/8";
86-
var hem = scrap[3];
87-
}
88-
var webbingLength = scrap[4];
89-
90-
// html id returns
91-
$('.patternLength').html(fabricL + unitText);
92-
$('.patternHeight').html(fabricH + unitText);
93-
$('.sA').html(sA + unitText);
94-
$('.hem').html(hem + unitText);
95-
$('.bottomWidth').html(bottomWidth + unitText);
96-
$('.halfBottomWidth').html(halfBottomWidth + unitText);
97-
$('.bottomLength').html(bottomLength + unitText);
98-
$('.webbingLength').html(webbingLength + unitText);
99-
$('.halfTop').html(halfTop + unitText);
100-
101-
}
102-
});
103-
});
7+
calculatorSetup(function () {
8+
let isMetric = getIsMetric();
9+
let fields = ['#bottomLength', '#bottomWidth', '#height'].map(getNumberFromField);
10+
11+
if (fields.some(field => field <= 0)) {
12+
return;
13+
}
14+
let [bottomLength, bottomWidth, height] = fields;
15+
16+
const { rollTop, hem, sA } = isMetric ?
17+
{ rollTop: 12, hem: 4, sA: 1.5 } :
18+
{ rollTop: 4.5, hem: 1.5, sA: 5 / 8 };
19+
20+
let rollTopHeight = rollTop + hem;
21+
let patternHeight;
22+
23+
// add material so top will close when bottomWidth is wide
24+
if (bottomWidth > 3) {
25+
// adds (1/2bW) to top of fabricH to make up additional width
26+
patternHeight = height + bottomWidth + sA + rollTopHeight;
27+
} else {
28+
// 1/2bW is the corner cut off bottom to make rectangular
29+
patternHeight = height + (0.5 * bottomWidth) + sA + rollTopHeight;
30+
}
31+
32+
let outputFields = {
33+
patternLength: (2 * bottomLength) + (2 * bottomWidth) + (2 * sA),
34+
patternHeight,
35+
webbingLength: bottomLength + bottomWidth + (4 * hem), //adds hem on each end to fold under
36+
halfBottomWidth: bottomWidth / 2,
37+
halfTop: bottomLength + bottomWidth,
38+
bottomLength,
39+
bottomWidth,
40+
sA,
41+
hem,
42+
};
43+
44+
setCalculatedValues(isMetric, outputFields);
45+
}, 'v0.1');

0 commit comments

Comments
 (0)