Skip to content

Commit

Permalink
Merge 53c9620 into 88e0644
Browse files Browse the repository at this point in the history
  • Loading branch information
Sineos committed Jul 11, 2019
2 parents 88e0644 + 53c9620 commit c35323d
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 2 deletions.
38 changes: 38 additions & 0 deletions nodes/combine-invert.html
@@ -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>
25 changes: 25 additions & 0 deletions 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);
};
Binary file added nodes/icons/invert.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -17,7 +17,8 @@
"max",
"average",
"median",
"deviation"
"deviation",
"invert"
],
"main": "none",
"engines": {
Expand Down Expand Up @@ -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": {
Expand Down
122 changes: 122 additions & 0 deletions 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();
});
});

0 comments on commit c35323d

Please sign in to comment.