From 817ef573791ee3dd5dc33700c18266b06b823fd1 Mon Sep 17 00:00:00 2001 From: "Morgan 'ARR\\!' Allen" Date: Sun, 9 Apr 2017 20:28:23 -0700 Subject: [PATCH] enable input of entire pnp csv file --- index.js | 67 ++++++++++++++++++++++++++++++++++++++---------- test/feeds.csv | 17 ++++++++---- test/fixture.csv | 15 +++++------ test/test.js | 2 +- 4 files changed, 72 insertions(+), 29 deletions(-) diff --git a/index.js b/index.js index 1d7e37f..8fe3b2c 100644 --- a/index.js +++ b/index.js @@ -4,26 +4,66 @@ var fs = require("fs"); var through = require("through2"); module.exports = function(input, output, feedFile, opts) { - var data = ""; async.waterfall([function(next) { var feeds = {}; + var buf = ""; - fs.createReadStream(feedFile).pipe(csv()) - .on("data", function(data){ - feeds[data[6]] = { - x: data[3], - y: data[4], - feed: data[5], - slot: data[2] + var section = -1; + var sections = [[], [], [], []]; + + console.log("processing sections"); + + fs.createReadStream(feedFile) + .on("data", function(data) { + buf += data.toString(); + + var lines = buf.split("\n"); + buf = lines.pop(); + + for(var i = 0; i < lines.length; i++) { + let line = lines[i].replace(/[\r\n]+$/, ""); + + if(line.length === 0) continue; + + if(line.slice(0, 2) === "%,") { + section++; + + continue; + } + + if(section === -1) { // input can be either just the CSV section or the entire pnp file + sections[1].push(line); + } else { + sections[section].push(line); + } } }) - .on("end", function() { - next(null, feeds) - }); + .on("end", function() { + next(null, sections); + }) + }, function(sections, next) { + console.log("associating parts"); + + var components = []; + var len = sections[1].length; + + for(var i = 0; i < len; i++) { + let section = sections[1][i].split(","); + + components[section[6]] = { + x: section[3], + y: section[4], + feed: section[5], + slot: section[2] + } + } + + next(null, components); }, function(feeds, next) { var rs = fs.createReadStream(input); var ws = fs.createWriteStream(output); var wsCsv = csv.createWriteStream(); + var data = ""; var seq = 0; var tr = through.obj(function(chunk, enc, done) { @@ -50,14 +90,13 @@ module.exports = function(input, output, feedFile, opts) { var feed = feeds[p]; - this.push([ 0, seq++, feed.slot, x, y, r ]); + this.push([ 0, seq++, feed.slot, x, y, r ].join(",") + "\n"); }; done(); }); rs.pipe(tr); - tr.pipe(wsCsv); - wsCsv.pipe(ws); + tr.pipe(ws); }]); } diff --git a/test/feeds.csv b/test/feeds.csv index 90dd724..4ec6af1 100644 --- a/test/feeds.csv +++ b/test/feeds.csv @@ -1,5 +1,12 @@ -65535,1,1,0.00,0.00,4.00,2.2n -65535,1,2,0.00,0.00,4.00,10uF -65535,1,3,-0.25,0.00,4.00,10nF -65545,1,4,-0.25,0.00,4.00,100nF -65535,1,5,-0.25,0.00,4.00,27nF +%,section 1 +65535,0,0.00,0.00 + +%,section 2 +65535,1,1,0.00,0.00,4.00,100nF, +65535,1,3,-0.25,0.00,4.00,0.1uF, + +%,section 3 +65535,3,0.00,0.00, + +%,section 4 +0,1,3,102.73,27.88,90,0.50,0,100,,, diff --git a/test/fixture.csv b/test/fixture.csv index f23d32b..597274f 100644 --- a/test/fixture.csv +++ b/test/fixture.csv @@ -1,9 +1,6 @@ -0,0,1,248.2850,-101.2190,0.0000 -0,1,3,237.9000,-99.6500,270.0000 -0,2,4,237.8710,-103.0732,270.0000 -0,3,4,213.8680,-99.4156,270.0000 -0,4,2,238.7600,-106.2990,180.0000 -0,5,2,228.6000,-76.6572,0.0000 -0,6,3,228.1961,-58.5445,270.0000 -0,7,2,218.4908,-51.4071,225.0000 -0,8,2,217.2284,-50.6171,250.0000 \ No newline at end of file +0,0,1,237.8710,-103.0732,270.0000 +0,1,1,213.8680,-99.4156,270.0000 +0,2,3,219.3061,-52.4408,210.0000 +0,3,3,219.9640,-55.0062,180.0000 +0,4,3,223.6470,-55.5840,90.0000 +0,5,3,214.8840,-60.2615,90.0000 diff --git a/test/test.js b/test/test.js index cd2831b..39c24a8 100644 --- a/test/test.js +++ b/test/test.js @@ -22,7 +22,7 @@ cp.stderr.on("data", function(d) { cp.on("close", function() { test(function(t) { - t.deepEqual(fs.readFileSync(output), fs.readFileSync(fixture), "output correct"); + t.deepEqual(fs.readFileSync(output).toString(), fs.readFileSync(fixture).toString(), "output correct"); t.end(); });