Skip to content

Commit 11fd5e3

Browse files
committed
Adds handlers option
1 parent e4f3cec commit 11fd5e3

File tree

4 files changed

+78
-17
lines changed

4 files changed

+78
-17
lines changed

README.md

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,22 @@
99
## Usage
1010

1111
```js
12-
var fs = require('fs');
13-
var util = require('util');
1412
var GCodeInterpreter = require('gcode-interpreter').GCodeInterpreter;
1513

1614
var GCodeRunner = function() {
17-
GCodeInterpreter.call(this);
15+
var handlers = {
16+
'G0': (args) => {
17+
console.log('G0', args);
18+
},
19+
'G1': (args) => {
20+
console.log('G1', args);
21+
}
22+
};
23+
24+
return new GCodeInterpreter({ handlers: handlers })
1825
};
19-
util.inherits(GCodeRunner, GCodeInterpreter);
2026

21-
GCodeRunner.prototype.G0 = function(args) {
22-
console.log('G0', args);
23-
};
24-
25-
GCodeRunner.prototype.G1 = function(args) {
26-
console.log('G1', args);
27-
};
28-
29-
var runner = new GCodeRunner();
27+
var runner = new GCodeRunner()
3028

3129
// Interpret G-code from file
3230
var file = 'example.nc';

dist/index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ var interpret = function interpret(self, data) {
6060
var word = words[0] || [];
6161
var letter = word[0];
6262
var arg = word[1];
63-
var cmd = (letter + arg).replace('.', '_');
63+
var cmd = letter + arg;
6464
var args = {};
6565

6666
if (_lodash2.default.includes(['G', 'M'], letter)) {
@@ -80,6 +80,11 @@ var interpret = function interpret(self, data) {
8080
args = _lodash2.default.zipObject(words); // returns an object composed from arrays of property names and values.
8181
}
8282

83+
if (typeof self.handlers[cmd] === 'function') {
84+
var func = self.handlers[cmd];
85+
func(args);
86+
}
87+
8388
if (typeof self[cmd] === 'function') {
8489
var func = self[cmd].bind(self);
8590
func(args);
@@ -88,14 +93,20 @@ var interpret = function interpret(self, data) {
8893
};
8994

9095
var GCodeInterpreter = (function () {
91-
function GCodeInterpreter() {
96+
function GCodeInterpreter(options) {
9297
_classCallCheck(this, GCodeInterpreter);
9398

9499
this.cmd = '';
100+
this.handlers = {};
95101
this._callbacks = {
96102
'data': [],
97103
'end': []
98104
};
105+
106+
options = options || {};
107+
options.handlers = options.handlers || {};
108+
109+
this.handlers = options.handlers;
99110
}
100111

101112
_createClass(GCodeInterpreter, [{

index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const interpret = (self, data) => {
3838
let word = words[0] || [];
3939
let letter = word[0];
4040
let arg = word[1];
41-
let cmd = (letter + arg).replace('.', '_');
41+
let cmd = (letter + arg);
4242
let args = {};
4343

4444
if (_.includes(['G', 'M'], letter)) {
@@ -58,6 +58,11 @@ const interpret = (self, data) => {
5858
args = _.zipObject(words); // returns an object composed from arrays of property names and values.
5959
}
6060

61+
if (typeof self.handlers[cmd] === 'function') {
62+
let func = self.handlers[cmd];
63+
func(args);
64+
}
65+
6166
if (typeof self[cmd] === 'function') {
6267
let func = self[cmd].bind(self);
6368
func(args);
@@ -67,11 +72,19 @@ const interpret = (self, data) => {
6772

6873
class GCodeInterpreter {
6974
cmd = '';
75+
handlers = {};
76+
7077
_callbacks = {
7178
'data': [],
7279
'end': []
7380
};
7481

82+
constructor(options) {
83+
options = options || {};
84+
options.handlers = options.handlers || {};
85+
86+
this.handlers = options.handlers;
87+
}
7588
on(evt, callback) {
7689
this._callbacks[evt] && this._callbacks[evt].push(callback);
7790
return this;

test/index.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,46 @@ describe('G-code Interpreter', (done) => {
5959
});
6060
});
6161

62-
describe('G-code: circle', (done) => {
62+
describe('G-code: circle (calls GCodeInterpreter)', (done) => {
63+
it('should call each function with the expected number of times.', (done) => {
64+
let calls = {};
65+
66+
class GCodeRunner {
67+
loadFile(file, callback) {
68+
const handlers = {
69+
'G0': (args) => {
70+
calls.G0 = (calls.G0 || 0) + 1;
71+
expect(args).to.be.an('object');
72+
},
73+
'G1': (args) => {
74+
calls.G1 = (calls.G1 || 0) + 1;
75+
expect(args).to.be.an('object');
76+
},
77+
'G2': (args) => {
78+
calls.G2 = (calls.G2 || 0) + 1;
79+
expect(args).to.be.an('object');
80+
}
81+
};
82+
83+
let interpreter = new GCodeInterpreter({ handlers: handlers })
84+
interpreter.interpretFile(file, callback);
85+
86+
return interpreter;
87+
}
88+
};
89+
90+
new GCodeRunner().loadFile('test/fixtures/circle.nc', (err, results) => {
91+
expect(calls.G0).to.equal(2);
92+
expect(calls.G1).to.equal(1);
93+
expect(calls.G2).to.equal(4);
94+
done();
95+
});
96+
});
97+
98+
});
99+
100+
101+
describe('G-code: circle (extends GCodeInterpreter)', (done) => {
63102
let calls = {};
64103

65104
class GCodeRunner extends GCodeInterpreter {

0 commit comments

Comments
 (0)