Skip to content

Commit

Permalink
Surfacing additional JSON.stringify arguments in formats.json.stringi…
Browse files Browse the repository at this point in the history
…fy, and adding the json_spacing option to the File constructor.
  • Loading branch information
ljharb authored and jfhbrook committed Apr 13, 2012
1 parent b369931 commit 6ce0b7a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/nconf/formats.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ var formats = exports;
// Standard JSON format which pretty prints `.stringify()`.
//
formats.json = {
stringify: function (obj) {
return JSON.stringify(obj, null, 2)
stringify: function (obj, replacer, spacing) {
return JSON.stringify(obj, replacer || null, spacing || 2)
},
parse: JSON.parse
};
Expand All @@ -25,4 +25,4 @@ formats.json = {
// Standard INI format supplied from the `ini` module
// http://en.wikipedia.org/wiki/INI_file
//
formats.ini = ini;
formats.ini = ini;
6 changes: 3 additions & 3 deletions lib/nconf/stores/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var File = exports.File = function (options) {
this.file = options.file;
this.dir = options.dir || process.cwd();
this.format = options.format || formats.json;
this.json_spacing = options.json_spacing || 2;

if (options.search) {
this.search(this.dir);
Expand All @@ -51,7 +52,7 @@ File.prototype.save = function (value, callback) {
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();
});
};
Expand All @@ -65,7 +66,7 @@ File.prototype.save = function (value, callback) {
//
File.prototype.saveSync = function (value) {
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) {
throw(ex);
Expand Down Expand Up @@ -224,4 +225,3 @@ File.prototype.search = function (base) {

return fullpath;
};

0 comments on commit 6ce0b7a

Please sign in to comment.