Last active
October 19, 2015 18:32
-
-
Save rgamez/38af3cf5d2bd1c4aad05 to your computer and use it in GitHub Desktop.
Pretty print a json skipping the BOM if present
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
#!/usr/bin/env node | |
var fs = require('fs'); | |
process.stdout.on('error', function(err) { | |
if (err.code == "EPIPE") { | |
process.exit(0); | |
} | |
}); | |
var prettyPrint = function(json) { | |
return JSON.stringify(JSON.parse(json), null, ' '); | |
}; | |
fs.readFile('locations.json', function(err, buffer) { | |
if (err) { | |
console.error(err); | |
return; | |
} | |
var prettyJSON; | |
var bom = new Buffer(3); | |
buffer.copy(bom, 0); | |
if (bom.equals(new Buffer([0xef, 0xbb, 0xbf]))) { | |
prettyJSON = prettyPrint(buffer.slice(3)); | |
} | |
else { | |
prettyJSON = prettyPrint(buffer); | |
} | |
process.stdout.write(prettyJSON); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment