Skip to content

Commit

Permalink
Fixed a bug that cannot properly handle invalid G-code words (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton committed May 24, 2019
1 parent 12f427a commit 5aaf935
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const parseLine = (() => {
const re3 = new RegExp(/\s+/g);
return (line => line.replace(re1, '').replace(re2, '').replace(re3, ''));
})();
const re = /(%.*)|({.*)|((?:\$\$)|(?:\$[a-zA-Z0-9#]*))|([a-zA-Z][0-9\+\-\.]*)|(\*[0-9]+)/igm;
const re = /(%.*)|({.*)|((?:\$\$)|(?:\$[a-zA-Z0-9#]*))|([a-zA-Z][0-9\+\-\.]+)|(\*[0-9]+)/igm;

return (line, options) => {
options = options || {};
Expand Down Expand Up @@ -105,7 +105,7 @@ const parseLine = (() => {
continue;
}

// Parser JSON commands for TinyG and g2core
// Parse JSON commands for TinyG and g2core
if (letter === '{') {
result.cmds = (result.cmds || []).concat(line.trim());
continue;
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/messed-up.gcode
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
messed up
10 changes: 10 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ describe('gcode-parser', () => {
});
});

describe('Invalid G-code words', () => {
it('should ignore invalid g-code words', (done) => {
const data = parseLine('messed up');
expect(data).to.be.an('object');
expect(data.line).to.be.equal('messed up');
expect(data.words).to.be.empty;
done();
});
});

describe('Commands', () => {
it('should be able to parse $ command (e.g. Grbl).', (done) => {
const data = parseLine('$H $C');
Expand Down

0 comments on commit 5aaf935

Please sign in to comment.