Skip to content

Commit d254599

Browse files
committed
Add parseFileSync and parseStringSync
1 parent 0412c75 commit d254599

File tree

3 files changed

+196
-11
lines changed

3 files changed

+196
-11
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ parser.parseFile(file, function(err, results) {
2222
console.log(results);
2323
});
2424

25+
// Synchronous version of parseFile.
26+
results = parser.parseFileSync(file);
27+
2528
// Parse from stream
2629
var stream = fs.createReadStream(file, { encoding: 'utf8' });
2730
parser.parseStream(stream, function(err, results) {
@@ -33,6 +36,9 @@ var str = fs.readFileSync(file, 'utf8');
3336
parser.parseString(str, function(err, results) {
3437
console.log(results);
3538
});
39+
40+
// Synchronous version of parseString.
41+
results = parser.parseStringSync(file);
3642
```
3743

3844
## Advanced Usage

src/index.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ const parseFile = (file, options, callback = noop) => {
168168
return parseStream(s, options, callback);
169169
};
170170

171+
const parseFileSync = (file, options) => {
172+
return parseStringSync(fs.readFileSync(file, 'utf8'), options);
173+
};
174+
171175
// @param {string} str The G-code text string
172176
// @param {options} options The options object
173177
// @param {function} callback The callback function
@@ -179,6 +183,27 @@ const parseString = (str, options, callback = noop) => {
179183
return parseStream(streamify(str), options, callback);
180184
};
181185

186+
const parseStringSync = (str, options) => {
187+
const { noParseLine = false } = { ...options };
188+
const results = [];
189+
const lines = str.split('\n');
190+
191+
for (let i = 0; i < lines.length; ++i) {
192+
const line = lines[i].trim();
193+
if (line.length === 0) {
194+
continue;
195+
}
196+
const result = parseLine(line, { noParseLine });
197+
results.push(result);
198+
}
199+
200+
return results;
201+
};
202+
203+
// @param {string} str The G-code text string
204+
// @param {options} options The options object
205+
206+
182207
class GCodeLineStream extends Transform {
183208
state = {
184209
lineCount: 0,
@@ -275,5 +300,7 @@ export {
275300
parseLine,
276301
parseStream,
277302
parseFile,
278-
parseString
303+
parseFileSync,
304+
parseString,
305+
parseStringSync
279306
};

test/index.js

Lines changed: 162 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import chai from 'chai';
22
import fs from 'fs';
3-
import { GCodeParser, parseFile, parseString, parseStream } from '../lib';
3+
import {
4+
GCodeParser,
5+
parseStream,
6+
parseString,
7+
parseStringSync,
8+
parseFile,
9+
parseFileSync
10+
} from '../lib';
411

512
const expect = chai.expect;
613
const should = chai.should();
714

8-
describe('G-code Parser', () => {
15+
describe('gcode-parser', () => {
916
describe('Pass a null value as the first argument', () => {
1017
it('should call parseString\'s callback.', (done) => {
1118
parseString(null, (err, results) => {
@@ -138,7 +145,7 @@ describe('G-code Parser', () => {
138145
});
139146
});
140147

141-
describe('parseFile / parseStream / parseString', () => {
148+
describe('parseStream()', () => {
142149
const expectedResults = [
143150
{
144151
line: 'G0 X-5 Y0 Z0 F200',
@@ -170,30 +177,175 @@ describe('G-code Parser', () => {
170177
}
171178
];
172179

173-
it('should get the expected results in the parseFile\'s callback.', (done) => {
174-
parseFile('test/fixtures/circle.gcode', (err, results) => {
180+
it('should get expected results in the callback.', (done) => {
181+
const stream = fs.createReadStream('test/fixtures/circle.gcode', { encoding: 'utf8' });
182+
parseStream(stream, (err, results) => {
175183
expect(results).to.deep.equal(expectedResults);
176184
done();
177185
});
178186
});
187+
});
179188

180-
it('should get the expected results in the parseStream\'s callback.', (done) => {
181-
const stream = fs.createReadStream('test/fixtures/circle.gcode', { encoding: 'utf8' });
182-
parseStream(stream, (err, results) => {
189+
describe('parseString()', () => {
190+
const expectedResults = [
191+
{
192+
line: 'G0 X-5 Y0 Z0 F200',
193+
words: [['G', 0], ['X', -5], ['Y', 0], ['Z', 0], ['F', 200]]
194+
},
195+
{
196+
line: 'G2 X0 Y5 I5 J0 F200',
197+
words: [['G', 2], ['X', 0], ['Y', 5], ['I', 5], ['J', 0], ['F', 200]]
198+
},
199+
{
200+
line: 'G02 X5 Y0 I0 J-5',
201+
words: [['G', 2], ['X', 5], ['Y', 0], ['I', 0], ['J', -5]]
202+
},
203+
{
204+
line: 'G02 X0 Y-5 I-5 J0',
205+
words: [['G', 2], ['X', 0], ['Y',-5], ['I', -5], ['J', 0]]
206+
},
207+
{
208+
line: 'G02 X-5 Y0 I0 J5',
209+
words: [['G', 2], ['X', -5], ['Y', 0], ['I', 0], ['J', 5]]
210+
},
211+
{
212+
line: 'G01 Z1 F500',
213+
words: [['G', 1], ['Z', 1], ['F', 500]]
214+
},
215+
{
216+
line: 'G00 X0 Y0 Z5',
217+
words: [['G', 0], ['X', 0], ['Y', 0], ['Z', 5]]
218+
}
219+
];
220+
221+
it('should get expected results in the callback.', (done) => {
222+
const str = fs.readFileSync('test/fixtures/circle.gcode', 'utf8');
223+
parseString(str, (err, results) => {
183224
expect(results).to.deep.equal(expectedResults);
184225
done();
185226
});
186227
});
228+
});
229+
230+
describe('parseStringSync()', () => {
231+
const expectedResults = [
232+
{
233+
line: 'G0 X-5 Y0 Z0 F200',
234+
words: [['G', 0], ['X', -5], ['Y', 0], ['Z', 0], ['F', 200]]
235+
},
236+
{
237+
line: 'G2 X0 Y5 I5 J0 F200',
238+
words: [['G', 2], ['X', 0], ['Y', 5], ['I', 5], ['J', 0], ['F', 200]]
239+
},
240+
{
241+
line: 'G02 X5 Y0 I0 J-5',
242+
words: [['G', 2], ['X', 5], ['Y', 0], ['I', 0], ['J', -5]]
243+
},
244+
{
245+
line: 'G02 X0 Y-5 I-5 J0',
246+
words: [['G', 2], ['X', 0], ['Y',-5], ['I', -5], ['J', 0]]
247+
},
248+
{
249+
line: 'G02 X-5 Y0 I0 J5',
250+
words: [['G', 2], ['X', -5], ['Y', 0], ['I', 0], ['J', 5]]
251+
},
252+
{
253+
line: 'G01 Z1 F500',
254+
words: [['G', 1], ['Z', 1], ['F', 500]]
255+
},
256+
{
257+
line: 'G00 X0 Y0 Z5',
258+
words: [['G', 0], ['X', 0], ['Y', 0], ['Z', 5]]
259+
}
260+
];
187261

188-
it('should get the expected results in the parseString\'s callback.', (done) => {
262+
it('should return expected results.', (done) => {
189263
const str = fs.readFileSync('test/fixtures/circle.gcode', 'utf8');
190-
parseString(str, (err, results) => {
264+
const results = parseStringSync(str);
265+
expect(results).to.deep.equal(expectedResults);
266+
done();
267+
});
268+
});
269+
270+
describe('parseFile()', () => {
271+
const expectedResults = [
272+
{
273+
line: 'G0 X-5 Y0 Z0 F200',
274+
words: [['G', 0], ['X', -5], ['Y', 0], ['Z', 0], ['F', 200]]
275+
},
276+
{
277+
line: 'G2 X0 Y5 I5 J0 F200',
278+
words: [['G', 2], ['X', 0], ['Y', 5], ['I', 5], ['J', 0], ['F', 200]]
279+
},
280+
{
281+
line: 'G02 X5 Y0 I0 J-5',
282+
words: [['G', 2], ['X', 5], ['Y', 0], ['I', 0], ['J', -5]]
283+
},
284+
{
285+
line: 'G02 X0 Y-5 I-5 J0',
286+
words: [['G', 2], ['X', 0], ['Y',-5], ['I', -5], ['J', 0]]
287+
},
288+
{
289+
line: 'G02 X-5 Y0 I0 J5',
290+
words: [['G', 2], ['X', -5], ['Y', 0], ['I', 0], ['J', 5]]
291+
},
292+
{
293+
line: 'G01 Z1 F500',
294+
words: [['G', 1], ['Z', 1], ['F', 500]]
295+
},
296+
{
297+
line: 'G00 X0 Y0 Z5',
298+
words: [['G', 0], ['X', 0], ['Y', 0], ['Z', 5]]
299+
}
300+
];
301+
302+
it('should get expected results in the callback.', (done) => {
303+
parseFile('test/fixtures/circle.gcode', (err, results) => {
191304
expect(results).to.deep.equal(expectedResults);
192305
done();
193306
});
194307
});
195308
});
196309

310+
describe('parseFileSync()', () => {
311+
const expectedResults = [
312+
{
313+
line: 'G0 X-5 Y0 Z0 F200',
314+
words: [['G', 0], ['X', -5], ['Y', 0], ['Z', 0], ['F', 200]]
315+
},
316+
{
317+
line: 'G2 X0 Y5 I5 J0 F200',
318+
words: [['G', 2], ['X', 0], ['Y', 5], ['I', 5], ['J', 0], ['F', 200]]
319+
},
320+
{
321+
line: 'G02 X5 Y0 I0 J-5',
322+
words: [['G', 2], ['X', 5], ['Y', 0], ['I', 0], ['J', -5]]
323+
},
324+
{
325+
line: 'G02 X0 Y-5 I-5 J0',
326+
words: [['G', 2], ['X', 0], ['Y',-5], ['I', -5], ['J', 0]]
327+
},
328+
{
329+
line: 'G02 X-5 Y0 I0 J5',
330+
words: [['G', 2], ['X', -5], ['Y', 0], ['I', 0], ['J', 5]]
331+
},
332+
{
333+
line: 'G01 Z1 F500',
334+
words: [['G', 1], ['Z', 1], ['F', 500]]
335+
},
336+
{
337+
line: 'G00 X0 Y0 Z5',
338+
words: [['G', 0], ['X', 0], ['Y', 0], ['Z', 5]]
339+
}
340+
];
341+
342+
it('should return expected results.', (done) => {
343+
const results = parseFileSync('test/fixtures/circle.gcode');
344+
expect(results).to.deep.equal(expectedResults);
345+
done();
346+
});
347+
});
348+
197349
describe('More examples', () => {
198350
it('should contain the line number.', (done) => {
199351
parseFile('test/fixtures/circle-inch.gcode', (err, list) => {

0 commit comments

Comments
 (0)