2014-04-29 18:01:30 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2014-04-29 18:01:30 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
module.exports = function(RED) {
|
2014-05-29 23:13:21 +02:00
|
|
|
"use strict";
|
2014-05-04 00:32:04 +02:00
|
|
|
function CSVNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
2015-03-24 20:02:07 +01:00
|
|
|
this.template = (n.temp || "").split(",");
|
2014-09-12 17:50:01 +02:00
|
|
|
this.sep = (n.sep || ',').replace("\\t","\t").replace("\\n","\n").replace("\\r","\r");
|
2014-05-04 00:32:04 +02:00
|
|
|
this.quo = '"';
|
2014-09-12 17:50:01 +02:00
|
|
|
this.ret = (n.ret || "\n").replace("\\n","\n").replace("\\r","\r");
|
|
|
|
this.winflag = (this.ret === "\r\n");
|
|
|
|
this.lineend = "\n";
|
|
|
|
this.multi = n.multi || "one";
|
|
|
|
this.hdrin = n.hdrin || false;
|
|
|
|
this.hdrout = n.hdrout || false;
|
|
|
|
this.goodtmpl = true;
|
2017-02-07 22:14:12 +01:00
|
|
|
var tmpwarn = true;
|
2014-05-04 00:32:04 +02:00
|
|
|
var node = this;
|
2014-08-30 14:22:22 +02:00
|
|
|
|
2014-09-12 17:50:01 +02:00
|
|
|
// pass in an array of column names to be trimed, de-quoted and retrimed
|
|
|
|
var clean = function(col) {
|
|
|
|
for (var t = 0; t < col.length; t++) {
|
|
|
|
col[t] = col[t].trim(); // remove leading and trailing whitespace
|
|
|
|
if (col[t].charAt(0) === '"' && col[t].charAt(col[t].length -1) === '"') {
|
|
|
|
// remove leading and trailing quotes (if they exist) - and remove whitepace again.
|
|
|
|
col[t] = col[t].substr(1,col[t].length -2).trim();
|
|
|
|
}
|
2014-08-30 14:22:22 +02:00
|
|
|
}
|
2014-09-12 17:50:01 +02:00
|
|
|
if ((col.length === 1) && (col[0] === "")) { node.goodtmpl = false; }
|
|
|
|
else { node.goodtmpl = true; }
|
|
|
|
return col;
|
2014-08-30 14:22:22 +02:00
|
|
|
}
|
2014-09-12 17:50:01 +02:00
|
|
|
node.template = clean(node.template);
|
2014-08-30 14:22:22 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
this.on("input", function(msg) {
|
|
|
|
if (msg.hasOwnProperty("payload")) {
|
2014-09-12 17:50:01 +02:00
|
|
|
if (typeof msg.payload == "object") { // convert object to CSV string
|
2014-05-04 00:32:04 +02:00
|
|
|
try {
|
2014-06-30 21:44:01 +02:00
|
|
|
var ou = "";
|
2014-09-12 17:50:01 +02:00
|
|
|
if (node.hdrout) {
|
|
|
|
ou += node.template.join(node.sep) + node.ret;
|
|
|
|
}
|
|
|
|
if (!Array.isArray(msg.payload)) { msg.payload = [ msg.payload ]; }
|
|
|
|
for (var s = 0; s < msg.payload.length; s++) {
|
2015-10-15 00:07:25 +02:00
|
|
|
if ((Array.isArray(msg.payload[s])) || (typeof msg.payload[s] !== "object")) {
|
|
|
|
if (typeof msg.payload[s] !== "object") { msg.payload = [ msg.payload ]; }
|
|
|
|
for (var t = 0; t < msg.payload[s].length; t++) {
|
2016-11-02 16:49:51 +01:00
|
|
|
if (!msg.payload[s][t] && (msg.payload[s][t] !== 0)) { msg.payload[s][t] = ""; }
|
2015-10-15 00:07:25 +02:00
|
|
|
if (msg.payload[s][t].toString().indexOf(node.quo) !== -1) { // add double quotes if any quotes
|
|
|
|
msg.payload[s][t] = msg.payload[s][t].toString().replace(/"/g, '""');
|
|
|
|
msg.payload[s][t] = node.quo + msg.payload[s][t].toString() + node.quo;
|
|
|
|
}
|
|
|
|
else if (msg.payload[s][t].toString().indexOf(node.sep) !== -1) { // add quotes if any "commas"
|
|
|
|
msg.payload[s][t] = node.quo + msg.payload[s][t].toString() + node.quo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ou += msg.payload[s].join(node.sep) + node.ret;
|
|
|
|
}
|
|
|
|
else {
|
2015-10-15 09:31:42 +02:00
|
|
|
if ((node.template.length === 1) && (node.template[0] === '')) {
|
2017-12-05 18:34:14 +01:00
|
|
|
/* istanbul ignore else */
|
2017-02-07 22:14:12 +01:00
|
|
|
if (tmpwarn === true) { // just warn about missing template once
|
|
|
|
node.warn(RED._("csv.errors.obj_csv"));
|
|
|
|
tmpwarn = false;
|
|
|
|
}
|
|
|
|
ou = "";
|
|
|
|
for (var p in msg.payload[0]) {
|
2017-11-02 17:50:13 +01:00
|
|
|
/* istanbul ignore else */
|
2017-02-07 22:14:12 +01:00
|
|
|
if (msg.payload[0].hasOwnProperty(p)) {
|
2017-11-02 17:50:13 +01:00
|
|
|
/* istanbul ignore else */
|
2017-02-07 22:14:12 +01:00
|
|
|
if (typeof msg.payload[0][p] !== "object") {
|
2017-11-02 17:50:13 +01:00
|
|
|
var q = "" + msg.payload[0][p];
|
2017-02-20 23:44:37 +01:00
|
|
|
if (q.indexOf(node.quo) !== -1) { // add double quotes if any quotes
|
|
|
|
q = q.replace(/"/g, '""');
|
|
|
|
ou += node.quo + q + node.quo + node.sep;
|
|
|
|
}
|
|
|
|
else if (q.indexOf(node.sep) !== -1) { // add quotes if any "commas"
|
|
|
|
ou += node.quo + q + node.quo + node.sep;
|
|
|
|
}
|
|
|
|
else { ou += q + node.sep; } // otherwise just add
|
2017-02-07 22:14:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ou = ou.slice(0,-1) + node.ret;
|
2015-10-15 09:31:42 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-10-15 00:38:29 +02:00
|
|
|
for (var t=0; t < node.template.length; t++) {
|
2015-10-15 09:31:42 +02:00
|
|
|
if (node.template[t] === '') {
|
|
|
|
ou += node.sep;
|
2015-10-15 00:38:29 +02:00
|
|
|
}
|
2015-10-15 09:31:42 +02:00
|
|
|
else {
|
2017-11-02 17:50:13 +01:00
|
|
|
var p = RED.util.ensureString(RED.util.getMessageProperty(msg,"payload["+s+"]['"+node.template[t]+"']"));
|
|
|
|
/* istanbul ignore else */
|
2015-10-15 09:31:42 +02:00
|
|
|
if (p === "undefined") { p = ""; }
|
|
|
|
if (p.indexOf(node.quo) !== -1) { // add double quotes if any quotes
|
|
|
|
p = p.replace(/"/g, '""');
|
|
|
|
ou += node.quo + p + node.quo + node.sep;
|
|
|
|
}
|
|
|
|
else if (p.indexOf(node.sep) !== -1) { // add quotes if any "commas"
|
|
|
|
ou += node.quo + p + node.quo + node.sep;
|
|
|
|
}
|
|
|
|
else { ou += p + node.sep; } // otherwise just add
|
2015-10-15 00:38:29 +02:00
|
|
|
}
|
2015-10-15 00:07:25 +02:00
|
|
|
}
|
2015-10-15 00:38:29 +02:00
|
|
|
ou = ou.slice(0,-1) + node.ret; // remove final "comma" and add "newline"
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2014-04-29 18:01:30 +02:00
|
|
|
}
|
|
|
|
}
|
2015-02-22 20:23:36 +01:00
|
|
|
msg.payload = ou;
|
2015-10-15 00:38:29 +02:00
|
|
|
if (msg.payload !== '') { node.send(msg); }
|
2014-04-29 18:01:30 +02:00
|
|
|
}
|
2015-03-16 14:58:01 +01:00
|
|
|
catch(e) { node.error(e,msg); }
|
2014-04-29 18:01:30 +02:00
|
|
|
}
|
2014-09-12 17:50:01 +02:00
|
|
|
else if (typeof msg.payload == "string") { // convert CSV string to object
|
2014-05-04 00:32:04 +02:00
|
|
|
try {
|
2014-09-12 17:50:01 +02:00
|
|
|
var f = true; // flag to indicate if inside or outside a pair of quotes true = outside.
|
|
|
|
var j = 0; // pointer into array of template items
|
|
|
|
var k = [""]; // array of data for each of the template items
|
|
|
|
var o = {}; // output object to build up
|
|
|
|
var a = []; // output array is needed for multiline option
|
|
|
|
var first = true; // is this the first line
|
2015-02-22 20:23:36 +01:00
|
|
|
var line = msg.payload;
|
2014-09-12 17:50:01 +02:00
|
|
|
var tmp = "";
|
2017-02-07 22:14:12 +01:00
|
|
|
var reg = /^[-]?[0-9]*\.?[0-9]+$/;
|
2017-12-05 18:34:14 +01:00
|
|
|
if (msg.hasOwnProperty("parts")) {
|
|
|
|
if (msg.parts.index > 0) { first = false; }
|
|
|
|
}
|
2014-09-12 17:50:01 +02:00
|
|
|
|
2014-09-16 20:05:16 +02:00
|
|
|
// For now we are just going to assume that any \r or \n means an end of line...
|
2014-09-12 17:50:01 +02:00
|
|
|
// got to be a weird csv that has singleton \r \n in it for another reason...
|
|
|
|
|
|
|
|
// Now process the whole file/line
|
2015-02-22 20:23:36 +01:00
|
|
|
for (var i = 0; i < line.length; i++) {
|
2014-09-12 17:50:01 +02:00
|
|
|
if ((node.hdrin === true) && first) { // if the template is in the first line
|
2015-02-22 20:23:36 +01:00
|
|
|
if ((line[i] === "\n")||(line[i] === "\r")) { // look for first line break
|
2014-09-12 17:50:01 +02:00
|
|
|
node.template = clean(tmp.split(node.sep));
|
|
|
|
first = false;
|
|
|
|
}
|
2015-02-22 20:23:36 +01:00
|
|
|
else { tmp += line[i]; }
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-02-22 20:23:36 +01:00
|
|
|
if (line[i] === node.quo) { // if it's a quote toggle inside or outside
|
2014-09-12 17:50:01 +02:00
|
|
|
f = !f;
|
2017-02-20 23:44:37 +01:00
|
|
|
if (line[i-1] === node.quo) {
|
|
|
|
if (f === false) { k[j] += '\"'; }
|
|
|
|
} // if it's a quotequote then it's actually a quote
|
2015-03-24 20:02:07 +01:00
|
|
|
//if ((line[i-1] !== node.sep) && (line[i+1] !== node.sep)) { k[j] += line[i]; }
|
2014-09-12 17:50:01 +02:00
|
|
|
}
|
2016-07-05 12:31:25 +02:00
|
|
|
else if ((line[i] === node.sep) && f) { // if it is the end of the line then finish
|
2014-09-12 17:50:01 +02:00
|
|
|
if (!node.goodtmpl) { node.template[j] = "col"+(j+1); }
|
|
|
|
if ( node.template[j] && (node.template[j] !== "") && (k[j] !== "" ) ) {
|
2016-06-14 23:36:44 +02:00
|
|
|
if ( reg.test(k[j]) ) { k[j] = parseFloat(k[j]); }
|
2014-09-12 17:50:01 +02:00
|
|
|
o[node.template[j]] = k[j];
|
|
|
|
}
|
|
|
|
j += 1;
|
|
|
|
k[j] = "";
|
|
|
|
}
|
2016-07-05 12:31:25 +02:00
|
|
|
else if ((line[i] === "\n") || (line[i] === "\r")) { // handle multiple lines
|
2014-09-12 17:50:01 +02:00
|
|
|
//console.log(j,k,o,k[j]);
|
2015-03-24 20:02:07 +01:00
|
|
|
if (!node.goodtmpl) { node.template[j] = "col"+(j+1); }
|
2014-09-12 17:50:01 +02:00
|
|
|
if ( node.template[j] && (node.template[j] !== "") && (k[j] !== "") ) {
|
2016-06-14 23:36:44 +02:00
|
|
|
if ( reg.test(k[j]) ) { k[j] = parseFloat(k[j]); }
|
2014-09-16 20:05:16 +02:00
|
|
|
else { k[j].replace(/\r$/,''); }
|
|
|
|
o[node.template[j]] = k[j];
|
2014-09-12 17:50:01 +02:00
|
|
|
}
|
|
|
|
if (JSON.stringify(o) !== "{}") { // don't send empty objects
|
2017-12-05 16:39:51 +01:00
|
|
|
a.push(o); // add to the array
|
2014-09-12 17:50:01 +02:00
|
|
|
}
|
|
|
|
j = 0;
|
|
|
|
k = [""];
|
|
|
|
o = {};
|
2016-07-05 12:31:25 +02:00
|
|
|
f = true; // reset in/out flag ready for next line.
|
2014-09-12 17:50:01 +02:00
|
|
|
}
|
|
|
|
else { // just add to the part of the message
|
2015-02-22 20:23:36 +01:00
|
|
|
k[j] += line[i];
|
2014-09-12 17:50:01 +02:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2014-04-29 18:01:30 +02:00
|
|
|
}
|
2014-09-12 17:50:01 +02:00
|
|
|
// Finished so finalize and send anything left
|
|
|
|
//console.log(j,k,o,k[j]);
|
|
|
|
if (!node.goodtmpl) { node.template[j] = "col"+(j+1); }
|
|
|
|
if ( node.template[j] && (node.template[j] !== "") && (k[j] !== "") ) {
|
2016-06-14 23:36:44 +02:00
|
|
|
if ( reg.test(k[j]) ) { k[j] = parseFloat(k[j]); }
|
2014-09-16 20:05:16 +02:00
|
|
|
else { k[j].replace(/\r$/,''); }
|
|
|
|
o[node.template[j]] = k[j];
|
2014-09-12 17:50:01 +02:00
|
|
|
}
|
|
|
|
if (JSON.stringify(o) !== "{}") { // don't send empty objects
|
2017-12-05 16:39:51 +01:00
|
|
|
a.push(o); // add to the aray
|
2014-09-12 17:50:01 +02:00
|
|
|
}
|
2015-02-22 20:23:36 +01:00
|
|
|
if (node.multi !== "one") {
|
|
|
|
msg.payload = a;
|
|
|
|
node.send(msg); // finally send the array
|
|
|
|
}
|
2017-12-05 18:34:14 +01:00
|
|
|
else {
|
2017-12-05 16:39:51 +01:00
|
|
|
var has_parts = msg.hasOwnProperty("parts");
|
2017-12-05 18:34:14 +01:00
|
|
|
var len = a.length;
|
|
|
|
for (var i = 0; i < len; i++) {
|
2017-12-05 16:39:51 +01:00
|
|
|
var newMessage = RED.util.cloneMessage(msg);
|
|
|
|
newMessage.payload = a[i];
|
2017-12-05 18:34:14 +01:00
|
|
|
if (!has_parts) {
|
|
|
|
newMessage.parts = {
|
|
|
|
id: msg._msgid,
|
|
|
|
index: i,
|
|
|
|
count: len
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else if (node.hdrin) { // if we removed the header line then shift the counts by 1
|
|
|
|
newMessage.parts.index -= 1;
|
|
|
|
newMessage.parts.count -= 1;
|
2017-12-05 16:39:51 +01:00
|
|
|
}
|
|
|
|
node.send(newMessage);
|
2017-12-05 18:34:14 +01:00
|
|
|
}
|
|
|
|
}
|
2014-04-29 18:01:30 +02:00
|
|
|
}
|
2015-03-16 14:58:01 +01:00
|
|
|
catch(e) { node.error(e,msg); }
|
2014-04-29 18:01:30 +02:00
|
|
|
}
|
2015-05-10 22:47:22 +02:00
|
|
|
else { node.warn(RED._("csv.errors.csv_js")); }
|
2014-04-29 18:01:30 +02:00
|
|
|
}
|
2015-03-24 20:02:07 +01:00
|
|
|
else { node.send(msg); } // If no payload - just pass it on.
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("csv",CSVNode);
|
2014-04-29 18:01:30 +02:00
|
|
|
}
|