ezzy-environment
Advanced tools
Comparing version 0.1.15 to 0.1.16
{ | ||
"name": "ezzy-environment", | ||
"description": "A lightweight tool to provide common environment settings", | ||
"version": "0.1.15", | ||
"version": "0.1.16", | ||
"author": { | ||
@@ -24,4 +24,4 @@ "name": "Moises Romero", | ||
"devDependencies": { | ||
"ezzy-testing": "^0.1.13" | ||
"ezzy-testing": "^0.1.16" | ||
} | ||
} |
@@ -5,3 +5,3 @@ let pkg; | ||
} catch (e) { | ||
pkg = {}; | ||
pkg = null; | ||
} | ||
@@ -46,2 +46,9 @@ const argument = require('ezzy-argument'); | ||
/** | ||
* Cache of entries. | ||
* @type {Object} | ||
* @private | ||
*/ | ||
this._cache = {}; | ||
} | ||
@@ -55,6 +62,6 @@ | ||
/** | ||
* If environment is in development | ||
* If environment is in development. | ||
* @type {boolean} | ||
*/ | ||
this.development = env === 'development' || env === 'dev'; | ||
this.development = env.indexOf('dev') > -1; | ||
@@ -68,9 +75,15 @@ /** | ||
/** | ||
* If environment is in alpha | ||
* If environment is in testing. | ||
* @type {boolean} | ||
*/ | ||
this.test = env.indexOf('test') > -1; | ||
/** | ||
* If environment is in alpha. | ||
* @type {boolean} | ||
*/ | ||
this.alpha = env === 'alpha'; | ||
/** | ||
* If environment is in beta | ||
* If environment is in beta. | ||
* @type {boolean} | ||
@@ -81,3 +94,3 @@ */ | ||
/** | ||
* If environment is in gamma | ||
* If environment is in gamma. | ||
* @type {boolean} | ||
@@ -90,3 +103,15 @@ */ | ||
*/ | ||
this.name = env; | ||
if (this.dev) { | ||
this.name = 'development'; | ||
} else if (this.test) { | ||
this.name = 'test'; | ||
} else if (this.alpha) { | ||
this.name = 'alpha'; | ||
} else if (this.beta) { | ||
this.name = 'beta'; | ||
} else if (this.gamma) { | ||
this.name = 'gamma'; | ||
} else { | ||
this.name = 'production'; | ||
} | ||
@@ -102,4 +127,4 @@ /** | ||
*/ | ||
this.production = | ||
!this.development && !this.alpha && !this.beta && !this.gamma; | ||
this.production = !this.development && !this.alpha && | ||
!this.beta && !this.gamma && !this.test; | ||
} | ||
@@ -123,3 +148,3 @@ | ||
*/ | ||
set(key, value) { | ||
set (key, value) { | ||
this[key] = value; | ||
@@ -134,3 +159,3 @@ } | ||
*/ | ||
get(key, defaultValue) { | ||
get (key, defaultValue) { | ||
return this[key] || defaultValue; | ||
@@ -166,26 +191,27 @@ } | ||
getConfiguration(scope, defaultConfig = {}) { | ||
let config = pkg[scope] || pkg[`_${scope}`] || defaultConfig; | ||
if (typeof config === 'object') { | ||
if (this._cache[scope]) { | ||
return this._cache[scope]; | ||
} | ||
let subPkg = pkg[this.name] || pkg[`_${this.name}`]; | ||
if (subPkg) { | ||
const subConfig = subPkg[scope] || subPkg[`_${scope}`]; | ||
if (subConfig) { | ||
if (typeof subConfig === 'object') { | ||
config = deepmerge(config, subConfig); | ||
} else { | ||
config = subConfig; | ||
} | ||
} | ||
} | ||
let configuration = pkg || defaultConfig; | ||
const scopes = scope.split('.'); | ||
const namespace = scopes.shift(); | ||
const subScopes = scopes.join('.'); | ||
subPkg = config[this.name] || config[`_${this.name}`]; | ||
if (subPkg) { | ||
if (typeof subPkg === 'object') { | ||
config = deepmerge(config, subPkg); | ||
} else { | ||
config = subPkg; | ||
} | ||
} | ||
if (typeof configuration[this.name] === 'object') { | ||
configuration = deepmerge(configuration, configuration[this.name]); | ||
} | ||
let config = configuration[namespace] || configuration[`_${namespace}`]; | ||
if (typeof config === 'object' && typeof config[this.name] === 'object') { | ||
config = deepmerge(config, config[this.name]); | ||
} | ||
if (subScopes.length) { | ||
config = eval(`config.${scopes.join('.')}`); | ||
} | ||
this._cache[scope] = config; | ||
return config; | ||
@@ -192,0 +218,0 @@ } |
@@ -15,3 +15,4 @@ const Environment = require('./Environment'); | ||
expect(environment.gamma).toBe(false); | ||
expect(environment.production).toBe(true); | ||
expect(environment.production).toBe(false); | ||
expect(environment.test).toBe(true); | ||
done(); | ||
@@ -36,12 +37,25 @@ }); | ||
const config1 = environment.getConfiguration('testProp', { | ||
a: 1, | ||
b: 5, | ||
c: { | ||
prop1: true | ||
}, | ||
production: { | ||
a: 2, | ||
testProp: { | ||
a: 1, | ||
b: 5, | ||
c: { | ||
prop2: true | ||
prop1: true | ||
}, | ||
d: { | ||
outside: 1, | ||
inside: 2 | ||
}, | ||
test: { | ||
a: 2, | ||
c: { | ||
prop2: true | ||
} | ||
} | ||
}, | ||
test: { | ||
testProp: { | ||
d: { | ||
inside: 3 | ||
} | ||
} | ||
} | ||
@@ -52,5 +66,44 @@ }); | ||
expect(config1.c.prop1).toEqual(config1.c.prop2); | ||
expect(config1.d.inside).toEqual(3); | ||
expect(config1.d.outside).toEqual(1); | ||
done(); | ||
}); | ||
it('should be able to access properties in dot notation', () => { | ||
const conf = { | ||
_anotherProp: { | ||
prop1: { | ||
a: 2 | ||
} | ||
}, | ||
testProp: { | ||
prop1: { | ||
prop2: { | ||
a: 1 | ||
} | ||
} | ||
}, | ||
test: { | ||
_anotherProp: { | ||
prop2: { | ||
a: 2 | ||
} | ||
}, | ||
testProp: { | ||
prop1: { | ||
prop3: { | ||
a: 1 | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
expect(environment.getConfiguration('testProp.prop1.prop2.a', conf)) | ||
.toBe(1); | ||
expect(environment.getConfiguration('testProp.prop1.prop3.a', conf)) | ||
.toBe(1); | ||
expect(environment.getConfiguration('anotherProp.prop1.a', conf)) | ||
.toBe(environment.getConfiguration('anotherProp.prop2.a', conf)); | ||
}) | ||
}); |
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
10071
283
1