61 lines
870 B
JavaScript
61 lines
870 B
JavaScript
'use strict';
|
|
|
|
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._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.hasErrors = function(){
|
|
|
|
return this._errors.length > 0;
|
|
};
|
|
|
|
JsonFile.prototype.forEach = function(fn){
|
|
|
|
forOwn(this._json, fn, this);
|
|
|
|
return this;
|
|
};
|
|
|
|
|
|
|