Surfacing additional JSON.stringify arguments in formats.json.stringify, and adding the json_spacing option to the File constructor.

This commit is contained in:
Jordan Harband 2012-04-04 23:58:27 -07:00 committed by Joshua Holbrook
parent b3699314cf
commit 6ce0b7aef3
2 changed files with 6 additions and 6 deletions

View file

@ -14,8 +14,8 @@ var formats = exports;
// Standard JSON format which pretty prints `.stringify()`. // Standard JSON format which pretty prints `.stringify()`.
// //
formats.json = { formats.json = {
stringify: function (obj) { stringify: function (obj, replacer, spacing) {
return JSON.stringify(obj, null, 2) return JSON.stringify(obj, replacer || null, spacing || 2)
}, },
parse: JSON.parse parse: JSON.parse
}; };

View file

@ -29,6 +29,7 @@ var File = exports.File = function (options) {
this.file = options.file; this.file = options.file;
this.dir = options.dir || process.cwd(); this.dir = options.dir || process.cwd();
this.format = options.format || formats.json; this.format = options.format || formats.json;
this.json_spacing = options.json_spacing || 2;
if (options.search) { if (options.search) {
this.search(this.dir); this.search(this.dir);
@ -51,7 +52,7 @@ File.prototype.save = function (value, callback) {
value = null; value = null;
} }
fs.writeFile(this.file, this.format.stringify(this.store), function (err) { fs.writeFile(this.file, this.format.stringify(this.store, null, this.json_spacing), function (err) {
return err ? callback(err) : callback(); return err ? callback(err) : callback();
}); });
}; };
@ -65,7 +66,7 @@ File.prototype.save = function (value, callback) {
// //
File.prototype.saveSync = function (value) { File.prototype.saveSync = function (value) {
try { try {
fs.writeFileSync(this.file, this.format.stringify(this.store)); fs.writeFileSync(this.file, this.format.stringify(this.store, null, this.json_spacing));
} }
catch (ex) { catch (ex) {
throw(ex); throw(ex);
@ -224,4 +225,3 @@ File.prototype.search = function (base) {
return fullpath; return fullpath;
}; };