Files
jspm-registry/test/lib/JsonFile.js
T

56 lines
789 B
JavaScript
Raw Normal View History

2015-03-23 05:15:10 -03:00
'use strict';
2015-03-19 05:11:58 -03:00
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);
2015-03-23 05:15:10 -03:00
this._errors = [];
this._json = {};
2015-03-19 05:11:58 -03:00
if( !(/\.json$/.test(file)) ){
this.error(this._file + ' is not json file');
} else {
2015-03-23 05:15:10 -03:00
try {
2015-03-19 05:11:58 -03:00
extend(this._json, require(this._file));
} catch(e) {
this.error(e.message);
}
}
};
JsonFile.prototype.error = function(msg){
this._errors.push(msg);
return this;
2015-03-23 05:15:10 -03:00
};
2015-03-19 05:11:58 -03:00
JsonFile.prototype.errors = function(){
return this._errors;
2015-03-23 05:15:10 -03:00
};
2015-03-19 05:11:58 -03:00
JsonFile.prototype.forEach = function(fn){
forOwn(this._json, fn, this);
2015-03-23 18:35:35 -03:00
return this;
2015-03-19 05:11:58 -03:00
};