mirror of
https://github.com/node-red/node-red-nodes.git
synced 2025-03-01 10:37:43 +00:00
Added "reduce" option to the Smooth function node (#577)
This commit is contained in:
committed by
Dave Conway-Jones
parent
1f0aa6908c
commit
38feb99072
@@ -316,5 +316,79 @@ describe('smooth node', function() {
|
||||
n1.emit("input", {payload:9, topic:"B"});
|
||||
});
|
||||
});
|
||||
|
||||
it("should reduce the number of messages by averaging if asked", function(done) {
|
||||
var flow = [{"id":"n1", "type":"smooth", action:"mean", count:"5", reduce:"true", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(testNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var c = 0;
|
||||
n2.on("input", function(msg) {
|
||||
c += 1;
|
||||
if (c === 1) { msg.should.have.a.property("payload", 3); }
|
||||
else if (c === 2) { msg.should.have.a.property("payload", 6); done(); }
|
||||
else if (c > 2) { done(new Error("should not emit more than two messages.")); }
|
||||
});
|
||||
n1.emit("input", {payload:1});
|
||||
n1.emit("input", {payload:2});
|
||||
n1.emit("input", {payload:3});
|
||||
n1.emit("input", {payload:4});
|
||||
n1.emit("input", {payload:5});
|
||||
n1.emit("input", {payload:6});
|
||||
n1.emit("input", {payload:7});
|
||||
n1.emit("input", {payload:8});
|
||||
n1.emit("input", {payload:9})
|
||||
; n1.emit("input", {payload:0});
|
||||
});
|
||||
});
|
||||
it("should reduce the number of messages by max value, if asked", function(done) {
|
||||
var flow = [{"id":"n1", "type":"smooth", action:"max", count:"5", reduce:"true", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(testNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var c = 0;
|
||||
n2.on("input", function(msg) {
|
||||
c += 1;
|
||||
if (c === 1) { msg.should.have.a.property("payload", 5); }
|
||||
else if (c === 2) { msg.should.have.a.property("payload", 9); done(); }
|
||||
else if (c > 2) { done(new Error("should not emit more than two messages.")); }
|
||||
});
|
||||
n1.emit("input", {payload:1});
|
||||
n1.emit("input", {payload:2});
|
||||
n1.emit("input", {payload:3});
|
||||
n1.emit("input", {payload:4});
|
||||
n1.emit("input", {payload:5});
|
||||
n1.emit("input", {payload:6});
|
||||
n1.emit("input", {payload:7});
|
||||
n1.emit("input", {payload:8});
|
||||
n1.emit("input", {payload:9});
|
||||
n1.emit("input", {payload:0});
|
||||
});
|
||||
});
|
||||
it("should reduce the number of messages by min value, if asked", function(done) {
|
||||
var flow = [{"id":"n1", "type":"smooth", action:"min", count:"5", reduce:"true", wires:[["n2"]] },
|
||||
{id:"n2", type:"helper"} ];
|
||||
helper.load(testNode, flow, function() {
|
||||
var n1 = helper.getNode("n1");
|
||||
var n2 = helper.getNode("n2");
|
||||
var c = 0;
|
||||
n2.on("input", function(msg) {
|
||||
c += 1;
|
||||
if (c === 1) { msg.should.have.a.property("payload", 1); }
|
||||
else if (c === 2) { msg.should.have.a.property("payload", 0); done(); }
|
||||
else if (c > 2) { done(new Error("should not emit more than two messages.")); }
|
||||
});
|
||||
n1.emit("input", {payload:1});
|
||||
n1.emit("input", {payload:2});
|
||||
n1.emit("input", {payload:3});
|
||||
n1.emit("input", {payload:4});
|
||||
n1.emit("input", {payload:5});
|
||||
n1.emit("input", {payload:6});
|
||||
n1.emit("input", {payload:7});
|
||||
n1.emit("input", {payload:8});
|
||||
n1.emit("input", {payload:9});
|
||||
n1.emit("input", {payload:0});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user