Skip to content

Commit 9e4de0b

Browse files
committed
Merge branch 'dev'
2 parents 25d20b0 + ef26fc2 commit 9e4de0b

File tree

6 files changed

+39
-36
lines changed

6 files changed

+39
-36
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2015 Cheton Wu
3+
Copyright (c) 2015-2016 Cheton Wu
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ parser.parseStream(stream, function(err, data) {
2222
console.log(data);
2323
});
2424

25-
// Parse from text string
25+
// Parse from string
2626
var text = fs.readFileSync(file, 'utf8');
27-
parser.parseText(text, function(err, data) {
27+
parser.parseString(text, function(err, data) {
2828
console.log(data);
2929
});
3030
```

dist/index.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict';
22

3-
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
4-
53
Object.defineProperty(exports, "__esModule", {
64
value: true
75
});
8-
exports.parseText = exports.parseFile = exports.parseStream = exports.GCodeParser = undefined;
6+
exports.parseString = exports.parseFile = exports.parseStream = exports.GCodeParser = undefined;
7+
8+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
99

1010
var _lodash = require('lodash');
1111

@@ -35,8 +35,8 @@ var streamify = function streamify(text) {
3535
};
3636

3737
var stripComments = function stripComments(s) {
38-
var re1 = /^\s+|\s+$/g; // Strip leading and trailing spaces
39-
var re2 = /\s*[#;].*$/g; // Strip everything after # or ; to the end of the line, including preceding spaces
38+
var re1 = /\s*[%#;].*/g; // Strip everything after %, #, or ; to the end of the line, including preceding spaces
39+
var re2 = /\s*\(.*\)/g; // Remove anything inside the parentheses
4040
return s.replace(re1, '').replace(re2, '');
4141
};
4242

@@ -84,17 +84,18 @@ var GCodeParser = function (_Transform) {
8484
chunk = chunk.toString(encoding);
8585
}
8686

87-
var lines = chunk.split(/\r\n|\r|\n/g);
87+
var lines = stripComments(chunk).split(/\r\n|\r|\n/g);
88+
8889
_lodash2.default.each(lines, function (line) {
89-
line = _lodash2.default.trim(stripComments(line));
90-
if (line.length === 0) {
90+
var list = removeSpaces(line).match(/([a-zA-Z][0-9\+\-\.]*)|(\*[0-9]+)/igm) || [];
91+
92+
if (list.length === 0) {
9193
return;
9294
}
9395

9496
var n = undefined; // Line number
9597
var cs = undefined; // Checksum
9698
var words = [];
97-
var list = removeSpaces(line).match(/([a-zA-Z][0-9\+\-\.]*)|(\*[0-9]+)/igm) || [];
9899

99100
_lodash2.default.each(list, function (word) {
100101
var letter = word[0].toUpperCase();
@@ -182,12 +183,12 @@ var parseFile = function parseFile(file, callback) {
182183
return parseStream(s, callback);
183184
};
184185

185-
var parseText = function parseText(text, callback) {
186+
var parseString = function parseString(text, callback) {
186187
var s = streamify(text);
187188
return parseStream(s, callback);
188189
};
189190

190191
exports.GCodeParser = GCodeParser;
191192
exports.parseStream = parseStream;
192193
exports.parseFile = parseFile;
193-
exports.parseText = parseText;
194+
exports.parseString = parseString;

index.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ const streamify = (text) => {
1010
};
1111

1212
const stripComments = (s) => {
13-
let re1 = /^\s+|\s+$/g; // Strip leading and trailing spaces
14-
let re2 = /\s*[#;].*$/g; // Strip everything after # or ; to the end of the line, including preceding spaces
13+
const re1 = /\s*[%#;].*/g; // Strip everything after %, #, or ; to the end of the line, including preceding spaces
14+
const re2 = /\s*\(.*\)/g; // Remove anything inside the parentheses
1515
return s.replace(re1, '').replace(re2, '');
1616
};
1717

@@ -49,18 +49,20 @@ class GCodeParser extends Transform {
4949
chunk = chunk.toString(encoding);
5050
}
5151

52-
let lines = chunk.split(/\r\n|\r|\n/g);
52+
const lines = stripComments(chunk)
53+
.split(/\r\n|\r|\n/g);
54+
5355
_.each(lines, (line) => {
54-
line = _.trim(stripComments(line));
55-
if (line.length === 0) {
56+
const list = removeSpaces(line)
57+
.match(/([a-zA-Z][0-9\+\-\.]*)|(\*[0-9]+)/igm) || [];
58+
59+
if (list.length === 0) {
5660
return;
5761
}
5862

5963
let n; // Line number
6064
let cs; // Checksum
6165
let words = [];
62-
let list = removeSpaces(line)
63-
.match(/([a-zA-Z][0-9\+\-\.]*)|(\*[0-9]+)/igm) || [];
6466

6567
_.each(list, (word) => {
6668
let letter = word[0].toUpperCase();
@@ -143,7 +145,7 @@ const parseFile = (file, callback) => {
143145
return parseStream(s, callback);
144146
};
145147

146-
const parseText = (text, callback) => {
148+
const parseString = (text, callback) => {
147149
let s = streamify(text);
148150
return parseStream(s, callback);
149151
};
@@ -152,5 +154,5 @@ export {
152154
GCodeParser,
153155
parseStream,
154156
parseFile,
155-
parseText
157+
parseString
156158
};

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.5.1",
3+
"version": "0.6.0",
44
"description": "A G-code parser for Node.js",
55
"author": "Cheton Wu <[email protected]>",
66
"homepage": "https://github.com/cheton/gcode-parser",

test/index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import chai from 'chai';
22
import fs from 'fs';
3-
import { GCodeParser, parseFile, parseText, parseStream } from '../dist/';
3+
import { GCodeParser, parseFile, parseString, parseStream } from '../dist/';
44
import _ from 'lodash';
55

66
const expect = chai.expect;
77
const should = chai.should();
88

99
describe('G-code Parser', (done) => {
1010
describe('Pass a null value as the first argument', (done) => {
11-
it('should call parseText\'s callback.', (done) => {
12-
parseText(null, (err, results) => {
11+
it('should call parseString\'s callback.', (done) => {
12+
parseString(null, (err, results) => {
1313
expect(err).to.be.okay;
1414
done();
1515
});
@@ -31,7 +31,7 @@ describe('G-code Parser', (done) => {
3131
describe('Pass an empty text as the first argument', (done) => {
3232
it('should get empty results.', (done) => {
3333
let sampleText = '';
34-
parseText(sampleText, (err, results) => {
34+
parseString(sampleText, (err, results) => {
3535
expect(results.length).to.be.empty;
3636
done();
3737
});
@@ -54,7 +54,7 @@ describe('G-code Parser', (done) => {
5454
' ' // empty line
5555
].join('\n');
5656

57-
parseText(sampleText, (err, results) => {
57+
parseString(sampleText, (err, results) => {
5858
console.log(sampleText, err, results);
5959

6060
expect(results.length).to.be.empty;
@@ -73,7 +73,7 @@ console.log(sampleText, err, results);
7373
});
7474
});
7575

76-
describe('parseStream / parseText / parseFile', (done) => {
76+
describe('parseFile / parseStream / parseString', (done) => {
7777
let expectedResults = [
7878
{
7979
line: 'G0 X-5 Y0 Z0 F200',
@@ -112,17 +112,17 @@ console.log(sampleText, err, results);
112112
});
113113
});
114114

115-
it('should get the expected results in the parseText\'s callback.', (done) => {
116-
let text = fs.readFileSync('test/fixtures/circle.gcode', 'utf8');
117-
parseText(text, (err, results) => {
115+
it('should get the expected results in the parseStream\'s callback.', (done) => {
116+
let stream = fs.createReadStream('test/fixtures/circle.gcode', { encoding: 'utf8' });
117+
parseStream(stream, (err, results) => {
118118
expect(results).to.deep.equal(expectedResults);
119119
done();
120120
});
121121
});
122122

123-
it('should get the expected results in the parseStream\'s callback.', (done) => {
124-
let stream = fs.createReadStream('test/fixtures/circle.gcode', { encoding: 'utf8' });
125-
parseStream(stream, (err, results) => {
123+
it('should get the expected results in the parseString\'s callback.', (done) => {
124+
let text = fs.readFileSync('test/fixtures/circle.gcode', 'utf8');
125+
parseString(text, (err, results) => {
126126
expect(results).to.deep.equal(expectedResults);
127127
done();
128128
});

0 commit comments

Comments
 (0)