vue-configurations
Version:
Plugin to inject application, company and user configurations into global Vue object.
85 lines (74 loc) • 1.58 kB
JavaScript
const EventEmitter = require('events');
/**
* The config.
*/
export class ApplicationConfiguration extends EventEmitter
{
/**
* Contructor.
*/
constructor()
{
super();
this.configurations = {};
}
initialize(configurations)
{
for(let i in configurations)
{
_.set(this.configurations, i, configurations[i]);
}
}
/**
* Return current config object.
*/
all()
{
return this.configurations;
}
/**
* Lookup for a config value by key.
* key could be a dot notation string for descendant lookup.
* @param {String} key
* @param {any} def Default value
*/
get(key, def = null)
{
if(!this.has(key, this.configurations))
{
return def;
}
return key.split('.').reduce((v, k) => v && v[k], this.configurations);
}
set(key, value)
{
_.set(this.configurations, key, value);
this.emit('set', key, value);
}
/**
* Test if key is in obj.
*/
has(key)
{
try
{
const found = this._lookUp(key, this.configurations);
return (found !== undefined);
}
catch(e)
{
return false;
}
}
/**
* Performs a key lookup in obj.
*
* @param {string} key
* @param {Object} obj
*/
_lookUp(key, obj)
{
return key.split('.').reduce((v, k) => v[k], obj);
}
};
export default ApplicationConfiguration;