ext-botanjs/config.js

53 lines
930 B
JavaScript
Raw Normal View History

2016-05-31 09:54:50 +00:00
var Conf = function()
{
this.kv = {};
};
var ResolvePath = function( path, _obj )
{
var p = path.split(".");
var l = p.length - 1;
if( !l ) return { obj: _obj, key: path };
if( !_obj[ p[0] ] ) _obj[ p[0] ] = {};
var obj = _obj[ p[0] ];
for( var i = 1; i < l; i ++ )
{
if( !obj[ p[i] ] ) obj[ p[i] ] = {};
obj = obj[ p[i] ];
}
return { obj: obj, key: p[l] };
};
/**
* Example:
* Conf.set( "a.b.c.d", 7 );
* gives { a: { b: { c: { d: 7 } } } }
**/
Conf.prototype.set = function( path, val )
{
var o = ResolvePath( path, this.kv );
o.obj[ o.key ] = val;
};
Conf.prototype.push = function( path, val )
{
var o = ResolvePath( path, this.kv );
o.obj[ o.key ]
? o.obj[ o.key ].push( val )
: o.obj[ o.key ] = [ val ]
;
};
Conf.prototype.toString = function()
{
return "var _AstConf_ = " + JSON.stringify( this.kv )
+ ( global.debug ? ", debugEnv = true" : "" )
+ ";";
};
module.exports = Conf;