1
0
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:
dceejay 2015-03-31 08:35:56 +01:00
parent 9c22a770ef
commit 78d1da5fbc
5 changed files with 74 additions and 33 deletions

View File

@ -23,10 +23,13 @@ module.exports = function(RED) {
var node = this;
this.on("input", function(msg) {
sentiment(msg.payload, msg.overrides || null, function (err, result) {
msg.sentiment = result;
node.send(msg);
});
if (msg.hasOwnProperty("payload")) {
sentiment(msg.payload, msg.overrides || null, function (err, result) {
msg.sentiment = result;
node.send(msg);
});
}
else { node.send(msg); } // If no payload - just pass it on.
});
}
RED.nodes.registerType("sentiment",SentimentNode);

View File

@ -66,7 +66,7 @@ module.exports = function(RED) {
}
else {
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 (RED.settings.verbose) { node.log(cl); }
var child = exec(cl, {encoding: 'binary', maxBuffer:10000000}, function (error, stdout, stderr) {

View File

@ -27,21 +27,24 @@ module.exports = function(RED) {
var node = this;
this.on('input', function (msg) {
var n = Number(msg.payload);
if (!isNaN(n)) {
if (node.action == "clamp") {
if (n < node.minin) { n = node.minin; }
if (n > node.maxin) { n = node.maxin; }
if (msg.hasOwnProperty("payload")) {
var n = Number(msg.payload);
if (!isNaN(n)) {
if (node.action == "clamp") {
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") {
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.log("Not a number: "+msg.payload); }
else { node.send(msg); } // If no payload - just pass it on.
});
}
RED.nodes.registerType("range", RangeNode);

View File

@ -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) {
var flow = [{id:"jn1",type:"sentiment",wires:[["jn2"]]},
{id:"jn2", type:"helper"}];

View File

@ -106,6 +106,25 @@ describe('range Node', function() {
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) {
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:[]}];