Comparing version 0.3.2 to 0.4.0
18
index.js
@@ -35,12 +35,22 @@ var fs = require('fs'); | ||
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; | ||
}; | ||
@@ -47,0 +57,0 @@ |
{ | ||
"name": "habitat", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"description": "Small library for managing environment variables", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# habitat [](http://travis-ci.org/brianloveswords/habitat) | ||
## Version 0.3.0 | ||
## Version 0.3.2 | ||
@@ -41,11 +41,2 @@ Library for managing your environment vars. | ||
Note that you don't have to pass in a prefix, if you're crazy like | ||
that. Keys will be looked up (after capitalization) straight from the | ||
env: | ||
```js | ||
var env = new habitat; | ||
var path= env.get('path'); | ||
``` | ||
`defaults` is an object representing the defaults if a key cannot be | ||
@@ -55,4 +46,4 @@ found in the environment. This should be used sparingly. | ||
```js | ||
var env = new habitat('airsupport', { port: 3000 }) | ||
// will try the environment first, then fall back to 3000 | ||
var env = new habitat('airsupport', { port: 1024 }) | ||
// will try the environment first, then fall back to 1024 | ||
var port = env.get('port'); | ||
@@ -99,3 +90,4 @@ ``` | ||
### Getting keys using camelCase | ||
You can also use camelcase instead of underscores if you want, habitat's got your back. | ||
You can also use camelcase instead of underscores if you want, habitat's | ||
got your back. | ||
@@ -113,3 +105,4 @@ ```bash | ||
You can also use `get` directly from the habitat objects to get unprefixed things from the environment. | ||
You can also use `get` directly from the habitat object to get | ||
unprefixed things from the environment. | ||
@@ -134,3 +127,3 @@ ```js | ||
``` | ||
```js | ||
habitat.load('/some/directory/.env'); // returns true on success | ||
@@ -137,0 +130,0 @@ console.dir(habitat.get('parameter')); // { one: 'one', two: 'two' } |
@@ -162,2 +162,12 @@ var fs = require('fs'); | ||
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) { | ||
@@ -164,0 +174,0 @@ process.env['APP_REDIS_HOST'] = 'localhost'; |
17353
377
197
36