Let CSV node only send headers once

(and then reset that on msg.reset)
and also accept msg.columns csv string to set column headers if not specified in node.
And Add tests
This commit is contained in:
Dave Conway-Jones 2020-04-03 15:54:19 +01:00
parent fbfc74e5ca
commit e969a1c97c
No known key found for this signature in database
GPG Key ID: 302A6725C594817F
5 changed files with 106 additions and 36 deletions

View File

@ -28,7 +28,7 @@
</div> </div>
<div class="form-row" style="padding-left:20px;"> <div class="form-row" style="padding-left:20px;">
<label><i class="fa fa-sign-in"></i> <span data-i18n="csv.label.input"></span></label> <label><i class="fa fa-sign-in"></i> <span data-i18n="csv.label.input"></span></label>
<span data-i18n="csv.label.skip-s"></span>&nbsp;<input type="text" id="node-input-skip" style="width:30px; height:25px;"/>&nbsp;<span data-i18n="csv.label.skip-e"></span><br/> <span data-i18n="csv.label.skip-s"></span>&nbsp;<input type="text" id="node-input-skip" style="width:40px; height:25px;"/>&nbsp;<span data-i18n="csv.label.skip-e"></span><br/>
<label>&nbsp;</label> <label>&nbsp;</label>
<input style="width:20px; vertical-align:baseline; margin-right:5px;" type="checkbox" id="node-input-hdrin"><label style="width:auto; margin-top:7px;" for="node-input-hdrin"><span data-i18n="csv.label.firstrow"></span></label><br/> <input style="width:20px; vertical-align:baseline; margin-right:5px;" type="checkbox" id="node-input-hdrin"><label style="width:auto; margin-top:7px;" for="node-input-hdrin"><span data-i18n="csv.label.firstrow"></span></label><br/>
<label>&nbsp;</label> <label>&nbsp;</label>
@ -49,8 +49,13 @@
<label style="width:100%;"><span data-i18n="csv.label.o2c"></span></label> <label style="width:100%;"><span data-i18n="csv.label.o2c"></span></label>
</div> </div>
<div class="form-row" style="padding-left:20px;"> <div class="form-row" style="padding-left:20px;">
<label><i class="fa fa-sign-in"></i> <span data-i18n="csv.label.output"></span></label> <label><i class="fa fa-sign-out"></i> <span data-i18n="csv.label.output"></span></label>
<input style="width:20px; vertical-align:top; margin-right:5px;" type="checkbox" id="node-input-hdrout"><label style="width:auto;" for="node-input-hdrout"><span data-i18n="csv.label.includerow"></span></span> <!-- <input style="width:20px; vertical-align:top; margin-right:5px;" type="checkbox" id="node-input-hdrout"><label style="width:auto;" for="node-input-hdrout"><span data-i18n="csv.label.includerow"></span></span> -->
<select style="width:60%" id="node-input-hdrout">
<option value="none" data-i18n="csv.hdrout.none"></option>
<option value="all" data-i18n="csv.hdrout.all"></option>
<option value="once" data-i18n="csv.hdrout.once"></option>
</select>
</div> </div>
<div class="form-row" style="padding-left:20px;"> <div class="form-row" style="padding-left:20px;">
<label></label> <label></label>
@ -73,7 +78,7 @@
sep: {value:',',required:true,validate:RED.validators.regex(/^.{1,2}$/)}, sep: {value:',',required:true,validate:RED.validators.regex(/^.{1,2}$/)},
//quo: {value:'"',required:true}, //quo: {value:'"',required:true},
hdrin: {value:""}, hdrin: {value:""},
hdrout: {value:""}, hdrout: {value:"none"},
multi: {value:"one",required:true}, multi: {value:"one",required:true},
ret: {value:'\\n'}, ret: {value:'\\n'},
temp: {value:""}, temp: {value:""},
@ -92,6 +97,8 @@
return this.name?"node_label_italic":""; return this.name?"node_label_italic":"";
}, },
oneditprepare: function() { oneditprepare: function() {
if (this.hdrout === false) { this.hdrout = "none"; $("#node-input-hdrout").val("none"); }
if (this.hdrout === true) { this.hdrout = "all"; $("#node-input-hdrout").val("all");}
if (this.strings === undefined) { this.strings = true; $("#node-input-strings").prop('checked', true); } if (this.strings === undefined) { this.strings = true; $("#node-input-strings").prop('checked', true); }
if (this.skip === undefined) { this.skip = 0; $("#node-input-skip").val("0");} if (this.skip === undefined) { this.skip = 0; $("#node-input-skip").val("0");}
$("#node-input-skip").spinner({ min:0 }); $("#node-input-skip").spinner({ min:0 });

View File

@ -26,7 +26,7 @@ module.exports = function(RED) {
this.lineend = "\n"; this.lineend = "\n";
this.multi = n.multi || "one"; this.multi = n.multi || "one";
this.hdrin = n.hdrin || false; this.hdrin = n.hdrin || false;
this.hdrout = n.hdrout || false; this.hdrout = n.hdrout || "none";
this.goodtmpl = true; this.goodtmpl = true;
this.skip = parseInt(n.skip || 0); this.skip = parseInt(n.skip || 0);
this.store = []; this.store = [];
@ -34,6 +34,8 @@ module.exports = function(RED) {
this.include_empty_strings = n.include_empty_strings || false; this.include_empty_strings = n.include_empty_strings || false;
this.include_null_values = n.include_null_values || false; this.include_null_values = n.include_null_values || false;
if (this.parsestrings === undefined) { this.parsestrings = true; } if (this.parsestrings === undefined) { this.parsestrings = true; }
if (this.hdrout === false) { this.hdrout = "none"; }
if (this.hdrout === true) { this.hdrout = "all"; }
var tmpwarn = true; var tmpwarn = true;
var node = this; var node = this;
@ -51,14 +53,22 @@ module.exports = function(RED) {
return col; return col;
} }
node.template = clean(node.template); node.template = clean(node.template);
node.hdrSent = false;
this.on("input", function(msg) { this.on("input", function(msg) {
if (msg.hasOwnProperty("reset")) {
node.hdrSent = false;
}
if (msg.hasOwnProperty("payload")) { if (msg.hasOwnProperty("payload")) {
if (typeof msg.payload == "object") { // convert object to CSV string if (typeof msg.payload == "object") { // convert object to CSV string
try { try {
var ou = ""; var ou = "";
if (node.hdrout) { if (node.hdrout !== "none" && node.hdrSent === false) {
if ((node.template.length === 1) && (node.template[0] === '') && (msg.hasOwnProperty("columns"))) {
node.template = clean((msg.columns || "").split(","));
}
ou += node.template.join(node.sep) + node.ret; ou += node.template.join(node.sep) + node.ret;
if (node.hdrout === "once") { node.hdrSent = true; }
} }
if (!Array.isArray(msg.payload)) { msg.payload = [ msg.payload ]; } if (!Array.isArray(msg.payload)) { msg.payload = [ msg.payload ]; }
for (var s = 0; s < msg.payload.length; s++) { for (var s = 0; s < msg.payload.length; s++) {
@ -77,13 +87,15 @@ module.exports = function(RED) {
ou += msg.payload[s].join(node.sep) + node.ret; ou += msg.payload[s].join(node.sep) + node.ret;
} }
else { else {
if ((node.template.length === 1) && (node.template[0] === '') && (msg.hasOwnProperty("columns"))) {
node.template = clean((msg.columns || "").split(","));
}
if ((node.template.length === 1) && (node.template[0] === '')) { if ((node.template.length === 1) && (node.template[0] === '')) {
/* istanbul ignore else */ /* istanbul ignore else */
if (tmpwarn === true) { // just warn about missing template once if (tmpwarn === true) { // just warn about missing template once
node.warn(RED._("csv.errors.obj_csv")); node.warn(RED._("csv.errors.obj_csv"));
tmpwarn = false; tmpwarn = false;
} }
ou = "";
for (var p in msg.payload[0]) { for (var p in msg.payload[0]) {
/* istanbul ignore else */ /* istanbul ignore else */
if (msg.payload[0].hasOwnProperty(p)) { if (msg.payload[0].hasOwnProperty(p)) {
@ -127,6 +139,7 @@ module.exports = function(RED) {
} }
} }
msg.payload = ou; msg.payload = ou;
msg.columns = node.template.join(',');
if (msg.payload !== '') { node.send(msg); } if (msg.payload !== '') { node.send(msg); }
} }
catch(e) { node.error(e,msg); } catch(e) { node.error(e,msg); }
@ -187,7 +200,7 @@ module.exports = function(RED) {
// if separator is last char in processing string line (without end of line), add null value at the end - example: '1,2,3\n3,"3",' // if separator is last char in processing string line (without end of line), add null value at the end - example: '1,2,3\n3,"3",'
k[j] = line.length - 1 === i ? null : ""; k[j] = line.length - 1 === i ? null : "";
} }
else if ((line[i] === "\n") || (line[i] === "\r")) { // handle multiple lines else if (((line[i] === "\n") || (line[i] === "\r")) && f) { // handle multiple lines
//console.log(j,k,o,k[j]); //console.log(j,k,o,k[j]);
if (!node.goodtmpl) { node.template[j] = "col"+(j+1); } if (!node.goodtmpl) { node.template[j] = "col"+(j+1); }
if ( node.template[j] && (node.template[j] !== "") ) { if ( node.template[j] && (node.template[j] !== "") ) {
@ -227,6 +240,7 @@ module.exports = function(RED) {
a.push(o); // add to the array a.push(o); // add to the array
} }
var has_parts = msg.hasOwnProperty("parts"); var has_parts = msg.hasOwnProperty("parts");
if (node.multi !== "one") { if (node.multi !== "one") {
msg.payload = a; msg.payload = a;
if (has_parts) { if (has_parts) {
@ -235,12 +249,15 @@ module.exports = function(RED) {
} }
if (msg.parts.index + 1 === msg.parts.count) { if (msg.parts.index + 1 === msg.parts.count) {
msg.payload = node.store; msg.payload = node.store;
console.log(node.template)
msg.columns = node.template.filter(val => val).join(',');
delete msg.parts; delete msg.parts;
node.send(msg); node.send(msg);
node.store = []; node.store = [];
} }
} }
else { else {
msg.columns = node.template.filter(val => val).join(',');
node.send(msg); // finally send the array node.send(msg); // finally send the array
} }
} }
@ -248,6 +265,7 @@ module.exports = function(RED) {
var len = a.length; var len = a.length;
for (var i = 0; i < len; i++) { for (var i = 0; i < len; i++) {
var newMessage = RED.util.cloneMessage(msg); var newMessage = RED.util.cloneMessage(msg);
newMessage.columns = node.template.filter(val => val).join(',');
newMessage.payload = a[i]; newMessage.payload = a[i];
if (!has_parts) { if (!has_parts) {
newMessage.parts = { newMessage.parts = {
@ -273,7 +291,11 @@ module.exports = function(RED) {
} }
else { node.warn(RED._("csv.errors.csv_js")); } else { node.warn(RED._("csv.errors.csv_js")); }
} }
else { node.send(msg); } // If no payload - just pass it on. else {
if (!msg.hasOwnProperty("reset")) {
node.send(msg); // If no payload and not reset - just pass it on.
}
}
}); });
} }
RED.nodes.registerType("csv",CSVNode); RED.nodes.registerType("csv",CSVNode);

View File

@ -723,6 +723,11 @@
"mac": "Mac (\\r)", "mac": "Mac (\\r)",
"windows": "Windows (\\r\\n)" "windows": "Windows (\\r\\n)"
}, },
"hdrout": {
"none": "never send column headers",
"all": "always send column headers",
"once": "send headers once, until msg.reset"
},
"errors": { "errors": {
"csv_js": "This node only handles CSV strings or js objects.", "csv_js": "This node only handles CSV strings or js objects.",
"obj_csv": "No columns template specified for object -> CSV." "obj_csv": "No columns template specified for object -> CSV."

View File

@ -38,11 +38,13 @@
<p>The column template can contain an ordered list of column names. When converting CSV to an object, the column names <p>The column template can contain an ordered list of column names. When converting CSV to an object, the column names
will be used as the property names. Alternatively, the column names can be taken from the first row of the CSV.</p> will be used as the property names. Alternatively, the column names can be taken from the first row of the CSV.</p>
<p>When converting to CSV, the column template is used to identify which properties to extract from the object and in what order.</p> <p>When converting to CSV, the column template is used to identify which properties to extract from the object and in what order.</p>
<p>If the template is blank then the node can use a simple comma separated list of properties supplied in <code>msg.columns</code> to
determine what to extract. If that is not present then all the object properties are ouput in the order in which they are found.</p>
<p>If the input is an array then the columns template is only used to optionally generate a row of column titles.</p> <p>If the input is an array then the columns template is only used to optionally generate a row of column titles.</p>
<p>If 'parse numerical values' option is checked, string numerical values will be returned as numbers, ie. middle value '1,"1.5",2'.</p> <p>If 'parse numerical values' option is checked, string numerical values will be returned as numbers, ie. middle value '1,"1.5",2'.</p>
<p>If 'include empty strings' option is checked, empty strings will be returned in result, ie. middle value '"1","",3'.</p> <p>If 'include empty strings' option is checked, empty strings will be returned in result, ie. middle value '"1","",3'.</p>
<p>If 'include null values' option is checked, null values will be returned in result, ie. middle value '"1",,3'.</p> <p>If 'include null values' option is checked, null values will be returned in result, ie. middle value '"1",,3'.</p>
<p>The node can accept a multi-part input as long as the <code>parts</code> property is set correctly.</p> <p>The node can accept a multi-part input as long as the <code>parts</code> property is set correctly, for example from a file-in node or split node.</p>
<p>If outputting multiple messages they will have their <code>parts</code> property set and form a complete message sequence.</p> <p>If outputting multiple messages they will have their <code>parts</code> property set and form a complete message sequence.</p>
<p><b>Note:</b> the column template must be comma separated - even if a different separator is chosen for the data.</p> <p><b>Note:</b> the column template must be comma separated - even if a different separator is chosen for the data.</p>
</script> </script>

View File

@ -1,3 +1,4 @@
/* eslint-disable no-undef */
/** /**
* Copyright JS Foundation and other contributors, http://js.foundation * Copyright JS Foundation and other contributors, http://js.foundation
* *
@ -70,12 +71,13 @@ describe('CSV node', function() {
it('should convert a simple csv string to a javascript object', function(done) { it('should convert a simple csv string to a javascript object', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
msg.should.have.property('payload', { a: 1, b: 2, c: 3, d: 4 }); msg.should.have.property('payload', { a: 1, b: 2, c: 3, d: 4 });
msg.should.have.property('columns', "a,b,c,d");
check_parts(msg, 0, 1); check_parts(msg, 0, 1);
done(); done();
}); });
@ -86,7 +88,7 @@ describe('CSV node', function() {
it('should remove quotes and whitespace from template', function(done) { it('should remove quotes and whitespace from template', function(done) {
var flow = [ { id:"n1", type:"csv", temp:'"a", "b" , " c "," d " ', wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:'"a", "b" , " c "," d " ', wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
@ -102,12 +104,13 @@ describe('CSV node', function() {
it('should create column names if no template provided', function(done) { it('should create column names if no template provided', function(done) {
var flow = [ { id:"n1", type:"csv", temp:'', wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:'', wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
msg.should.have.property('payload', { col1: 1, col2: 2, col3: 3, col4: 4 }); msg.should.have.property('payload', { col1: 1, col2: 2, col3: 3, col4: 4 });
msg.should.have.property('columns', "col1,col2,col3,col4");
check_parts(msg, 0, 1); check_parts(msg, 0, 1);
done(); done();
}); });
@ -118,12 +121,13 @@ describe('CSV node', function() {
it('should allow dropping of fields from the template', function(done) { it('should allow dropping of fields from the template', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,,,d", wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,,,d", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
msg.should.have.property('payload', { a: 1, d: 4 }); msg.should.have.property('payload', { a: 1, d: 4 });
msg.should.have.property('columns', 'a,d');
check_parts(msg, 0, 1); check_parts(msg, 0, 1);
done(); done();
}); });
@ -134,7 +138,7 @@ describe('CSV node', function() {
it('should leave numbers starting with 0, e and + as strings (except 0.)', function(done) { it('should leave numbers starting with 0, e and + as strings (except 0.)', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d,e,f,g", wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d,e,f,g", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
@ -150,7 +154,7 @@ describe('CSV node', function() {
it('should not parse numbers when told not to do so', function(done) { it('should not parse numbers when told not to do so', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d,e,f,g", strings:false, wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d,e,f,g", strings:false, wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
@ -166,7 +170,7 @@ describe('CSV node', function() {
it('should leave handle strings with scientific notation as numbers', function(done) { it('should leave handle strings with scientific notation as numbers', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d,e,f,g", wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d,e,f,g", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
@ -183,7 +187,7 @@ describe('CSV node', function() {
it('should allow quotes in the input (but drop blank strings)', function(done) { it('should allow quotes in the input (but drop blank strings)', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d,e,f,g,h", wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d,e,f,g,h", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
@ -234,7 +238,7 @@ describe('CSV node', function() {
it('should recover from an odd number of quotes in the input', function(done) { it('should recover from an odd number of quotes in the input', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d,e,f,g", wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d,e,f,g", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
@ -277,12 +281,13 @@ describe('CSV node', function() {
it('should be able to output multiple lines as one array', function(done) { it('should be able to output multiple lines as one array', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", multi:"yes", wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", multi:"yes", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
n2.on("input", function(msg) { n2.on("input", function(msg) {
msg.should.have.property('payload', [ { a: 1, b: 2, c: 3, d: 4 },{ a: 5, b: -6, c: '07', d: '+8' },{ a: 9, b: 0, c: 'a', d: 'b' },{ a: 'c', b: 'd', c: 'e', d: 'f' } ]); msg.should.have.property('payload', [ { a: 1, b: 2, c: 3, d: 4 },{ a: 5, b: -6, c: '07', d: '+8' },{ a: 9, b: 0, c: 'a', d: 'b' },{ a: 'c', b: 'd', c: 'e', d: 'f' } ]);
msg.should.have.property('columns','a,b,c,d');
msg.should.not.have.property('parts'); msg.should.not.have.property('parts');
done(); done();
}); });
@ -293,7 +298,7 @@ describe('CSV node', function() {
it('should handle numbers in strings but not IP addresses', function(done) { it('should handle numbers in strings but not IP addresses', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d,e", wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d,e", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
@ -309,7 +314,7 @@ describe('CSV node', function() {
it('should preserve parts property', function(done) { it('should preserve parts property', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
@ -353,7 +358,7 @@ describe('CSV node', function() {
it('should skip several lines from start if requested', function(done) { it('should skip several lines from start if requested', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", skip: 2, wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", skip: 2, wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
@ -367,9 +372,9 @@ describe('CSV node', function() {
}); });
}); });
it('should skip several lines from start then use next line as a tempate', function(done) { it('should skip several lines from start then use next line as a template', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", hdrin:true, skip: 2, wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", hdrin:true, skip: 2, wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
@ -385,7 +390,7 @@ describe('CSV node', function() {
it('should skip several lines from start and correct parts', function(done) { it('should skip several lines from start and correct parts', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", skip: 2, wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", skip: 2, wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
@ -417,11 +422,13 @@ describe('CSV node', function() {
n2.on("input", function(msg) { n2.on("input", function(msg) {
if (c === 0) { if (c === 0) {
msg.should.have.property('payload', { w: 1, x: 2, y: 3, z: 4 }); msg.should.have.property('payload', { w: 1, x: 2, y: 3, z: 4 });
msg.should.have.property('columns', 'w,x,y,z');
check_parts(msg, 0, 2); check_parts(msg, 0, 2);
c += 1; c += 1;
} }
else { else {
msg.should.have.property('payload', { w: 5, x: 6, y: 7, z: 8 }); msg.should.have.property('payload', { w: 5, x: 6, y: 7, z: 8 });
msg.should.have.property('columns', 'w,x,y,z');
check_parts(msg, 1, 2); check_parts(msg, 1, 2);
done(); done();
} }
@ -445,7 +452,7 @@ describe('CSV node', function() {
it('should convert a simple object back to a csv', function(done) { it('should convert a simple object back to a csv', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,,e", wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,,e", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
@ -463,7 +470,7 @@ describe('CSV node', function() {
it('should convert a simple object back to a csv with no template', function(done) { it('should convert a simple object back to a csv with no template', function(done) {
var flow = [ { id:"n1", type:"csv", temp:" ", wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:" ", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
@ -481,7 +488,7 @@ describe('CSV node', function() {
it('should handle a template with spaces in the property names', function(done) { it('should handle a template with spaces in the property names', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b o,c p,,e", wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b o,c p,,e", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
@ -499,7 +506,7 @@ describe('CSV node', function() {
it('should convert an array of objects to a multi-line csv', function(done) { it('should convert an array of objects to a multi-line csv', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
@ -517,7 +524,7 @@ describe('CSV node', function() {
it('should convert a simple array back to a csv', function(done) { it('should convert a simple array back to a csv', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
@ -535,7 +542,7 @@ describe('CSV node', function() {
it('should convert an array of arrays back to a multi-line csv', function(done) { it('should convert an array of arrays back to a multi-line csv', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
@ -553,7 +560,7 @@ describe('CSV node', function() {
it('should be able to include column names as first row', function(done) { it('should be able to include column names as first row', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", hdrout:true, ret:"\r\n", wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", hdrout:true, ret:"\r\n", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
@ -569,9 +576,36 @@ describe('CSV node', function() {
}); });
}); });
it('should be able to pass in column names', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"", hdrout:"once", ret:"\r\n", wires:[["n2"]] },
{id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var count = 0;
n2.on("input", function(msg) {
count += 1;
try {
if (count === 1) {
msg.should.have.property('payload', 'a,,b,a\r\n4,,3,4\r\n');
}
if (count === 3) {
msg.should.have.property('payload', '4,,3,4\r\n');
done()
}
}
catch(e) { done(e); }
});
var testJson = [{ d: 1, b: 3, c: 2, a: 4 }];
n1.emit("input", {payload:testJson, columns:"a,,b,a"});
n1.emit("input", {payload:testJson});
n1.emit("input", {payload:testJson});
});
});
it('should handle quotes and sub-properties', function(done) { it('should handle quotes and sub-properties', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
@ -591,7 +625,7 @@ describe('CSV node', function() {
it('should just pass through if no payload provided', function(done) { it('should just pass through if no payload provided', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
@ -611,7 +645,7 @@ describe('CSV node', function() {
it('should warn if provided a number or boolean', function(done) { it('should warn if provided a number or boolean', function(done) {
var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] }, var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d", wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(csvNode, flow, function() { helper.load(csvNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");