Skip to content

Instantly share code, notes, and snippets.

@adriancbo
Created September 25, 2014 12:42
Show Gist options
  • Save adriancbo/f87b2547d0f78aa8038a to your computer and use it in GitHub Desktop.
Save adriancbo/f87b2547d0f78aa8038a to your computer and use it in GitHub Desktop.
NodeJS Process File then Output to File
var fs = require('fs');
fs.readFile('cats.txt', function(err, data) {
if(err) throw err;
var array = data.toString().split("\n");
var lookup = array.map(function(line) {
var significant = line.split('. ')[1].split(' (');
return { category: significant[0], id: significant[1].split(')')[0] };
});
var json = JSON.stringify(lookup);
fs.writeFile('cats.js', json, function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment