//title: Sheet All-Needle Cast-On (javascript with knitout module)
// Casts on a sheet by knitting all needles and then stacking front and back stitches to produce a decorative welt.

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

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

//Bring in carrier:
K.inhook(carrier);
//Set stitch table entry for cast-on:
K.stitchNumber(101);

//On SWGN2 machines, carriers start on the right,
//so will start by tucking all needles right-to-left,
//and will be sure to tuck the leftmost back-bed needle last.

//Need to use quarter-pitch racking to tuck all needles in one pass:
K.rack(-0.75);

for (let n = max; n >= min; n -= 1) {
	K.tuck("-", 'f' + n, carrier);
	K.tuck("-", 'b' + n, carrier);
}

//Return to aligned racking:
K.rack(0);

//Set stitch table entry for knitting:
K.stitchNumber(105);

//Do a rows of plain knitting on the front and back:
for (let n = min; n <= max; n += 1) {
	K.knit("+", 'f' + n, carrier);
}
for (let n = max; n >= min; n -= 1) {
	K.knit("-", 'b' + n, carrier);
}
//One can knit more on the front or back beds here to make a larger "welt" at the edge.

//Stack back-bed stitches with front-bed stitches:
for (let n = max; n >= min; n -= 1) {
	K.xfer('b' + n, 'f' + n);
}

//knit a plain row through the stacked stitches and return carrier to the right edge:
for (let n = min; n <= max; n += 1) {
	K.knit("+", 'f' + n, carrier);
}

//send out yarn inserting hook; it is no longer needed to hold the yarn in place:
K.releasehook(carrier);

//...now can knit on [min,max].

K.write('sheet-all-needle-cast-on.swgn2.k');
