function processPathLettersIntoArray(source){
source = source.split("M").join(";M");
source = source.split("L").join(";L");
source = source.split("z").join(";z");
source = source.split(";");
source = source.slice(1);
return source.map(mapGroup);
}
This is my split method.
function gcodeXY(source){
var [xVal, yVal] = source.split(",");
return `X${xVal} Y${yVal}`
}
let start="X0 Y0"; //another side effect :-(
function mapGroup(grp){
let s=grp.split(" ");
if (s[0]=="M"){
start=gcodeXY(s[1]);
last=s[1];
return "G0 F200 "+start;
}
else if (s[0]=="L"){
start=gcodeXY(s[1]);
last=s[1];
return "G1 F100 "+start;
}
else if (s[0]=="z"){
last=s[1];
return "G1 F100 "+start;
}
return grp;
}
This is how I want each string to be achieved after splitting.
function path2gcode(source){
source = processPathLettersIntoArray(source);
return source;
}
This is the method I need to run.
What I want ask is that in Javascript, when I separate the strings, how should I run the method for each string after the split?The picture shows the input and output examples and my current output.
This will work
function processPathLettersIntoArray(source) {
const result = source.replace(/M/g, ";M").replace(/L/g, ";L").replace(/z/g, ";z").split(';').slice(1);
return result.map(mapGroup);
}
function gcodeXY(source) {
const [xVal, yVal] = source.split(",");
return `X${xVal} Y${yVal}`;
}
let start="X0 Y0"; //another side effect :-(
function mapGroup(grp) {
let s = grp.split(" ");
if (s[0]=="M"){
start = gcodeXY(s[1]);
last=s[1];
return "G0 F200 "+start;
}
else if (s[0]=="L"){
start=gcodeXY(s[1]);
last=s[1];
return "G1 F100 "+start;
}
else if (s[0]=="z"){
last=s[1];
return "G1 F100 "+start;
}
return grp;
}
function path2gcode() {
const source = 'M 1,2 L 2,2 L 3,3 z';
return processPathLettersIntoArray(source);
}