Permalink
Browse files

`habitat#get` should behave properly [bump]

* When trying to get an object by prefix, if a default value was passed,
it would always use that. This fixes that bug, but I consider it an API
change just in case anyone relied on that misbehavior.
  • Loading branch information...
1 parent b76aa75 commit 4db6bbbbdbf3a6a60ca50550f2ab5a206d57494c @brianloveswords committed Nov 3, 2012
Showing with 25 additions and 5 deletions.
  1. +14 −4 index.js
  2. +1 −1 package.json
  3. +10 −0 test/habitat.test.js
View
@@ -32,16 +32,26 @@ habitat.prototype.setDefaults = function setDefaults(defaults) {
*/
habitat.prototype.get = function get(key, someDefault) {
+ var value, envkey;
if (key.match(/[a-z]+[A-Z]/))
return this.get(fromCamelCase(key));
- var envkey = this.envkey(key);
- var value = process.env[envkey] || someDefault;
+ envkey = this.envkey(key);
+ value = process.env[envkey];
+
+ // try straight up
if (typeof value !== 'undefined')
return habitat.parse(value);
+
+ // try as an object
value = this.getAsObject(key);
- if (Object.keys(value).length)
- return value;
+ if (typeof value !== 'undefined') {
+ if (Object.keys(value).length)
+ return value;
+ }
+
+ // fallback
+ return someDefault;
};
/**
View
@@ -1,6 +1,6 @@
{
"name": "habitat",
- "version": "0.3.2",
+ "version": "0.4.0",
"description": "Small library for managing environment variables",
"main": "index.js",
"directories": {
View
@@ -159,6 +159,16 @@ test('habitat#get: should try `getAsObject` if no value is found', function (t)
t.end();
});
+test('habitat#get: should not use default when there is a valid obj', function (t) {
+ process.env['APP_REDIS_HOST'] = 'localhost';
+ process.env['APP_REDIS_PORT'] = 3000;
+ var env = new habitat('app');
+ var obj = env.get('redis', { host: 'nope', port: 1e300 });
+ t.same(obj.host, 'localhost');
+ t.same(obj.port, 3000);
+ t.end();
+});
+
test('habitat#get: should try to expand camelcase', function (t) {
process.env['APP_REDIS_HOST'] = 'localhost';
process.env['APP_REDIS_PORT'] = 3000;

0 comments on commit 4db6bbb

Please sign in to comment.