Skip to content
This repository
Browse code

`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...
commit 4db6bbbbdbf3a6a60ca50550f2ab5a206d57494c 1 parent b76aa75
Brian J Brennan authored November 03, 2012
18  index.js
@@ -32,16 +32,26 @@ habitat.prototype.setDefaults = function setDefaults(defaults) {
32 32
  */
33 33
 
34 34
 habitat.prototype.get = function get(key, someDefault) {
  35
+  var value, envkey;
35 36
   if (key.match(/[a-z]+[A-Z]/))
36 37
     return this.get(fromCamelCase(key));
37 38
 
38  
-  var envkey = this.envkey(key);
39  
-  var value = process.env[envkey] || someDefault;
  39
+  envkey = this.envkey(key);
  40
+  value = process.env[envkey];
  41
+
  42
+  // try straight up
40 43
   if (typeof value !== 'undefined')
41 44
     return habitat.parse(value);
  45
+
  46
+  // try as an object
42 47
   value = this.getAsObject(key);
43  
-  if (Object.keys(value).length)
44  
-    return value;
  48
+  if (typeof value !== 'undefined') {
  49
+    if (Object.keys(value).length)
  50
+      return value;
  51
+  }
  52
+
  53
+  // fallback
  54
+  return someDefault;
45 55
 };
46 56
 
47 57
 /**
2  package.json
... ...
@@ -1,6 +1,6 @@
1 1
 {
2 2
   "name": "habitat",
3  
-  "version": "0.3.2",
  3
+  "version": "0.4.0",
4 4
   "description": "Small library for managing environment variables",
5 5
   "main": "index.js",
6 6
   "directories": {
10  test/habitat.test.js
@@ -159,6 +159,16 @@ test('habitat#get: should try `getAsObject` if no value is found', function (t)
159 159
   t.end();
160 160
 });
161 161
 
  162
+test('habitat#get: should not use default when there is a valid obj', function (t) {
  163
+  process.env['APP_REDIS_HOST'] = 'localhost';
  164
+  process.env['APP_REDIS_PORT'] = 3000;
  165
+  var env = new habitat('app');
  166
+  var obj = env.get('redis', { host: 'nope', port: 1e300 });
  167
+  t.same(obj.host, 'localhost');
  168
+  t.same(obj.port, 3000);
  169
+  t.end();
  170
+});
  171
+
162 172
 test('habitat#get: should try to expand camelcase', function (t) {
163 173
   process.env['APP_REDIS_HOST'] = 'localhost';
164 174
   process.env['APP_REDIS_PORT'] = 3000;

0 notes on commit 4db6bbb

Please sign in to comment.
Something went wrong with that request. Please try again.