Skip to content

Instantly share code, notes, and snippets.

@code-for-coffee
Last active November 3, 2016 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-for-coffee/0793992d6a521b813db3ac1aa373df73 to your computer and use it in GitHub Desktop.
Save code-for-coffee/0793992d6a521b813db3ac1aa373df73 to your computer and use it in GitHub Desktop.
ExoplanetClass
/**
* Created by codeforcoffee on 11/2/16.
*/
const Converter = require("csvtojson").Converter;
const fs = require('fs');
const path = require('path');
class CSVObectifier {
constructor(csvFileData) {
this.converter = new Converter({});
this.csv = csvFileData;
};
convert(callback) {
this.converter.fromString(this.csv, function(err, json){
callback(json);
});
};
writeFile(filename) {
if (!filename) filename = 'default.json';
var x = (result) => {
console.log(result);
fs.writeFileSync(filename, JSON.stringify(result), 'utf8');
};
this.convert(x);
};
};
// test our class and verify things work
var data = fs.readFileSync(path.join(__dirname, 'planets.csv'), { encoding : 'utf8'});
console.log(data);
let x = new CSVObectifier(data);
x.writeFile('data.json');
//module.exports = CSVObectifier;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment