mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
Updates to sentiment, exec and range to handle missing payload properties
This commit is contained in:
parent
9c22a770ef
commit
78d1da5fbc
@ -23,10 +23,13 @@ module.exports = function(RED) {
|
|||||||
var node = this;
|
var node = this;
|
||||||
|
|
||||||
this.on("input", function(msg) {
|
this.on("input", function(msg) {
|
||||||
sentiment(msg.payload, msg.overrides || null, function (err, result) {
|
if (msg.hasOwnProperty("payload")) {
|
||||||
msg.sentiment = result;
|
sentiment(msg.payload, msg.overrides || null, function (err, result) {
|
||||||
node.send(msg);
|
msg.sentiment = result;
|
||||||
});
|
node.send(msg);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else { node.send(msg); } // If no payload - just pass it on.
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("sentiment",SentimentNode);
|
RED.nodes.registerType("sentiment",SentimentNode);
|
||||||
|
@ -66,7 +66,7 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var cl = node.cmd;
|
var cl = node.cmd;
|
||||||
if ((node.addpay === true) && (msg.payload.trim() !== "")) { cl += " "+msg.payload; }
|
if ((node.addpay === true) && ((msg.payload || "").trim() !== "")) { cl += " "+msg.payload; }
|
||||||
if (node.append.trim() !== "") { cl += " "+node.append; }
|
if (node.append.trim() !== "") { cl += " "+node.append; }
|
||||||
if (RED.settings.verbose) { node.log(cl); }
|
if (RED.settings.verbose) { node.log(cl); }
|
||||||
var child = exec(cl, {encoding: 'binary', maxBuffer:10000000}, function (error, stdout, stderr) {
|
var child = exec(cl, {encoding: 'binary', maxBuffer:10000000}, function (error, stdout, stderr) {
|
||||||
|
@ -27,21 +27,24 @@ module.exports = function(RED) {
|
|||||||
var node = this;
|
var node = this;
|
||||||
|
|
||||||
this.on('input', function (msg) {
|
this.on('input', function (msg) {
|
||||||
var n = Number(msg.payload);
|
if (msg.hasOwnProperty("payload")) {
|
||||||
if (!isNaN(n)) {
|
var n = Number(msg.payload);
|
||||||
if (node.action == "clamp") {
|
if (!isNaN(n)) {
|
||||||
if (n < node.minin) { n = node.minin; }
|
if (node.action == "clamp") {
|
||||||
if (n > node.maxin) { n = node.maxin; }
|
if (n < node.minin) { n = node.minin; }
|
||||||
|
if (n > node.maxin) { n = node.maxin; }
|
||||||
|
}
|
||||||
|
if (node.action == "roll") {
|
||||||
|
if (n >= node.maxin) { n = (n - node.minin) % (node.maxin - node.minin) + node.minin; }
|
||||||
|
if (n < node.minin) { n = (n - node.minin) % (node.maxin - node.minin) + node.maxin; }
|
||||||
|
}
|
||||||
|
msg.payload = ((n - node.minin) / (node.maxin - node.minin) * (node.maxout - node.minout)) + node.minout;
|
||||||
|
if (node.round) { msg.payload = Math.round(msg.payload); }
|
||||||
|
node.send(msg);
|
||||||
}
|
}
|
||||||
if (node.action == "roll") {
|
else { node.log("Not a number: "+msg.payload); }
|
||||||
if (n >= node.maxin) { n = (n - node.minin) % (node.maxin - node.minin) + node.minin; }
|
|
||||||
if (n < node.minin) { n = (n - node.minin) % (node.maxin - node.minin) + node.maxin; }
|
|
||||||
}
|
|
||||||
msg.payload = ((n - node.minin) / (node.maxin - node.minin) * (node.maxout - node.minout)) + node.minout;
|
|
||||||
if (node.round) { msg.payload = Math.round(msg.payload); }
|
|
||||||
node.send(msg);
|
|
||||||
}
|
}
|
||||||
else { node.log("Not a number: "+msg.payload); }
|
else { node.send(msg); } // If no payload - just pass it on.
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("range", RangeNode);
|
RED.nodes.registerType("range", RangeNode);
|
||||||
|
@ -37,6 +37,22 @@ describe('sentiment Node', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should pass on msg if no payload', function(done) {
|
||||||
|
var flow = [{id:"jn1",type:"sentiment",wires:[["jn2"]]},
|
||||||
|
{id:"jn2", type:"helper"}];
|
||||||
|
helper.load(sentimentNode, flow, function() {
|
||||||
|
var jn1 = helper.getNode("jn1");
|
||||||
|
var jn2 = helper.getNode("jn2");
|
||||||
|
jn2.on("input", function(msg) {
|
||||||
|
msg.should.not.have.property('sentiment');
|
||||||
|
msg.topic.should.equal("pass on");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
var testString = 'good, great, best, brilliant';
|
||||||
|
jn1.receive({topic:"pass on"});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('should add a positive score for good words', function(done) {
|
it('should add a positive score for good words', function(done) {
|
||||||
var flow = [{id:"jn1",type:"sentiment",wires:[["jn2"]]},
|
var flow = [{id:"jn1",type:"sentiment",wires:[["jn2"]]},
|
||||||
{id:"jn2", type:"helper"}];
|
{id:"jn2", type:"helper"}];
|
||||||
|
@ -24,7 +24,7 @@ describe('range Node', function() {
|
|||||||
beforeEach(function(done) {
|
beforeEach(function(done) {
|
||||||
helper.startServer(done);
|
helper.startServer(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function(done) {
|
afterEach(function(done) {
|
||||||
helper.unload();
|
helper.unload();
|
||||||
helper.stopServer(done);
|
helper.stopServer(done);
|
||||||
@ -39,7 +39,7 @@ describe('range Node', function() {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run a generic range test
|
* Run a generic range test
|
||||||
* @param action - scale/clamp (range limit)/roll (modulo): what action to choose
|
* @param action - scale/clamp (range limit)/roll (modulo): what action to choose
|
||||||
@ -69,63 +69,82 @@ describe('range Node', function() {
|
|||||||
rangeNode1.receive({payload:aPayload});
|
rangeNode1.receive({payload:aPayload});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
it('ranges numbers up tenfold', function(done) {
|
it('ranges numbers up tenfold', function(done) {
|
||||||
genericRangeTest("scale", 0, 100, 0, 1000, false, 50, 500, done);
|
genericRangeTest("scale", 0, 100, 0, 1000, false, 50, 500, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('ranges numbers down such as centimetres to metres', function(done) {
|
it('ranges numbers down such as centimetres to metres', function(done) {
|
||||||
genericRangeTest("scale", 0, 100, 0, 1, false, 55, 0.55, done);
|
genericRangeTest("scale", 0, 100, 0, 1, false, 55, 0.55, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('wraps numbers down say for degree/rotation reading 1/2', function(done) {
|
it('wraps numbers down say for degree/rotation reading 1/2', function(done) {
|
||||||
genericRangeTest("roll", 0, 10, 0, 360, true, 15, 180, done); // 1/2 around wrap => "one and a half turns"
|
genericRangeTest("roll", 0, 10, 0, 360, true, 15, 180, done); // 1/2 around wrap => "one and a half turns"
|
||||||
});
|
});
|
||||||
|
|
||||||
it('wraps numbers around say for degree/rotation reading 1/3', function(done) {
|
it('wraps numbers around say for degree/rotation reading 1/3', function(done) {
|
||||||
genericRangeTest("roll", 0, 10, 0, 360, true, 13.3333, 120, done); // 1/3 around wrap => "one and a third turns"
|
genericRangeTest("roll", 0, 10, 0, 360, true, 13.3333, 120, done); // 1/3 around wrap => "one and a third turns"
|
||||||
});
|
});
|
||||||
|
|
||||||
it('wraps numbers around say for degree/rotation reading 1/4', function(done) {
|
it('wraps numbers around say for degree/rotation reading 1/4', function(done) {
|
||||||
genericRangeTest("roll", 0, 10, 0, 360, true, 12.5, 90, done); // 1/4 around wrap => "one and a quarter turns"
|
genericRangeTest("roll", 0, 10, 0, 360, true, 12.5, 90, done); // 1/4 around wrap => "one and a quarter turns"
|
||||||
});
|
});
|
||||||
|
|
||||||
it('wraps numbers around say for degree/rotation reading 1/4', function(done) {
|
it('wraps numbers around say for degree/rotation reading 1/4', function(done) {
|
||||||
genericRangeTest("roll", 0, 10, 0, 360, true, 12.5, 90, done); // 1/4 around wrap => "one and a quarter turns"
|
genericRangeTest("roll", 0, 10, 0, 360, true, 12.5, 90, done); // 1/4 around wrap => "one and a quarter turns"
|
||||||
});
|
});
|
||||||
|
|
||||||
it('wraps numbers down say for degree/rotation reading 1/4', function(done) {
|
it('wraps numbers down say for degree/rotation reading 1/4', function(done) {
|
||||||
genericRangeTest("roll", 0, 10, 0, 360, true, -12.5, 270, done); // 1/4 backwards wrap => "one and a quarter turns backwards"
|
genericRangeTest("roll", 0, 10, 0, 360, true, -12.5, 270, done); // 1/4 backwards wrap => "one and a quarter turns backwards"
|
||||||
});
|
});
|
||||||
|
|
||||||
it('clamps numbers within a range - over max', function(done) {
|
it('clamps numbers within a range - over max', function(done) {
|
||||||
genericRangeTest("clamp", 0, 10, 0, 1000, false, 111, 1000, done);
|
genericRangeTest("clamp", 0, 10, 0, 1000, false, 111, 1000, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('clamps numbers within a range - below min', function(done) {
|
it('clamps numbers within a range - below min', function(done) {
|
||||||
genericRangeTest("clamp", 0, 10, 0, 1000, false, -1, 0, done);
|
genericRangeTest("clamp", 0, 10, 0, 1000, false, -1, 0, done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('just passes on msg if payload not present', function(done) {
|
||||||
|
var flow = [{"id":"rangeNode1","type":"range","minin":0,"maxin":100,"minout":0,"maxout":100,"action":"scale","round":true,"name":"rangeNode","wires":[["helperNode1"]]},
|
||||||
|
{id:"helperNode1", type:"helper", wires:[]}];
|
||||||
|
helper.load(rangeNode, flow, function() {
|
||||||
|
var rangeNode1 = helper.getNode("rangeNode1");
|
||||||
|
var helperNode1 = helper.getNode("helperNode1");
|
||||||
|
helperNode1.on("input", function(msg) {
|
||||||
|
try {
|
||||||
|
msg.should.not.have.property('payload');
|
||||||
|
msg.topic.should.equal("pass on");
|
||||||
|
done();
|
||||||
|
} catch(err) {
|
||||||
|
done(err);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
rangeNode1.receive({topic:"pass on"});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('reports if input is not a number', function(done) {
|
it('reports if input is not a number', function(done) {
|
||||||
var flow = [{"id":"rangeNode1","type":"range","minin":0,"maxin":0,"minout":0,"maxout":0,"action":"scale","round":true,"name":"rangeNode","wires":[["helperNode1"]]},
|
var flow = [{"id":"rangeNode1","type":"range","minin":0,"maxin":0,"minout":0,"maxout":0,"action":"scale","round":true,"name":"rangeNode","wires":[["helperNode1"]]},
|
||||||
{id:"helperNode1", type:"helper", wires:[]}];
|
{id:"helperNode1", type:"helper", wires:[]}];
|
||||||
helper.load(rangeNode, flow, function() {
|
helper.load(rangeNode, flow, function() {
|
||||||
var rangeNode1 = helper.getNode("rangeNode1");
|
var rangeNode1 = helper.getNode("rangeNode1");
|
||||||
var helperNode1 = helper.getNode("helperNode1");
|
var helperNode1 = helper.getNode("helperNode1");
|
||||||
|
|
||||||
var sinon = require('sinon');
|
var sinon = require('sinon');
|
||||||
sinon.stub(rangeNode1, 'log', function(log) {
|
sinon.stub(rangeNode1, 'log', function(log) {
|
||||||
if(log.indexOf("Not a number") > -1) {
|
if(log.indexOf("Not a number") > -1) {
|
||||||
done();
|
done();
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
should.fail(null, null, "Non-number inputs should be reported!");
|
should.fail(null, null, "Non-number inputs should be reported!");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
done(err);
|
done(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
rangeNode1.receive({payload:"NOT A NUMBER"});
|
rangeNode1.receive({payload:"NOT A NUMBER"});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user