Skip to content

Commit

Permalink
work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredkam committed Jan 15, 2015
1 parent eff2c32 commit 2d9c06a
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 51 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -1 +1,3 @@
.DS_STORE
node_modules

50 changes: 0 additions & 50 deletions csv-to-json/csv.js

This file was deleted.

2 changes: 2 additions & 0 deletions csv-to-json/index.js
@@ -0,0 +1,2 @@
// Require the sdk from the root of directory
module.exports = require('./lib/csv');
44 changes: 44 additions & 0 deletions csv-to-json/lib/csv.js
@@ -0,0 +1,44 @@
var fs = require('fs');

module.exports = {
setfs: function (fsModule) {
fs = fsModule;
},
parse: function (config, next) {
var filename = config.filename;
var opts = config.opts || {}; // options to enable configurations
var json = [];

if (!filename) {
next('Missing filename');
}

var csv = fs.readFileSync(filename).toString().split("\n");
var tokens = csv[0].split(",");

for(var i=1;i < csv.length;i++) {
var content = csv[i].split(",");
var tmp = {};
for(var j=0;j < tokens.length; j++) {
try {
tmp[tokens[j]] = content[j];
} catch(err) {
tmp[tokens[j]] = "";
}
}
json.push(tmp);
}
next(null, json);
return;
},
write: function (config, next) {
fs.writeFile(filename, JSON.stringify(json), function(err) {
if (err) {
next(err);
return;
}
next(null, "File saved");
return;
});
}
};
2 changes: 1 addition & 1 deletion csv-to-json/package.json
@@ -1,6 +1,6 @@
{
"name": "csv-to-json",
"version": "0.0.2",
"version": "0.0.4",
"description": "parses csv and returns it into json object",
"main": "csv.js",
"scripts": {
Expand Down
42 changes: 42 additions & 0 deletions csv-to-json/test/index-test.js
@@ -0,0 +1,42 @@
var chai = require('chai');
var expect = chai.expect;
var csv = require('../lib/csv');

describe('csv parse test', function (){
var fs = {
readFileSync: function () {
return; // should return a string
}
};
// mock fs
csv.setfs(fs);

it('should be able to parse csv', function (done) {
var json = csv.parse({
filename: ''
}, function (json) {
expect(json).to.be.an('object');
done();
});
});
});

describe('csv write test', function (){
var fs = {
writeFile: function () {

}
};
it('should be able to write csv', function (done) {
csv.write({
json: json,
filename: '',
opts: {

}
}, function () {
done();
});
});
});

0 comments on commit 2d9c06a

Please sign in to comment.