Skip to content

Commit

Permalink
Allow preview mode to do transforms. fixes issue #120 (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
glynnbird authored Jan 20, 2017
1 parent db66f5d commit 5d442ed
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
14 changes: 13 additions & 1 deletion includes/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,19 @@ var stream = function(rs, opts, callback) {
rs.destroy(rs);
if (!calledback) {
calledback = true;
analyseString(str, callback);
analyseString(str, function (err, data, delimiter) {

// allow transformation in preview
if (opts.COUCH_TRANSFORM && typeof opts.COUCH_TRANSFORM === 'function') {
var func = opts.COUCH_TRANSFORM;
for (var i in data) {
data[i] = func(data[i], opts.COUCH_META);
}
}

callback(err, data, delimiter);

});
}
}).on('error', function(e) {
if (!calledback) {
Expand Down
30 changes: 23 additions & 7 deletions test/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,43 @@ describe('Preview mode', function() {
it('preview csv', function(done) {
couchimport.previewCSVFile("./test/test.csv", opts, function(err,data, delimiter) {
assert.equal(err, null);
data.should.be.an.Object;
data.should.be.an.Array;
delimiter.should.be.a.String;
delimiter.should.equal(",");
done();
});
});

if('preview tsv', function(done) {
couchimport.previewCSVFile("./test/guitars.csv", opts, function(err,data, delimiter) {
it('preview tsv', function(done) {
couchimport.previewCSVFile("./test/guitars.tsv", opts, function(err,data, delimiter) {
assert.equal(err, null);
data.should.be.an.Object;
data.should.be.an.Array;
delimiter.should.be.a.String;
delimiter.should.equal("\t");
done();
});
})

if('preview nonsense', function(done) {
couchimport.previewCSVFile("./test/guitars.csv", opts, function(err,data, delimiter) {
it('preview tsv with transform', function(done) {
opts.COUCH_TRANSFORM = function(doc) {
doc.price = parseFloat(doc.price);
return doc;
};
couchimport.previewCSVFile("./test/guitars.tsv", opts, function(err,data, delimiter) {
assert.equal(err, null);
data.should.be.an.Object;
data.should.be.an.Array;
data[0].should.be.an.Object;
data[0].price.should.be.a.Number;
delimiter.should.be.a.String;
delimiter.should.equal("\t");
done();
});
})

it('preview nonsense', function(done) {
couchimport.previewCSVFile("./test/guitars.tsv", opts, function(err,data, delimiter) {
assert.equal(err, null);
data.should.be.an.Array;
delimiter.should.be.a.String;
delimiter.should.equal("\t");
done();
Expand Down

0 comments on commit 5d442ed

Please sign in to comment.