Permalink
Browse files

Surfacing additional JSON.stringify arguments in formats.json.stringi…

…fy, and adding the json_spacing option to the File constructor.
  • Loading branch information...
1 parent b369931 commit 6ce0b7aef3c789eaeb56c04c0f12d0f62e43b3de @ljharb ljharb committed with jfhbrook Apr 5, 2012
Showing with 6 additions and 6 deletions.
  1. +3 −3 lib/nconf/formats.js
  2. +3 −3 lib/nconf/stores/file.js
View
@@ -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
};
@@ -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;
View
@@ -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);
@@ -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();
});
};
@@ -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);
@@ -224,4 +225,3 @@ File.prototype.search = function (base) {
return fullpath;
};
-

0 comments on commit 6ce0b7a

Please sign in to comment.