diff --git a/packages/node_modules/@node-red/nodes/core/parsers/70-CSV.js b/packages/node_modules/@node-red/nodes/core/parsers/70-CSV.js index 8ab296159..184f40bdd 100644 --- a/packages/node_modules/@node-red/nodes/core/parsers/70-CSV.js +++ b/packages/node_modules/@node-red/nodes/core/parsers/70-CSV.js @@ -203,7 +203,7 @@ module.exports = function(RED) { 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 (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].trim()) ) { k[j] = parseFloat(k[j].trim()); } if (node.include_null_values && k[j] === null) o[template[j]] = k[j]; if (node.include_empty_strings && k[j] === "") o[template[j]] = k[j]; if (k[j] !== null && k[j] !== "") o[template[j]] = k[j]; @@ -218,7 +218,7 @@ module.exports = function(RED) { if ( template[j] && (template[j] !== "") ) { // 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 ( (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].trim()) ) { k[j] = parseFloat(k[j].trim()); } else { if (k[j] !== null) k[j].replace(/\r$/,''); } if (node.include_null_values && k[j] === null) o[template[j]] = k[j]; if (node.include_empty_strings && k[j] === "") o[template[j]] = k[j]; @@ -242,7 +242,7 @@ module.exports = function(RED) { if (!node.goodtmpl) { template[j] = "col"+(j+1); } 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].trim()) ) { k[j] = parseFloat(k[j].trim()); } else { if (k[j] !== null) k[j].replace(/\r$/,''); } if (node.include_null_values && k[j] === null) o[template[j]] = k[j]; if (node.include_empty_strings && k[j] === "") o[template[j]] = k[j]; diff --git a/test/nodes/core/parsers/70-CSV_spec.js b/test/nodes/core/parsers/70-CSV_spec.js index f8e99ac31..cb8d7ca09 100644 --- a/test/nodes/core/parsers/70-CSV_spec.js +++ b/test/nodes/core/parsers/70-CSV_spec.js @@ -305,6 +305,22 @@ describe('CSV node', function() { }); }); + it('should parse numbers when told to do so', function(done) { + var flow = [ { id:"n1", type:"csv", temp:"a,b,c,d,e,f,g", wires:[["n2"]] }, + {id:"n2", type:"helper"} ]; + helper.load(csvNode, flow, function() { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n2.on("input", function(msg) { + msg.should.have.property('payload', { a: 1.23, b: -123, c: 1000, d: 0 }); + check_parts(msg, 0, 1); + done(); + }); + var testString = ' 1.23 , -123,1e3 , 0 '+String.fromCharCode(10); + n1.emit("input", {payload:testString}); + }); + }); + 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"]] }, {id:"n2", type:"helper"} ];