This commit is contained in:
Fede Ramirez
2015-03-19 05:11:58 -03:00
parent 42b00aeb14
commit 8bdeccca64
8 changed files with 333 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
"use strict;"
var fs = require('fs');
var mout = require('mout');
var extend = mout.object.deepMixIn;
var forOwn = mout.object.forOwn;
/**
* @constructor JsonFile
*/
var JsonFile = module.exports = function JsonFile(file){
if( !(this instanceof JsonFile) )
return new JsonFile(file);
this._file = String(file);
this._errors = this._errors || [];
this._json = this._json || {};
if( !(/\.json$/.test(file)) ){
this.error(this._file + ' is not json file');
} else {
try{
extend(this._json, require(this._file));
} catch(e) {
this.error(e.message);
}
}
};
JsonFile.prototype.error = function(msg){
this._errors.push(msg);
return this;
}
JsonFile.prototype.errors = function(){
return this._errors;
}
JsonFile.prototype.forEach = function(fn){
forOwn(this._json, fn, this);
return this;
};