Skip to content

Commit 38c8b86

Browse files
committed
Merge branch 'dev'
2 parents f8c8eaa + 8930c74 commit 38c8b86

File tree

8 files changed

+123
-44
lines changed

8 files changed

+123
-44
lines changed

dist/index.js

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,27 +79,47 @@ var GCodeParser = (function (_Transform) {
7979
}
8080

8181
var n = undefined;
82+
var checksum = undefined;
8283
var words = [];
83-
var list = removeSpaces(line).match(/([a-zA-Z][^a-zA-Z]*)/igm) || [];
84+
var list = removeSpaces(line).match(/([a-zA-Z][0-9\+\-\.]*)|(\*[0-9]+)/igm) || [];
85+
8486
_lodash2.default.each(list, function (word) {
85-
var r = word.match(/([a-zA-Z])([^a-zA-Z]*)/) || [];
86-
var letter = (r[1] || '').toUpperCase();
87-
var argument = _lodash2.default.isNaN(parseFloat(r[2])) ? r[2] : Number(r[2]);
88-
89-
if (letter === 'N' && typeof n === 'undefined') {
90-
// Line (block) number in program
91-
n = Number(argument);
92-
return;
87+
var letter = word[0].toUpperCase();
88+
var argument = word.substr(1);
89+
90+
argument = _lodash2.default.isNaN(parseFloat(argument)) ? argument : Number(argument);
91+
92+
//
93+
// Special fields
94+
//
95+
96+
{
97+
// N: Line number
98+
if (letter === 'N' && _lodash2.default.isUndefined(n)) {
99+
// Line (block) number in program
100+
n = Number(argument);
101+
return;
102+
}
103+
}
104+
105+
{
106+
// *: Checksum
107+
if (letter === '*' && _lodash2.default.isUndefined(checksum)) {
108+
checksum = Number(argument);
109+
return;
110+
}
93111
}
94112

95113
words.push([letter, argument]);
96114
});
97115

98-
_this2.push({
99-
line: line,
100-
N: n,
101-
words: words
102-
});
116+
var obj = {};
117+
obj.line = line;
118+
obj.words = words;
119+
typeof n !== 'undefined' && (obj.N = n); // N: Line number
120+
typeof checksum !== 'undefined' && (obj.checksum = checksum); // *: Checksum
121+
122+
_this2.push(obj);
103123
});
104124

105125
next();

index.js

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,46 @@ class GCodeParser extends Transform {
4444
}
4545

4646
let n;
47+
let checksum;
4748
let words = [];
4849
let list = removeSpaces(line)
49-
.match(/([a-zA-Z][^a-zA-Z]*)/igm) || [];
50+
.match(/([a-zA-Z][0-9\+\-\.]*)|(\*[0-9]+)/igm) || [];
51+
5052
_.each(list, (word) => {
51-
let r = word.match(/([a-zA-Z])([^a-zA-Z]*)/) || [];
52-
let letter = (r[1] || '').toUpperCase();
53-
let argument = _.isNaN(parseFloat(r[2])) ? r[2] : Number(r[2]);
54-
55-
if (letter === 'N' && (typeof n === 'undefined')) {
56-
// Line (block) number in program
57-
n = Number(argument);
58-
return;
53+
let letter = word[0].toUpperCase();
54+
let argument = word.substr(1);
55+
56+
argument = _.isNaN(parseFloat(argument)) ? argument : Number(argument);
57+
58+
//
59+
// Special fields
60+
//
61+
62+
{ // N: Line number
63+
if (letter === 'N' && _.isUndefined(n)) {
64+
// Line (block) number in program
65+
n = Number(argument);
66+
return;
67+
}
68+
}
69+
70+
{ // *: Checksum
71+
if (letter === '*' && _.isUndefined(checksum)) {
72+
checksum = Number(argument);
73+
return;
74+
}
5975
}
6076

6177
words.push([letter, argument]);
6278
});
6379

64-
this.push({
65-
line: line,
66-
N: n,
67-
words: words
68-
});
80+
let obj = {};
81+
obj.line = line;
82+
obj.words = words;
83+
(typeof(n) !== 'undefined') && (obj.N = n); // N: Line number
84+
(typeof(checksum) !== 'undefined') && (obj.checksum = checksum); // *: Checksum
85+
86+
this.push(obj);
6987
});
7088

7189
next();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gcode-parser",
3-
"version": "0.3.0",
3+
"version": "0.4.0",
44
"description": "A G-code parser for Node.js",
55
"author": "Cheton Wu <[email protected]>",
66
"homepage": "https://github.com/cheton/gcode-parser",
File renamed without changes.
File renamed without changes.
File renamed without changes.

test/fixtures/special-fields.gcode

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
N3 T0*57
2+
N4 G92 E0*67
3+
N5 G28*22
4+
N6 G1 F1500.0*82
5+
N7 G1 X2.0 Y2.0 F3000.0*85
6+
N8 G1 X3.0 Y3.0*33

test/index.js

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,58 +77,51 @@ console.log(sampleText, err, results);
7777
let expectedResults = [
7878
{
7979
line: 'G0 X-5 Y0 Z0 F200',
80-
N: undefined,
8180
words: [['G', 0], ['X', -5], ['Y', 0], ['Z', 0], ['F', 200]]
8281
},
8382
{
8483
line: 'G2 X0 Y5 I5 J0 F200',
85-
N: undefined,
8684
words: [['G', 2], ['X', 0], ['Y', 5], ['I', 5], ['J', 0], ['F', 200]]
8785
},
8886
{
8987
line: 'G02 X5 Y0 I0 J-5',
90-
N: undefined,
9188
words: [['G', 2], ['X', 5], ['Y', 0], ['I', 0], ['J', -5]]
9289
},
9390
{
9491
line: 'G02 X0 Y-5 I-5 J0',
95-
N: undefined,
9692
words: [['G', 2], ['X', 0], ['Y',-5], ['I', -5], ['J', 0]]
9793
},
9894
{
9995
line: 'G02 X-5 Y0 I0 J5',
100-
N: undefined,
10196
words: [['G', 2], ['X', -5], ['Y', 0], ['I', 0], ['J', 5]]
10297
},
10398
{
10499
line: 'G01 Z1 F500',
105-
N: undefined,
106100
words: [['G', 1], ['Z', 1], ['F', 500]]
107101
},
108102
{
109103
line: 'G00 X0 Y0 Z5',
110-
N: undefined,
111104
words: [['G', 0], ['X', 0], ['Y', 0], ['Z', 5]]
112105
}
113106
];
114107

115108
it('should get the expected results in the parseFile\'s callback.', (done) => {
116-
parseFile('test/fixtures/circle.nc', (err, results) => {
109+
parseFile('test/fixtures/circle.gcode', (err, results) => {
117110
expect(results).to.deep.equal(expectedResults);
118111
done();
119112
});
120113
});
121114

122115
it('should get the expected results in the parseText\'s callback.', (done) => {
123-
let text = fs.readFileSync('test/fixtures/circle.nc', 'utf8');
116+
let text = fs.readFileSync('test/fixtures/circle.gcode', 'utf8');
124117
parseText(text, (err, results) => {
125118
expect(results).to.deep.equal(expectedResults);
126119
done();
127120
});
128121
});
129122

130123
it('should get the expected results in the parseStream\'s callback.', (done) => {
131-
let stream = fs.createReadStream('test/fixtures/circle.nc', { encoding: 'utf8' });
124+
let stream = fs.createReadStream('test/fixtures/circle.gcode', { encoding: 'utf8' });
132125
parseStream(stream, (err, results) => {
133126
expect(results).to.deep.equal(expectedResults);
134127
done();
@@ -138,7 +131,7 @@ console.log(sampleText, err, results);
138131

139132
describe('More examples', (done) => {
140133
it('should contain the line number.', (done) => {
141-
parseFile('test/fixtures/circle-inch.nc', (err, list) => {
134+
parseFile('test/fixtures/circle-inch.gcode', (err, list) => {
142135
expect(err).to.be.null;
143136
list.forEach((data) => {
144137
let { N } = data;
@@ -148,25 +141,67 @@ console.log(sampleText, err, results);
148141
});
149142
});
150143

144+
it('should get the expected results for special fields.', (done) => {
145+
let expectedResults = [
146+
{
147+
N: 3,
148+
checksum: 57,
149+
line: 'N3 T0*57',
150+
words: [['T', 0]]
151+
},
152+
{
153+
N: 4,
154+
checksum: 67,
155+
line: 'N4 G92 E0*67',
156+
words: [['G', 92], ['E', 0]]
157+
},
158+
{
159+
N: 5,
160+
checksum: 22,
161+
line: 'N5 G28*22',
162+
words: [['G', 28]]
163+
},
164+
{
165+
N: 6,
166+
checksum: 82,
167+
line: 'N6 G1 F1500.0*82',
168+
words: [['G', 1], ['F', 1500]]
169+
},
170+
{
171+
N: 7,
172+
checksum: 85,
173+
line: 'N7 G1 X2.0 Y2.0 F3000.0*85',
174+
words: [['G', 1], ['X', 2], ['Y', 2], ['F', 3000]]
175+
},
176+
{
177+
N: 8,
178+
checksum: 33,
179+
line: 'N8 G1 X3.0 Y3.0*33',
180+
words: [['G', 1], ['X', 3], ['Y', 3]]
181+
}
182+
];
183+
parseFile('test/fixtures/special-fields.gcode', (err, results) => {
184+
expect(results).to.deep.equal(expectedResults);
185+
done();
186+
});
187+
});
188+
151189
it('should allow spaces between commands.', (done) => {
152190
let expectedResults = [
153191
{
154192
line: 'G0X-5Y0Z0F200',
155-
N: undefined,
156193
words: [['G', 0], ['X', -5], ['Y', 0], ['Z', 0], ['F', 200]]
157194
},
158195
{
159196
line: 'G0 X-5 Y0 Z0 F200',
160-
N: undefined,
161197
words: [['G', 0], ['X', -5], ['Y', 0], ['Z', 0], ['F', 200]]
162198
},
163199
{
164200
line: 'G0 X -5 Y 0 Z 0 F 200',
165-
N: undefined,
166201
words: [['G', 0], ['X', -5], ['Y', 0], ['Z', 0], ['F', 200]]
167202
}
168203
];
169-
parseFile('test/fixtures/spaces.nc', (err, results) => {
204+
parseFile('test/fixtures/spaces.gcode', (err, results) => {
170205
expect(results).to.deep.equal(expectedResults);
171206
done();
172207
});

0 commit comments

Comments
 (0)