Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
91 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.DS_STORE | ||
node_modules | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Require the sdk from the root of directory | ||
module.exports = require('./lib/csv'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); | ||
}); | ||
|