//title: Single Jersey Tube (javascript with knitout module)

//include knitout module:
const knitout = require('knitout'); //include knitout module
const K = new knitout.Writer({ carriers:['1', '2', '3', '4', '5', '6'] });
K.addHeader('Machine', 'Kniterate');

//Parameters:
let min = 1; //needle number of left edge
let max = 20; //needle number of right edge
let rows = 20; //number of rows to knit
let carrier = "3"; //carrier name

// ---- set up initial loops and yarn carrier ----
//Bring in carrier:
K.in(carrier);

//On Kniterate machines, carriers start on the left,
//so will start tucking onto needles left-to-right:
for (let n = min; n <= max; n += 1) {
	//tuck alternating needles starting at the left edge:
	if ((n-min) % 2 === 0) {
		K.tuck("+", 'b' + n, carrier);
	}
}

//...continue clockwise around the tube to the front bed:
for (let n = max; n >= min; n -= 1) {
	//note that adding 1+max-min preserves the alternating-needles pattern:
	if ((1+max-min + max-n) % 2 === 0) {
		K.tuck("-", 'f' + n, carrier);
	}
}

//...continue clockwise around the tube to the back bed and fill in needles missed the first time:
for (let n = min; n <= max; n += 1) {
	//tuck alternating needles starting at the left edge:
	if ((n-min) % 2 !== 0) {
		K.tuck("+", 'b' + n, carrier);
	}
}

//...finally, fill in the rest of the front bed:
for (let n = max; n >= min; n -= 1) {
	//note that adding 1+max-min preserves the alternating-needles pattern:
	if ((1+max-min + max-n) % 2 !== 0) {
		K.tuck("-", 'f' + n, carrier);
	}
}

//now knit one and a half rows of plain knitting to let the cast-on stitches relax (and because having the carrier on the right will be convenient for example code):
for (let n = min; n <= max; n += 1) {
	K.knit("+", 'b' + n, carrier);
}
for (let n = max; n >= min; n -= 1) {
	K.knit("-", 'f' + n, carrier);
}
for (let n = min; n <= max; n += 1) {
	K.knit("+", 'b' + n, carrier);
}

// ---- single jersey tube ----
//Create a single-jersey (all knits) tube:
for (let r = 0; r <= rows; r += 1) {
	//knit front of tube going left:
	for (let n = max; n >= min; n -= 1) {
		K.knit("-", 'f' + n, carrier);
	}
	//knit back of tube going right:
	for (let n = min; n <= max; n += 1) {
		K.knit("+", 'b' + n, carrier);
	}
}

// ---- take carrier out and drop remaining loops ----
//Send carrier back to its parking location:
K.out(carrier);

//drop loops:
for (let n = min; n <= max; n += 1) {
	K.drop('f' + n);
}
for (let n = min; n <= max; n += 1) {
	K.drop('b' + n);
}

K.write('single-jersey-tube.kniterate.k');
