Merge pull request #90 from midknight41/master

added support for BOM in load() and loadSync(), fixed travis build too.
This commit is contained in:
Jarrett Cruger 2013-10-26 17:05:16 -07:00
commit 5f148d5aa5
7 changed files with 105 additions and 6 deletions

2
.gitignore vendored
View file

@ -3,6 +3,8 @@ config.json
test/fixtures/*.json
!test/fixtures/complete.json
!test/fixtures/malformed.json
!test/fixtures/bom.json
!test/fixtures/no-bom.json
node_modules/
node_modules/*
npm-debug.log

View file

@ -1,10 +1,10 @@
language: node_js
node_js:
- 0.4
- 0.6
- 0.8
- 0.9
- 0.10
- 0.11
notifications:
email:
- travis@nodejitsu.com

View file

@ -97,7 +97,12 @@ File.prototype.load = function (callback) {
}
try {
self.store = self.format.parse(data.toString());
//deals with string that include BOM
var stringData = data.toString();
if (stringData.charAt(0) === '\uFEFF') stringData = stringData.substr(1);
self.store = self.format.parse(stringData);
}
catch (ex) {
return callback(new Error("Error parsing your JSON configuration file: [" + self.file + '].'));
@ -125,7 +130,11 @@ File.prototype.loadSync = function () {
// Else, the path exists, read it from disk
//
try {
data = this.format.parse(fs.readFileSync(this.file, 'utf8'));
//deals with file that include BOM
var fileData = fs.readFileSync(this.file, 'utf8');
if (fileData.charAt(0) === '\uFEFF') fileData = fileData.substr(1);
data = this.format.parse(fileData);
this.store = data;
}
catch (ex) {

View file

@ -22,7 +22,7 @@
"pkginfo": "0.2.x"
},
"devDependencies": {
"vows": "0.6.x"
"vows": "0.7.x"
},
"main": "./lib/nconf",
"scripts": {

19
test/fixtures/bom.json vendored Normal file
View file

@ -0,0 +1,19 @@
{
"I've seen things": {
"like": [
"carrots",
"handbags",
"cheese",
"toilets",
"russians",
"planets",
"hampsters",
"weddings",
"poets",
"stalin",
"kuala lumpur"
]
},
"host": "weebls-stuff.com",
"port": 78304
}

19
test/fixtures/no-bom.json vendored Normal file
View file

@ -0,0 +1,19 @@
{
"I've seen things": {
"like": [
"carrots",
"handbags",
"cheese",
"toilets",
"russians",
"planets",
"hampsters",
"weddings",
"poets",
"stalin",
"kuala lumpur"
]
},
"host": "weebls-stuff.com",
"port": 78304
}

View file

@ -47,6 +47,56 @@ vows.describe('nconf/stores/file').addBatch({
assert.match(err, /malformed\.json/);
}
}
},
"with a valid UTF8 JSON file that contains a BOM": {
topic: function () {
var filePath = path.join(__dirname, '..', 'fixtures', 'bom.json');
this.store = store = new nconf.File({ file: filePath });
return null;
},
"the load() method": {
topic: function () {
this.store.load(this.callback);
},
"should load the data correctly": function (err, data) {
assert.isNull(err);
assert.deepEqual(data, this.store.store);
}
},
"the loadSync() method": {
topic: function () {
var data = this.store.loadSync();
return data;
},
"should load the data correctly": function (result) {
assert.deepEqual(result, this.store.store);
}
}
},
"with a valid UTF8 JSON file that contains no BOM": {
topic: function () {
var filePath = path.join(__dirname, '..', 'fixtures', 'no-bom.json');
this.store = store = new nconf.File({ file: filePath });
return null;
},
"the load() method": {
topic: function () {
this.store.load(this.callback);
},
"should load the data correctly": function (err, data) {
assert.isNull(err);
assert.deepEqual(data, this.store.store);
}
},
"the loadSync() method": {
topic: function () {
var data = this.store.loadSync();
return data;
},
"should load the data correctly": function (result) {
assert.deepEqual(result, this.store.store);
}
}
}
}
}).addBatch({