-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
189 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<script type="text/javascript"> | ||
RED.nodes.registerType('combine-invert', { | ||
category: 'combine', | ||
defaults: { | ||
name: {value: ''} | ||
}, | ||
inputs: 1, | ||
outputs: 1, | ||
icon: 'invert.png', | ||
align: 'left', | ||
color: '#D8BFD8', | ||
paletteLabel: 'invert', | ||
label() { | ||
return this.name || 'invert'; | ||
} | ||
}); | ||
</script> | ||
|
||
<script type="text/x-red" data-template-name="combine-invert"> | ||
<div class="form-row"> | ||
<label for="node-input-name"><i class="icon-bookmark"></i> Name</label> | ||
<input type="text" id="node-input-name"> | ||
</div> | ||
</script> | ||
|
||
<script type="text/x-red" data-help-name="combine-invert"> | ||
<p> | ||
Invert: Receives a boolean input value and sends the inverted representation <br/> | ||
All incoming msg.payloads are converted into a boolean value according to the following rules: | ||
<ol> | ||
<li>True Boolean values are taken as-is.</li> | ||
<li><code>"true"</code> or <code>"false"</code> as strings are converted to their boolean pendants</li> | ||
<li>Numeric or string <code>1</code> are interpreted as boolean <code>true</code>.</li> | ||
<li>Numeric or string <code>0</code> are interpreted as boolean <code>false</code>.</li> | ||
<li>Any other values are ignored. There is no implicit type conversion</li> | ||
</ol> | ||
</p> | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); |