forked from textiles-lab/knitout-frontend-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknitout.js
469 lines (386 loc) · 14 KB
/
knitout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
"use strict";
// basic writer
var Writer = function(opts){
//public data:
this.carriers = {}; //names of all currently active carriers
this.needles = {}; //names of all currently-holding-loops needles
this.racking = 0; //current racking value
//private data:
this._carriers = []; //array of carrier names, front-to-back order
this._operations = []; //array of operations, stored as strings
this._headers = []; //array of headers. stored as strings
//fill '_carriers' array from opts.carriers:
if (typeof(opts) === 'undefined' || !('carriers' in opts)) {
console.warn("WARNING: options object passed to knitout.Writer does not contain a 'carriers' member. Will assume a default carrier layout (a single carrier named \"A\")");
this._carriers = ["A"];
} else {
if (!Array.isArray(opts.carriers)) throw new Error("opts.carriers should be an array of carrier names");
opts.carriers.forEach((name) => {
if (!(typeof(name) === 'string' && name.indexOf(' ') === -1)) {
throw new Error("Carrier names must be strings that do not contain the space character (' ').");
}
});
this._carriers = opts.carriers.slice();
}
//build a 'carriers' header from the '_carriers' list:
this._headers.push(";;Carriers: " + this._carriers.join(" "));
};
// function that queues header information to header list
Writer.prototype.addHeader = function(name, value) {
if (name === undefined || value === undefined) {
throw new Error("Writer.addHeader should be called with a name and a value");
}
if (!(typeof(name) === 'string' && name.indexOf(': ') === -1)) {
throw new Error("Header names must be strings that don't contain the sequence ': '");
}
if (!(typeof(value) === 'string' && value.indexOf('\n') === -1)) {
throw new Error("Header values must be strings that do not contain the LF character ('\\n').");
}
//Check for valid headers:
if (name === "Carriers") {
throw new Error("Writer.addHeader can't set Carriers header (use the 'carriers' option when creating the writer instead).");
} else if (name === "Machine") {
//no restrictions on value
} else if (name === "Gauge") {
//TODO: warn if value is not a string representing a number
} else if (name === "Position") {
//TODO: commit to a list of values, possibly warn on non-standard value
} else if (name.startsWith("Yarn-")) {
//check for valid carrier name, warn otherwise
let carrierName = name.substr(5);
if (this._carriers.indexOf(carrierName) === -1) {
console.warn("Warning: header '" + name + "' mentions a carrier that isn't in the carriers list.");
}
} else if (name.startsWith('X-')) {
//all extension header values are okay!
} else {
console.warn("Warning: header name '" + name + "' not recognized; header will still be written.");
}
this._headers.push(';;' + name + ': ' + value);
};
// escape hatch to dump your custom instruction to knitout
// if you know what you are doing
Writer.prototype.addRawOperation = function( operation ){
console.warn("Warning: operation added to list as is(string), no error checking performed.");
this._operations.push(operation);
};
//helpers to extract parameters from argument arrays:
// (these remove the extracted arguments from the start of the array and throw on errors)
//shiftDirection interprets the first element of 'args' as a direction, and throws on error:
// returns '+' or '-'.
function shiftDirection(args) {
console.assert(Array.isArray(args));
if (args.length === 0) {
throw new Error("Direction missing.");
}
if (!(args[0] === '+' || args[0] === '-')) {
throw new Error("Direction should be '+' or '-'.");
}
let dir = args.shift();
return dir;
}
//shiftBedNeedle interprets the first one or two arguments of 'args' as a bed+needle number, and throws on error:
// returns a {bed:, needle:} object.
const BedNeedleRegex = /^([fb]s?)(-?\d+)$/;
const BedRegex = /^[fb]s?$/;
function shiftBedNeedle(args) {
console.assert(Array.isArray(args));
if (args.length === 0) {
throw new Error("Needle missing.");
}
let bed, needle;
//case: bed string, needle number:
if (typeof(args[0]) === 'string' && BedRegex.test(args[0])) {
if (args.length < 2 || !Number.isInteger(args[1])) {
throw new Error("Expecting bed name to be followed by a needle number.");
}
bed = args.shift();
needle = args.shift();
//case: single string "f12", "b-2", "bs66":
} else if (typeof(args[0]) === 'string') {
let m = args[0].match(BedNeedleRegex);
if (m === null) {
throw new Error("String '" + args[0] + "' does not look like a compound bed+needle string.");
}
bed = m[1];
needle = parseInt(m[2]);
args.shift();
//case: two-member array ["fs", 2]
} else if (Array.isArray(args[0])) {
if (!( args[0].length === 2 && typeof(args[0][0]) === 'string' && BedRegex.test(args[0][0]) && Number.isInteger(args[0][1]) )) {
throw new Error("Bed+needle array should look like [\"f\", 12].");
}
bed = args[0][0];
needle = args[0][1];
args.shift();
//case: object {bed:"fs", needle:5}
} else if (typeof(args[0]) === 'object') {
if (!( 'bed' in args[0] && typeof(args[0].bed) === 'string' && BedRegex.test(args[0].bed) )) {
throw new Error("Bed+needle object should have a 'bed' member string naming the bed.");
}
if (!( 'needle' in args[0] && Number.isInteger(args[0].needle) )) {
throw new Error("Bed+needle object should have a 'needle' member integer.");
}
bed = args[0].bed;
needle = args[0].needle;
args.shift();
} else {
throw new Error("Expecting bed+needle as name+number (\"fs\", 6), string (\"b-2\"), array ([\"f\", 6]), or object ({bed:\"bs\", needle:12}). Got '" + JSON.stringify(args) + "'");
}
return {bed:bed, needle:needle};
}
//shiftCarrierSet interprets the remaining contents of 'args' as an array of carrier names, and throws on error:
// returns an array of carrier names, e.g., ["C", "A"].
function shiftCarrierSet(args, carrierNames) {
let carrierSet = [];
//carrier set as array, e.g., knit(..., ["A", "B"]):
if (args.length === 1 && Array.isArray(args[0])) {
carrierSet = args.shift().slice();
} else {
//carrier set as parameters, e.g., knit(..., "A", "B");
carrierSet = args.splice(0,args.length).slice();
}
// slightly ugly handling of various ways of typeing "A B", "A, B"
carrierSet.forEach(function(name, idx){
let space_split = name.split(" ");
let first = true;
space_split.forEach(function(s, sidx){
if(s == '') return;
if(first){
carrierSet[idx] = s;
first = false;
}
else carrierSet.push(s);
});
});
carrierSet.forEach(function(name, idx){
let comma_split = name.split(",");
let first = true;
comma_split.forEach(function(s, sidx){
if(s =='') return;
if(first) {
carrierSet[idx] = s;
first = false;
}
else carrierSet.push(s);
});
});
carrierSet.forEach(function(name){
if (carrierNames.indexOf(name) === -1) {
throw new Error("Invalid carrier name '" + name + "'");
}
});
return carrierSet;
}
Writer.prototype.in = function(...args){
let cs = shiftCarrierSet(args, this._carriers);
if (cs.length === 0) {
throw new Error("It doesn't make sense to 'in' on an empty carrier set.");
}
cs.forEach(function(cn){
if (cn in this.carriers) {
throw new Error("Carrier '" + cn + "' is already in.");
}
this.carriers[cn] = {hook:false};
}, this);
this._operations.push('in ' + cs.join(' '));
};
Writer.prototype.inhook = function(...args){
let cs = shiftCarrierSet(args, this._carriers);
if (cs.length === 0) {
throw new Error("It doesn't make sense to 'inhook' on an empty carrier set.");
}
cs.forEach(function(cn){
if (cn in this.carriers) {
throw new Error("Carrier '" + cn + "' is already in.");
}
this.carriers[cn] = {hook:true};
}, this);
this._operations.push('inhook ' + cs.join(' '));
};
Writer.prototype.releasehook = function(...args){
let cs = shiftCarrierSet(args, this._carriers);
if (cs.length === 0) {
throw new Error("It doesn't make sense to 'releasehook' on an empty carrier set.");
}
cs.forEach(function(cn){
if (!(cn in this.carriers)) {
throw new Error("Carrier '" + cn + "' isn't in.");
}
if (!this.carriers[cn].hook) {
throw new Error("Carrier '" + cn + "' isn't in the hook.");
}
this.carriers[cn].hook = false;
}, this);
this._operations.push('releasehook ' + cs.join(' '));
};
Writer.prototype.out = function(...args){
let cs = shiftCarrierSet(args, this._carriers);
if (cs.length === 0) {
throw new Error("It doesn't make sense to 'out' on an empty carrier set.");
}
cs.forEach(function(cn){
if (!(cn in this.carriers)) {
throw new Error("Carrier '" + cn + "' isn't in.");
}
delete this.carriers[cn];
}, this);
this._operations.push('out ' + cs.join(' '));
};
Writer.prototype.outhook = function(...args){
let cs = shiftCarrierSet(args, this._carriers);
if (cs.length === 0) {
throw new Error("It doesn't make sense to 'outhook' on an empty carrier set.");
}
cs.forEach(function(cn){
if (!(cn in this.carriers)) {
throw new Error("Carrier '" + cn + "' isn't in.");
}
delete this.carriers[cn];
}, this);
this._operations.push('outhook ' + cs.join(' '));
};
function isFiniteNumber( n ) {
if (typeof(n) === 'number' && Number.isFinite(n) && !Number.isNaN(n)) return true;
return false;
}
Writer.prototype.stitch = function(before, after) {
if (!(isFiniteNumber(before) && isFiniteNumber(after))) {
throw new Error("Stitch L and T values must be finite numbers.");
}
this._operations.push('stitch ' + before.toString() + ' ' + after.toString());
};
//note: extension!
Writer.prototype.stitchNumber = function (stitchNumber) {
if (!(Number.isInteger(stitchNumber) && stitchNumber > 0)) {
throw new Error("Stitch numbers are positive integer values.");
}
this._operations.push('x-stitch-number ' + stitchNumber.toString());
};
Writer.prototype.fabricPresser = function (presserMode){
if(presserMode === 'auto'){
this._operations.push('x-presser-mode auto');
}
else if(presserMode === 'on'){
this._operations.push('x-presser-mode on');
}
else if(presserMode === 'off'){
this._operations.push('x-presser-mode off');
}
else{
console.warn('Ignoring presser mode extension, unknown mode ' + presserMode + '. Valid modes: on, off, auto');
}
}
Writer.prototype.rack = function(rack) {
if (!(isFiniteNumber(rack))) {
throw new Error("Racking values must be finite numbers.");
}
this.racking = rack;
this._operations.push('rack ' + rack.toString());
};
Writer.prototype.knit = function(...args) {
let dir = shiftDirection(args);
let bn = shiftBedNeedle(args);
let cs = shiftCarrierSet(args, this._carriers);
if (cs.length > 0) {
this.needles[bn.bed + bn.needle.toString()] = true;
} else {
delete this.needles[bn.bed + bn.needle.toString()];
}
this._operations.push('knit ' + dir + ' ' + bn.bed + bn.needle.toString() + ' ' + cs.join(' '));
};
Writer.prototype.tuck = function(...args) {
let dir = shiftDirection(args);
let bn = shiftBedNeedle(args);
let cs = shiftCarrierSet(args, this._carriers);
this.needles[bn.bed + bn.needle.toString()] = true;
this._operations.push('tuck ' + dir + ' ' + bn.bed + bn.needle.toString() + ' ' + cs.join(' '));
};
Writer.prototype.split = function(...args) {
let dir = shiftDirection(args);
let from = shiftBedNeedle(args);
let to = shiftBedNeedle(args);
let cs = shiftCarrierSet(args, this._carriers);
if ((from.bed + from.needle.toString()) in this.needles) {
this.needles[to.bed + to.needle.toString()] = true;
delete this.needles[from.bed + from.needle.toString()];
}
if (cs.length > 0) {
this.needles[from.bed + from.needle.toString()] = true;
}
this._operations.push('split ' + dir + ' ' + from.bed + from.needle.toString() + ' ' + to.bed + to.needle.toString() + ' ' + cs.join(' '));
};
Writer.prototype.miss = function(...args) {
let dir = shiftDirection(args);
let bn = shiftBedNeedle(args);
let cs = shiftCarrierSet(args, this._carriers);
if (cs.length === 0) {
throw new Error("It doesn't make sense to miss with no carriers.");
}
this._operations.push('miss ' + dir + ' ' + bn.bed + bn.needle.toString() + ' ' + cs.join(' '));
};
// drop -> knit without yarn, but supported in knitout
Writer.prototype.drop = function(...args) {
let bn = shiftBedNeedle(args);
if (args.length !== 0) {
throw new Error("drop only takes a bed+needle");
}
delete this.needles[bn.bed + bn.needle.toString()];
this._operations.push('drop ' + bn.bed + bn.needle.toString());
};
// amiss -> tuck without yarn, but supported in knitout
Writer.prototype.amiss = function(...args) {
let bn = shiftBedNeedle(args);
if (args.length !== 0) {
throw new Error("amiss only takes a bed+needle");
}
this._operations.push('amiss ' + bn.bed + bn.needle.toString());
};
// xfer -> split without yarn, but supported in knitout
Writer.prototype.xfer = function(...args) {
let from = shiftBedNeedle(args);
let to = shiftBedNeedle(args);
if (args.length !== 0) {
throw new Error("xfer only takes two bed+needles");
}
if ((from.bed + from.needle.toString()) in this.needles) {
this.needles[to.bed + to.needle.toString()] = true;
delete this.needles[from.bed + from.needle.toString()];
}
this._operations.push('xfer ' + from.bed + from.needle.toString() + ' ' + to.bed + to.needle.toString());
};
// add comments to knitout
Writer.prototype.comment = function( str ){
let multi = str.split('\n');
multi.forEach(function(entry){
// cannot add header comments with comment
while(entry.startsWith(';')){
console.warn('Warning: comment starts with ; use addHeader for adding header comments.');
entry = entry.substr(1, entry.length);
}
this._operations.push(';' + entry.toString());
}, this);
};
Writer.prototype.pause = function(comment){
// deals with multi-line comments
this.comment(comment);
this._operations.push('pause');
};
Writer.prototype.write = function(filename){
let version = ';!knitout-2';
let content = version + '\n' +
this._headers.join('\n') + '\n' +
this._operations.join('\n') + '\n';
try{
let fs = require('fs');
fs.writeFileSync(filename, content); //default is utf8
}
catch(e){
console.warn("Can't load 'fs'. Did not write file.");
}
return content;
};
// browser-compatibility
if(typeof(module) !== 'undefined'){
module.exports.Writer = Writer;
}