Comparing version 0.0.3 to 0.1.0
124
index.js
module.exports = require('./lib/envcfg'); | ||
/*! | ||
* Deps. | ||
*/ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var clone = require('clone'); | ||
/** | ||
* Expose `envcfg` as the module. | ||
*/ | ||
module.exports = exports = envcfg; | ||
/** | ||
* Descriptor defaults. | ||
* | ||
* @type {Object} | ||
* @api private | ||
*/ | ||
var descriptors = { | ||
writable: false, | ||
configurable: false, | ||
enumerable: false | ||
}; | ||
/** | ||
* Expose `Config`. | ||
*/ | ||
exports.Config = Config; | ||
/** | ||
* Config constructor. | ||
* | ||
* @param {Object} | ||
* @api public | ||
*/ | ||
function Config(cfg) { | ||
define(this, 'env', { | ||
value: process.env.NODE_ENV || 'development' | ||
}); | ||
var settings = clone(cfg[this.env]) || cfg; | ||
if (cfg['*']) extend(settings, cfg['*']); | ||
Object.keys(settings).forEach(function(key) { | ||
define(this, key, { | ||
value: settings[key], | ||
enumerable: true | ||
}); | ||
}, this); | ||
} | ||
/** | ||
* Accessor for sugar. | ||
* | ||
* @param {String} key | ||
* @return {Mixed} | ||
* @api public | ||
*/ | ||
Config.prototype.get = function(key){ | ||
return this[key]; | ||
}; | ||
/** | ||
* Create a config into an environment aware one. | ||
* | ||
* @param {String|Object} config | ||
* @return {Object} | ||
* @api public | ||
*/ | ||
function envcfg(config) { | ||
config = typeof config === 'string' ? read(config) : config; | ||
return Object.freeze(new Config(config)); | ||
} | ||
/** | ||
* Reads the `file`. Handles json and modules. | ||
* | ||
* @param {String} file | ||
* @return {Object} | ||
* @api private | ||
*/ | ||
function read(file) { | ||
return path.extname(file) === '.json' | ||
? JSON.parse(fs.readFileSync(file, 'utf-8')) | ||
: require(file); | ||
} | ||
/** | ||
* Extend object `a` with object `b`. | ||
* | ||
* @param {Object} a | ||
* @param {Object} b | ||
* @return {Object} | ||
* @api private | ||
*/ | ||
function extend(a, b) { | ||
Object.keys(b).forEach(function(key) { | ||
if (!a.hasOwnProperty(key)) a[key] = b[key]; | ||
}); | ||
return a; | ||
} | ||
/** | ||
* Short hand descriptor creation. | ||
* | ||
* @param {Object} context | ||
* @param {String} key | ||
* @param {Object} value | ||
* @api private | ||
*/ | ||
function define(context, key, value){ | ||
Object.defineProperty(context, key, extend(value, descriptors)); | ||
} |
@@ -5,3 +5,3 @@ { | ||
"description": "stupid simple environment aware configuration", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"repository": { | ||
@@ -14,11 +14,9 @@ "type": "git", | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"clone": "~0.1.9" | ||
}, | ||
"devDependencies": { | ||
"mocha": "1.0.0", | ||
"should": "0.6.0" | ||
}, | ||
"optionalDependencies": {}, | ||
"engines": { | ||
"node": ">= 0.4.12" | ||
"mocha": "*", | ||
"should": "*" | ||
} | ||
} |
@@ -55,3 +55,3 @@ 'use strict'; | ||
it('should not be possible to delete values', function() { | ||
it('shouvld not be possible to delete values', function() { | ||
assert.throws(function() { | ||
@@ -73,6 +73,12 @@ delete config.foo; | ||
it('should only iterate config values', function() { | ||
assert.equal(Object.keys(config).length, 3); | ||
assert.equal(Object.keys(config).length, 4); | ||
}); | ||
}); | ||
describe('#.get(key)', function(){ | ||
it('should access the property', function(){ | ||
config.get('some stuff').should.equal('whatever'); | ||
}); | ||
}); | ||
}); |
{ | ||
"*": { | ||
"foo": "foo-*", | ||
"buz": "buzz-*" | ||
"buz": "buzz-*", | ||
"some stuff": "whatever" | ||
}, | ||
@@ -6,0 +7,0 @@ "development": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7685
192
1
9
1
+ Addedclone@~0.1.9
+ Addedclone@0.1.19(transitive)