MCPcopy Create free account
hub / github.com/alefore/knit / TrianglesPatternFactory

Class TrianglesPatternFactory

src/triangles_pattern_factory.ts:99–150  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

97}
98
99class TrianglesPatternFactory {
100 factoryName: string = 'Triangles';
101 knittingStyleInput: PatternFactoryInput;
102 lengthInput: PatternFactoryInput;
103 sectionCountInput: PatternFactoryInput;
104 sections: Section[];
105
106 constructor() {
107 this.knittingStyleInput = new PatternFactoryInput(
108 'Knitting Style', 'Should the piece be knit in the round or flat?',
109 RowSwitchStyles.round, null, Object.values(RowSwitchStyles));
110 this.lengthInput = new PatternFactoryInput(
111 'Length', 'How long should the set of triangles be?', 30, 'rows');
112 this.sectionCountInput = new PatternFactoryInput(
113 'Sections', 'How many sections should the polygon have?', 4,
114 'sections');
115 this.sections = Array.from(
116 {length: 20},
117 (_, index) => new Section(
118 index, this.sectionCountInput, this.knittingStyleInput));
119 }
120
121 getInputs(): PatternFactoryInput[] {
122 return [
123 this.knittingStyleInput, this.lengthInput, this.sectionCountInput,
124 ...this.sections.flatMap((s) => s.getInputs())
125 ];
126 }
127
128 build(): Pattern {
129 const output = new Pattern();
130 const knittingStyle = this.knittingStyleInput.value() as RowSwitchStyle;
131 output.rowSwitchStyle = knittingStyle;
132
133 for (let rowIndex = 0; rowIndex < this.lengthInput.numberValue();
134 rowIndex++) {
135 let stitches: Stitch[] = [];
136 const invertOrder =
137 knittingStyle === RowSwitchStyles.backAndForth && rowIndex % 2 === 1;
138 for (let section = 0; section < this.sectionCountInput.numberValue();
139 section++) {
140 const actualSection = invertOrder ?
141 this.sectionCountInput.numberValue() - section - 1 :
142 section;
143 stitches.push(...this.sections[actualSection]!.getStitches(
144 rowIndex, this.lengthInput.numberValue()));
145 }
146 output.addRow(new Row(stitches));
147 }
148 return output;
149 }
150}
151
152PatternFactoryRegistry.register('Triangles', TrianglesPatternFactory);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected