From 53c96200380cdf173196dd5350f5890bc99d96d7 Mon Sep 17 00:00:00 2001 From: Sineos Date: Thu, 11 Jul 2019 20:41:06 +0200 Subject: [PATCH] Add invert node --- nodes/combine-invert.html | 38 ++++++++++++ nodes/combine-invert.js | 25 ++++++++ nodes/icons/invert.png | Bin 0 -> 1227 bytes package.json | 6 +- test/invert_spec.js | 122 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 189 insertions(+), 2 deletions(-) create mode 100644 nodes/combine-invert.html create mode 100644 nodes/combine-invert.js create mode 100644 nodes/icons/invert.png create mode 100644 test/invert_spec.js diff --git a/nodes/combine-invert.html b/nodes/combine-invert.html new file mode 100644 index 0000000..44e636b --- /dev/null +++ b/nodes/combine-invert.html @@ -0,0 +1,38 @@ + + + + + \ No newline at end of file diff --git a/nodes/combine-invert.js b/nodes/combine-invert.js new file mode 100644 index 0000000..ff9d766 --- /dev/null +++ b/nodes/combine-invert.js @@ -0,0 +1,25 @@ +module.exports = function (RED) { + class CombineInvertNode { + constructor(config) { + RED.nodes.createNode(this, config); + this.on('input', msg => { + if ((msg.payload === true) || + (msg.payload === 'true') || + (msg.payload === 1) || + (msg.payload === '1')) { + msg.payload = false; + this.status({fill: 'grey', shape: 'ring', text: 'false'}); + this.send(msg); + } else if ((msg.payload === false) || + (msg.payload === 'false') || + (msg.payload === 0) || + (msg.payload === '0')) { + msg.payload = true; + this.status({fill: 'blue', shape: 'dot', text: 'true'}); + this.send(msg); + } + }); + } + } + RED.nodes.registerType('combine-invert', CombineInvertNode); +}; diff --git a/nodes/icons/invert.png b/nodes/icons/invert.png new file mode 100644 index 0000000000000000000000000000000000000000..c0d93f895ccf6c931f8282ebc0d597324c6d60e8 GIT binary patch literal 1227 zcmV;+1T_1JP)2Q3!MmD$$}6!RDa z(n@wK7!3NYR_m`OGHIl1k^q^32Rnh>3}l~KP~p-&i-F7rvS(&yrb$th0W+Y&r3;YY z_B`1>APe!}Js_PxJ~D}IPI>|VVWNC3-3w#^`TgnX>4uDqj3-F}IQIe`!*iPi4#*|k z_fAbsIWjXdpC$$1+)Ik`7ZatKv3M8AGCbH92n6g|Sy|7M0#Jkpdx6{mWEYT2@nGNN zAivDb&Hcmc z^)?g~6pY4!FFw%>kk%-BJ~}$;EGjDUhSwV$9L%b&u5PD}o!dOpGbY?^ z#_)fp0;-S_P}s=N0Z><1Sa{Fvb~lxkmHFg3AggL>YVPoCnFc^nPO^Mm?(gqE;&3=- zOaoLQ&tptHd78AQw6wH)XlQ6ld3pI~Orl;{;6rbYGX5rcNOhDIksq|%#i?nBOa&BS z@C*!j60^na$i|J4BUQujgzTU%Q# zdLaQg$`X`=;qOSBu`w&<kLnR-0Wv(2S zp|m&S^DQ7RH7{gL_9Sg>ZSTykg#_FM>0Z^S1h*?>AqV_0?cJxnU|3xz<*U@Xr?|eU z0n}qS7GdO!Oc3?DcX)WXy`rMxTgrYS)zj^LKwadS2bc!ZN$Dc)pSxVH&f3~qO?z<# zWQ-ld;As_ZU}R*ZxvHwlPgw_0tKksPdL~MJ#7B+sd^>P~$Ts5!2;6>Iq*a5D#>U1r z&M(q3X?V7q#d-k1)923*wk4FNK4w#clLEh%s=zu&(u zo}rfLqXB_ylu%2yqQJdi96941tcpGgkiou|G1M(vP~hDBhC0m%_eHt^fvW@VH{fi* zz2d&ntPPV$@5#bS;I7D)6F4v3yur*Zr${g0G07!t0&b((n|6`zE(XpoF z7N&t~Pt?JYC}u$5f~0Q}dGscV2~Z+p(%wWLUW#G`+`kydzoMvsYH+zI2k4?t|8I!J pRtnIlQh-L40yL@=piw0~;A>+*?tP};!zKU#002ovPDHLkV1k1%K4ky^ literal 0 HcmV?d00001 diff --git a/package.json b/package.json index cb93cfb..1278087 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "max", "average", "median", - "deviation" + "deviation", + "invert" ], "main": "none", "engines": { @@ -48,7 +49,8 @@ "logic": "nodes/combine-logic.js", "statistic": "nodes/combine-statistic.js", "list": "nodes/combine-list.js", - "defer": "nodes/combine-defer.js" + "defer": "nodes/combine-defer.js", + "invert": "nodes/combine-invert.js" } }, "devDependencies": { diff --git a/test/invert_spec.js b/test/invert_spec.js new file mode 100644 index 0000000..713d736 --- /dev/null +++ b/test/invert_spec.js @@ -0,0 +1,122 @@ +/* eslint-disable unicorn/filename-case, no-unused-vars */ +/* globals describe, before, after, it, afterEach */ + +const should = require('should'); +const sinon = require('sinon'); +const helper = require('node-red-node-test-helper'); +const ifNode = require('../nodes/combine-invert'); + +helper.init(require.resolve('node-red')); + +describe('combine invert', () => { + const flow1 = [ + { + id: 'n-i', + type: 'combine-invert', + name: 'invert', + topic: 'cond', + wires: [ + [ + 'nh' + ] + ] + }, + { + id: 'nh', + type: 'helper' + } + ]; + + let nh; + let ni; + + before(done => { + helper.startServer(() => { + helper.load(ifNode, flow1, () => { + ni = helper.getNode('n-i'); + nh = helper.getNode('nh'); + done(); + }); + }); + }); + + after(done => { + helper.unload(); + helper.stopServer(done); + }); + + afterEach(() => { + sinon.restore(); + }); + /* test input 'true', expect output 'false' */ + it('should send "false" payload on input "true" (boolean)', () => { + const callback = sinon.spy(); + nh.once('input', callback); + ni.receive({topic: 'a', payload: true}); + callback.calledOnce.should.be.true(); + callback.firstCall.args[0].should.have.properties({topic: 'a', payload: false}); + }); + it('should send "false" payload on input "true" (string)', () => { + const callback = sinon.spy(); + nh.once('input', callback); + ni.receive({topic: 'a', payload: 'true'}); + callback.calledOnce.should.be.true(); + callback.firstCall.args[0].should.have.properties({topic: 'a', payload: false}); + }); + it('should send "false" payload on input "1" (string)', () => { + const callback = sinon.spy(); + nh.once('input', callback); + ni.receive({topic: 'a', payload: '1'}); + callback.calledOnce.should.be.true(); + callback.firstCall.args[0].should.have.properties({topic: 'a', payload: false}); + }); + it('should send "false" payload on input "1" (number)', () => { + const callback = sinon.spy(); + nh.once('input', callback); + ni.receive({topic: 'a', payload: 1}); + callback.calledOnce.should.be.true(); + callback.firstCall.args[0].should.have.properties({topic: 'a', payload: false}); + }); + /* test input 'false', expect output 'true' */ + it('should send "true" payload on input "false" (boolean)', () => { + const callback = sinon.spy(); + nh.once('input', callback); + ni.receive({topic: 'a', payload: false}); + callback.calledOnce.should.be.true(); + callback.firstCall.args[0].should.have.properties({topic: 'a', payload: true}); + }); + it('should send "true" payload on input "false" (string)', () => { + const callback = sinon.spy(); + nh.once('input', callback); + ni.receive({topic: 'a', payload: 'false'}); + callback.calledOnce.should.be.true(); + callback.firstCall.args[0].should.have.properties({topic: 'a', payload: true}); + }); + it('should send "true" payload on input "0" (string)', () => { + const callback = sinon.spy(); + nh.once('input', callback); + ni.receive({topic: 'a', payload: '0'}); + callback.calledOnce.should.be.true(); + callback.firstCall.args[0].should.have.properties({topic: 'a', payload: true}); + }); + it('should send "true" payload on input "0" (number)', () => { + const callback = sinon.spy(); + nh.once('input', callback); + ni.receive({topic: 'a', payload: 0}); + callback.calledOnce.should.be.true(); + callback.firstCall.args[0].should.have.properties({topic: 'a', payload: true}); + }); + /* test other input, expect no output */ + it('should not send a message on input "a"', () => { + const callback = sinon.spy(); + nh.once('input', callback); + ni.receive({topic: 'a', payload: 'a'}); + callback.notCalled.should.be.true(); + }); + it('should not send a message on input NULL', () => { + const callback = sinon.spy(); + nh.once('input', callback); + ni.receive({topic: 'a', payload: null}); + callback.notCalled.should.be.true(); + }); +});