mirror of
https://github.com/node-red/node-red.git
synced 2023-10-10 13:36:53 +02:00
redo CSV fix for commas in header template
This commit is contained in:
parent
2de43b719e
commit
16b9abbe92
@ -48,7 +48,8 @@ module.exports = function(RED) {
|
|||||||
else { node.goodtmpl = true; }
|
else { node.goodtmpl = true; }
|
||||||
return col;
|
return col;
|
||||||
}
|
}
|
||||||
node.template = clean(node.template);
|
var template = clean(node.template);
|
||||||
|
var notemplate = template.length === 1 && template[0] === '';
|
||||||
node.hdrSent = false;
|
node.hdrSent = false;
|
||||||
|
|
||||||
this.on("input", function(msg, send, done) {
|
this.on("input", function(msg, send, done) {
|
||||||
@ -58,19 +59,21 @@ module.exports = function(RED) {
|
|||||||
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 {
|
||||||
|
if (!(notemplate && (msg.hasOwnProperty("parts") && msg.parts.hasOwnProperty("index") && msg.parts.index > 0))) {
|
||||||
|
template = clean(node.template);
|
||||||
|
}
|
||||||
var ou = "";
|
var ou = "";
|
||||||
if (!Array.isArray(msg.payload)) { msg.payload = [ msg.payload ]; }
|
if (!Array.isArray(msg.payload)) { msg.payload = [ msg.payload ]; }
|
||||||
if (node.hdrout !== "none" && node.hdrSent === false) {
|
if (node.hdrout !== "none" && node.hdrSent === false) {
|
||||||
if ((node.template.length === 1) && (node.template[0] === '')) {
|
if ((template.length === 1) && (template[0] === '')) {
|
||||||
if (msg.hasOwnProperty("columns")) {
|
if (msg.hasOwnProperty("columns")) {
|
||||||
node.template = clean(msg.columns || "");
|
template = clean(msg.columns || "");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
node.template = Object.keys(msg.payload[0]);
|
template = Object.keys(msg.payload[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ou += node.template.join(node.sep) + node.ret;
|
ou += template.map(v => v.indexOf(node.sep)!==-1 ? '"'+v+'"' : v).join(node.sep) + node.ret;
|
||||||
ou += node.template.map(v => v.indexOf(node.sep)!==-1 ? '"'+v+'"' : v).join(node.sep) + node.ret;
|
|
||||||
if (node.hdrout === "once") { node.hdrSent = true; }
|
if (node.hdrout === "once") { node.hdrSent = true; }
|
||||||
}
|
}
|
||||||
for (var s = 0; s < msg.payload.length; s++) {
|
for (var s = 0; s < msg.payload.length; s++) {
|
||||||
@ -89,10 +92,10 @@ 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"))) {
|
if ((template.length === 1) && (template[0] === '') && (msg.hasOwnProperty("columns"))) {
|
||||||
node.template = clean(msg.columns || "")//.split(","));
|
template = clean(msg.columns || "");
|
||||||
}
|
}
|
||||||
if ((node.template.length === 1) && (node.template[0] === '')) {
|
if ((template.length === 1) && (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"));
|
||||||
@ -118,12 +121,12 @@ module.exports = function(RED) {
|
|||||||
ou = ou.slice(0,-1) + node.ret;
|
ou = ou.slice(0,-1) + node.ret;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
for (var t=0; t < node.template.length; t++) {
|
for (var t=0; t < template.length; t++) {
|
||||||
if (node.template[t] === '') {
|
if (template[t] === '') {
|
||||||
ou += node.sep;
|
ou += node.sep;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var p = RED.util.ensureString(RED.util.getMessageProperty(msg,"payload["+s+"]['"+node.template[t]+"']"));
|
var p = RED.util.ensureString(RED.util.getMessageProperty(msg,"payload["+s+"]['"+template[t]+"']"));
|
||||||
/* istanbul ignore else */
|
/* istanbul ignore else */
|
||||||
if (p === "undefined") { p = ""; }
|
if (p === "undefined") { p = ""; }
|
||||||
if (p.indexOf(node.quo) !== -1) { // add double quotes if any quotes
|
if (p.indexOf(node.quo) !== -1) { // add double quotes if any quotes
|
||||||
@ -141,7 +144,7 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
msg.payload = ou;
|
msg.payload = ou;
|
||||||
msg.columns = node.template.map(v => v.indexOf(',')!==-1 ? '"'+v+'"' : v).join(',');
|
msg.columns = template.map(v => v.indexOf(',')!==-1 ? '"'+v+'"' : v).join(',');
|
||||||
if (msg.payload !== '') { send(msg); }
|
if (msg.payload !== '') { send(msg); }
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
@ -176,7 +179,7 @@ module.exports = function(RED) {
|
|||||||
if ((node.hdrin === true) && first) { // if the template is in the first line
|
if ((node.hdrin === true) && first) { // if the template is in the first line
|
||||||
if ((line[i] === "\n")||(line[i] === "\r")||(line.length - i === 1)) { // look for first line break
|
if ((line[i] === "\n")||(line[i] === "\r")||(line.length - i === 1)) { // look for first line break
|
||||||
if (line.length - i === 1) { tmp += line[i]; }
|
if (line.length - i === 1) { tmp += line[i]; }
|
||||||
node.template = clean(tmp);
|
template = clean(tmp);
|
||||||
first = false;
|
first = false;
|
||||||
}
|
}
|
||||||
else { tmp += line[i]; }
|
else { tmp += line[i]; }
|
||||||
@ -190,14 +193,14 @@ module.exports = function(RED) {
|
|||||||
//if ((line[i-1] !== node.sep) && (line[i+1] !== node.sep)) { k[j] += line[i]; }
|
//if ((line[i-1] !== node.sep) && (line[i+1] !== node.sep)) { k[j] += line[i]; }
|
||||||
}
|
}
|
||||||
else if ((line[i] === node.sep) && f) { // if it is the end of the line then finish
|
else if ((line[i] === node.sep) && f) { // if it is the end of the line then finish
|
||||||
if (!node.goodtmpl) { node.template[j] = "col"+(j+1); }
|
if (!node.goodtmpl) { template[j] = "col"+(j+1); }
|
||||||
if ( node.template[j] && (node.template[j] !== "") ) {
|
if ( template[j] && (template[j] !== "") ) {
|
||||||
// if no value between separators ('1,,"3"...') or if the line beings with separator (',1,"2"...') treat value as null
|
// if no value between separators ('1,,"3"...') or if the line beings with separator (',1,"2"...') treat value as null
|
||||||
if (line[i-1] === node.sep || line[i-1].includes('\n','\r')) k[j] = null;
|
if (line[i-1] === node.sep || line[i-1].includes('\n','\r')) k[j] = null;
|
||||||
if ( (k[j] !== null && node.parsestrings === true) && reg.test(k[j]) ) { k[j] = parseFloat(k[j]); }
|
if ( (k[j] !== null && node.parsestrings === true) && reg.test(k[j]) ) { k[j] = parseFloat(k[j]); }
|
||||||
if (node.include_null_values && k[j] === null) o[node.template[j]] = k[j];
|
if (node.include_null_values && k[j] === null) o[template[j]] = k[j];
|
||||||
if (node.include_empty_strings && k[j] === "") o[node.template[j]] = k[j];
|
if (node.include_empty_strings && k[j] === "") o[template[j]] = k[j];
|
||||||
if (k[j] !== null && k[j] !== "") o[node.template[j]] = k[j];
|
if (k[j] !== null && k[j] !== "") o[template[j]] = k[j];
|
||||||
}
|
}
|
||||||
j += 1;
|
j += 1;
|
||||||
// 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",'
|
||||||
@ -205,15 +208,15 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
else if (((line[i] === "\n") || (line[i] === "\r")) && f) { // 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) { template[j] = "col"+(j+1); }
|
||||||
if ( node.template[j] && (node.template[j] !== "") ) {
|
if ( template[j] && (template[j] !== "") ) {
|
||||||
// if separator before end of line, set null value ie. '1,2,"3"\n1,2,\n1,2,3'
|
// if separator before end of line, set null value ie. '1,2,"3"\n1,2,\n1,2,3'
|
||||||
if (line[i-1] === node.sep) k[j] = null;
|
if (line[i-1] === node.sep) k[j] = null;
|
||||||
if ( (k[j] !== null && node.parsestrings === true) && reg.test(k[j]) ) { k[j] = parseFloat(k[j]); }
|
if ( (k[j] !== null && node.parsestrings === true) && reg.test(k[j]) ) { k[j] = parseFloat(k[j]); }
|
||||||
else { if (k[j] !== null) k[j].replace(/\r$/,''); }
|
else { if (k[j] !== null) k[j].replace(/\r$/,''); }
|
||||||
if (node.include_null_values && k[j] === null) o[node.template[j]] = k[j];
|
if (node.include_null_values && k[j] === null) o[template[j]] = k[j];
|
||||||
if (node.include_empty_strings && k[j] === "") o[node.template[j]] = k[j];
|
if (node.include_empty_strings && k[j] === "") o[template[j]] = k[j];
|
||||||
if (k[j] !== null && k[j] !== "") o[node.template[j]] = k[j];
|
if (k[j] !== null && k[j] !== "") o[template[j]] = k[j];
|
||||||
}
|
}
|
||||||
if (JSON.stringify(o) !== "{}") { // don't send empty objects
|
if (JSON.stringify(o) !== "{}") { // don't send empty objects
|
||||||
a.push(o); // add to the array
|
a.push(o); // add to the array
|
||||||
@ -230,14 +233,14 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
// Finished so finalize and send anything left
|
// Finished so finalize and send anything left
|
||||||
if (f === false) { node.warn(RED._("csv.errors.bad_csv")); }
|
if (f === false) { node.warn(RED._("csv.errors.bad_csv")); }
|
||||||
if (!node.goodtmpl) { node.template[j] = "col"+(j+1); }
|
if (!node.goodtmpl) { template[j] = "col"+(j+1); }
|
||||||
|
|
||||||
if ( node.template[j] && (node.template[j] !== "") ) {
|
if ( template[j] && (template[j] !== "") ) {
|
||||||
if ( (k[j] !== null && node.parsestrings === true) && reg.test(k[j]) ) { k[j] = parseFloat(k[j]); }
|
if ( (k[j] !== null && node.parsestrings === true) && reg.test(k[j]) ) { k[j] = parseFloat(k[j]); }
|
||||||
else { if (k[j] !== null) k[j].replace(/\r$/,''); }
|
else { if (k[j] !== null) k[j].replace(/\r$/,''); }
|
||||||
if (node.include_null_values && k[j] === null) o[node.template[j]] = k[j];
|
if (node.include_null_values && k[j] === null) o[template[j]] = k[j];
|
||||||
if (node.include_empty_strings && k[j] === "") o[node.template[j]] = k[j];
|
if (node.include_empty_strings && k[j] === "") o[template[j]] = k[j];
|
||||||
if (k[j] !== null && k[j] !== "") o[node.template[j]] = k[j];
|
if (k[j] !== null && k[j] !== "") o[template[j]] = k[j];
|
||||||
}
|
}
|
||||||
if (JSON.stringify(o) !== "{}") { // don't send empty objects
|
if (JSON.stringify(o) !== "{}") { // don't send empty objects
|
||||||
a.push(o); // add to the array
|
a.push(o); // add to the array
|
||||||
@ -252,14 +255,14 @@ 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;
|
||||||
msg.columns = node.template.map(v => v.indexOf(',')!==-1 ? '"'+v+'"' : v).filter(v => v).join(',');
|
msg.columns = template.map(v => v.indexOf(',')!==-1 ? '"'+v+'"' : v).filter(v => v).join(',');
|
||||||
delete msg.parts;
|
delete msg.parts;
|
||||||
send(msg);
|
send(msg);
|
||||||
node.store = [];
|
node.store = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
msg.columns = node.template.map(v => v.indexOf(',')!==-1 ? '"'+v+'"' : v).filter(v => v).join(',');
|
msg.columns = template.map(v => v.indexOf(',')!==-1 ? '"'+v+'"' : v).filter(v => v).join(',');
|
||||||
send(msg); // finally send the array
|
send(msg); // finally send the array
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -267,7 +270,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.map(v => v.indexOf(',')!==-1 ? '"'+v+'"' : v).filter(v => v).join(',');
|
newMessage.columns = template.map(v => v.indexOf(',')!==-1 ? '"'+v+'"' : v).filter(v => v).join(',');
|
||||||
newMessage.payload = a[i];
|
newMessage.payload = a[i];
|
||||||
if (!has_parts) {
|
if (!has_parts) {
|
||||||
newMessage.parts = {
|
newMessage.parts = {
|
||||||
@ -296,7 +299,7 @@ module.exports = function(RED) {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (!msg.hasOwnProperty("reset")) {
|
if (!msg.hasOwnProperty("reset")) {
|
||||||
send(msg); // If no payload and not reset - just pass it on.
|
node.send(msg); // If no payload and not reset - just pass it on.
|
||||||
}
|
}
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user