0
var FileHasher = require("./FileHasher");

if (process.argv.length < 3) {
  console.log("Program file and one argument are required for this program.");
  process.exit(1);
}

var filename = process.argv[2];
var fs = require('fs');
var file = fs.readFileSync(filename, "UTF-8"); // get file contents
var data = fs.readFileSync("../sha1.txt", "UTF-8"); // read current key-value pairs

console.log(data); // print all current key-value pairs

var filesha1 = FileHasher(file);

console.log(filesha1);

var object = {
  name: filename,
  value: filesha1
};

console.log(object);

fs.appendFile("../sha1.txt", JSON.stringify(object), function(err) {
  console.log(err);
  console.log(object); // prints object so I know it was stored correctly
});

Right now everything works fine. However, when saving the object to the text file, instead of getting this format:

{
  "name":"sha1.js",
  "value":"6d358b6f267e22e327c1028e79a5c8b200bf453d"
};

I get this:

{"name":"sha1.js","value":"6d358b6f267e22e327c1028e79a5c8b200bf453d"}

I want to change the spacing and add the semicolon when the object is saved. In the future I also plan on parsing this list to check whether or not a file already exists, and to change the value of the value upon an update in the sha1 of the file. A guiding hand towards the right way to format my code or object, or perhaps a different function I should be using, would be much appreciated!

1 Answers1

2

Those two things are identical, when parsed, ignoring the semicolon. Semicolons are not valid after JSON objects, so you should have those between records (a comma would be more appropriate, if anything).

Now, the JSON.stringify function has a few little-known parameters controlling whitespace and other pretty-print features. It looks like you want JS-standard two-space indentation, which would be a call like JSON.stringify(object, null, ' ').

To add the semicolon -- which you really shouldn't do -- you can simply use string concatenation:

fs.appendFile("../sha1.txt", JSON.stringify(object) + ';\n', function(err) {

This will add a semicolon and newline after the JSON string. Since the semicolon is added after serialization, it will occur at the end of the message regardless of line count.

ssube
  • 47,010
  • 7
  • 103
  • 140
  • Thanks for the quick response!! I've been digging around on the right way to format a key value pair, and found [this response](http://stackoverflow.com/questions/6397990/storing-a-key-value-array-into-a-compact-json-string). As a result, I'm not sure how to store a new key:value pair inside the the brackets. Some guidance with that would be much appreciated! –  Jul 13 '15 at 16:00
  • 1
    To add a new property to a JSON object, you'll need to parse it into a JS object (`JSON.parse`), add the property (`obj.newKey = newValue`), then serialize it back into JSON and write it to the file. You can't (easily) do an inline-append to a JSON object. – ssube Jul 13 '15 at 16:03
  • Ah gotcha. I'll give that a shot. While reading up on key-value pairs and editing files with them, I came across [this answer](http://stackoverflow.com/questions/6397990/storing-a-key-value-array-into-a-compact-json-string). Any thoughts on which format would be best for my use? using 'meta' seemed pretty nice since it would automatically identify the pairs as key:value, but dealing with key-value literal seems to be the simplest as I don't have to worry about brackets within brackets. –  Jul 13 '15 at 16:17
  • Just use JSON. It's much easier to parse (you don't have to write any code) and even the cheapest compression (like ZIP) will deduplicate the keys and make size a non-issue. – ssube Jul 13 '15 at 16:21
  • Ahh I gotcha. I don't need any code to do the actual parsing if I use key-value literal, and inserting isn't that hard - just follow the three steps you provided in your previous answer, right? Correct me if I'm wrong though - I am a newcomer when it comes to node.js –  Jul 13 '15 at 16:31