Skip to content

Commit c4e1efc

Browse files
authored
fest: support parsing nested comments (#9)
* fest: support parsing nested comments * feat: apply PR review suggestions
1 parent b3bdf84 commit c4e1efc

File tree

2 files changed

+152
-97
lines changed

2 files changed

+152
-97
lines changed

src/__tests__/index.test.js

Lines changed: 66 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ describe('Pass a null value as the first argument', () => {
3535

3636
describe('Pass an empty text as the first argument', () => {
3737
it('should get empty results.', (done) => {
38-
const sampleText = '';
39-
parseString(sampleText, (err, results) => {
38+
const inputText = '';
39+
parseString(inputText, (err, results) => {
40+
expect(err).toBeNull();
4041
expect(results.length).toBe(0);
4142
done();
4243
});
@@ -144,9 +145,38 @@ describe('Commands', () => {
144145
});
145146
});
146147

147-
describe('Comments', () => {
148+
describe('Stripping comments', () => {
149+
it('should correctly parse a semicolon comment before parentheses', () => {
150+
const line = 'M6 ; comment (tool change) T1';
151+
const data = parseLine(line, { lineMode: 'stripped' });
152+
expect(data.line).toBe('M6');
153+
expect(data.comments).toEqual([
154+
'comment (tool change) T1',
155+
]);
156+
});
157+
158+
it('should correctly parse nested parentheses containing a semicolon', () => {
159+
const line = 'M6 (outer (inner;)) T1 ; comment';
160+
const data = parseLine(line, { lineMode: 'stripped' });
161+
expect(data.line).toBe('M6 T1');
162+
expect(data.comments).toEqual([
163+
'outer (inner;)',
164+
'comment',
165+
]);
166+
});
167+
168+
it('should correctly parse multiple comments in a line', () => {
169+
const line = 'M6 (first comment) T1 ; second comment';
170+
const data = parseLine(line, { lineMode: 'stripped' });
171+
expect(data.line).toBe('M6 T1');
172+
expect(data.comments).toEqual([
173+
'first comment',
174+
'second comment',
175+
]);
176+
});
177+
148178
it('should strip everything after a semi-colon to the end of the loine including preceding spaces.', (done) => {
149-
const sampleText = [
179+
const inputText = [
150180
' % ',
151181
' #',
152182
'; Operation: 0',
@@ -161,17 +191,26 @@ describe('Comments', () => {
161191
' ' // empty line
162192
].join('\n');
163193

164-
parseString(sampleText, (err, results) => {
165-
results = results.filter(result => result.length > 0);
166-
expect(results.length).toBe(0);
194+
parseString(inputText, { lineMode: 'stripped' }, (err, results) => {
195+
expect(results).toEqual([
196+
{ line: '%', words: [], cmds: [ '%' ] },
197+
{ line: '#', words: [] },
198+
{ line: '', words: [], comments: [ 'Operation: 0' ] },
199+
{ line: '', words: [], comments: [ 'Name:' ] },
200+
{ line: '', words: [], comments: [ 'Type: Pocket' ] },
201+
{ line: '', words: [], comments: [ 'Paths: 3' ] },
202+
{ line: '', words: [], comments: [ 'Direction: Conventional' ] },
203+
{ line: '', words: [], comments: [ 'Cut Depth: 3.175' ] },
204+
{ line: '', words: [], comments: [ 'Pass Depth: 1.9999999999999998' ] },
205+
{ line: '', words: [], comments: [ 'Plunge rate: 127' ] },
206+
{ line: '', words: [], comments: [ 'Cut rate: 1016' ] }
207+
]);
167208
done();
168209
});
169210
});
170-
});
171211

172-
describe('Parentheses', () => {
173212
it('should remove anything inside parentheses.', (done) => {
174-
const sampleText = [
213+
const inputText = [
175214
'(Generated with: DXF2GCODE, Version: Py3.4.4 PyQt5.4.1, Date: $Date: Sun Apr 17 16:32:22 2016 +0200 $)',
176215
'(Created from file: G:/Dropbox/Konstruktionen/20161022 - MicroCopter 180/complete.dxf)',
177216
'(Time: Sun Oct 23 12:30:46 2016)',
@@ -187,80 +226,60 @@ describe('Parentheses', () => {
187226
].join('\n');
188227
const expectedResults = [
189228
{
190-
gcode: '',
191-
cmds: undefined,
229+
line: '',
192230
comments: ['Generated with: DXF2GCODE, Version: Py3.4.4 PyQt5.4.1, Date: $Date: Sun Apr 17 16:32:22 2016 +0200 $'],
193231
},
194232
{
195-
gcode: '',
196-
cmds: undefined,
233+
line: '',
197234
comments: ['Created from file: G:/Dropbox/Konstruktionen/20161022 - MicroCopter 180/complete.dxf'],
198235
},
199236
{
200-
gcode: '',
201-
cmds: undefined,
237+
line: '',
202238
comments: ['Time: Sun Oct 23 12:30:46 2016'],
203239
},
204240
{
205-
gcode: 'G21G90',
206-
cmds: undefined,
241+
line: 'G21G90',
207242
comments: ['Units in millimeters', 'Absolute programming'],
208243
},
209244
{
210-
gcode: '',
211-
cmds: ['$H'],
245+
line: '$H',
212246
comments: undefined,
213247
},
214248
{
215-
gcode: 'F1000',
216-
cmds: undefined,
249+
line: 'F1000',
217250
comments: undefined,
218251
},
219252
{
220-
gcode: '',
221-
cmds: undefined,
253+
line: '',
222254
comments: ['*** LAYER: 0 ***'],
223255
},
224256
{
225-
gcode: 'T5M6',
226-
cmds: undefined,
257+
line: 'T5M06',
227258
comments: undefined,
228259
},
229260
{
230-
gcode: 'S200',
231-
cmds: undefined,
261+
line: 'S200',
232262
comments: undefined,
233263
},
234264
{
235-
gcode: '',
236-
cmds: undefined,
265+
line: '',
237266
comments: ['* SHAPE Nr: 0 *'],
238267
},
239268
{
240-
gcode: 'G0X180.327Y137.08',
241-
cmds: undefined,
269+
line: 'G0X180.327Y137.080',
242270
comments: undefined,
243271
},
244272
{
245-
gcode: 'M3',
246-
cmds: undefined,
273+
line: 'M03',
247274
comments: undefined,
248275
},
249276
];
250277

251-
parseString(sampleText, (err, results) => {
252-
results = results.map(result => {
253-
const gcode = result.words.map(word => {
254-
return word.join('');
255-
}).join('');
256-
const cmds = result.cmds;
257-
const comments = result.comments;
258-
return {
259-
gcode,
260-
cmds,
261-
comments,
262-
};
263-
});
278+
parseString(inputText, { lineMode: 'compact' }, (err, results) => {
279+
results = results.map(result => ({
280+
line: result.line,
281+
comments: result.comments,
282+
}));
264283
expect(results).toEqual(expectedResults);
265284
done();
266285
});

0 commit comments

Comments
 (0)