Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

envcfg

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

envcfg - npm Package Compare versions

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));
}

14

package.json

@@ -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": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc