Permalink
Browse files

`get` can now get objects by prefix [bump]

  • Loading branch information...
1 parent ed906f0 commit 4f446a30b008390d19771daaf5333b7913dba35b @brianloveswords committed Oct 6, 2012
Showing with 83 additions and 9 deletions.
  1. +17 −1 README.md
  2. +33 −7 index.js
  3. +1 −1 package.json
  4. +32 −0 test/habitat.test.js
View
@@ -64,7 +64,7 @@ Gets a key from the environment. Automatically prefixes with the
like a `boolean`, `number` or `json`, so you can do things like this:
```bash
-exports APP_ADMINS='["me@example.com", "you@example.com"]';
+exports APP_ADMINS='["me@example.com", "you@example.com"]'
```
```js
var env = new habitat('app');
@@ -75,6 +75,22 @@ console.log(admins.indexOf('you@example.com')) // 1
If a `default` is passed, if the key is undefined in either the env or
the constructor-set defaults, it will fall back to that.
+You can also do stuff like this:
+
+```bash
+exports APP_DB="redis"
+exports APP_REDIS_HOST="127.0.0.1"
+exports APP_REDIS_PORT=6379
+```
+```js
+var env = new habitat('app');
+var db = env.get('db');
+var options = env.get('redis');
+console.log(db); // "redis"
+console.log(options.host); // "127.0.0.1"
+console.log(options.port); // 6379
+```
+
## habitat#set(key, value)
Sets an environment variable, with prefix if passed.
View
@@ -31,7 +31,11 @@ habitat.prototype.setDefaults = function setDefaults(defaults) {
habitat.prototype.get = function get(key, someDefault) {
var envkey = this.envkey(key);
var value = process.env[envkey] || someDefault;
- return habitat.parse(value);
+ if (typeof value !== 'undefined')
+ return habitat.parse(value);
+ value = this.getAsObject(key);
+ if (Object.keys(value).length)
+ return value;
};
/**
@@ -131,20 +135,42 @@ habitat.prototype.envkey = function envkey(key) {
habitat.prototype.all = function all() {
var prefix = this.prefix;
- var keys = Object.keys(process.env);
if (!prefix) return process.env;
- return keys.reduce(function (accum, key) {
- if (key.indexOf(prefix) === 0)
- accum.push(key)
- return accum;
- }, []).reduce(function (obj, key) {
+ var keys = this.rawKeys();
+ return keys.reduce(function (obj, key) {
var lowerKey = key.replace(prefix + '_', '').toLowerCase();
obj[lowerKey] = habitat.parse(process.env[key]);
return obj;
}, {});
};
+habitat.prototype.rawKeys = function rawKeys() {
+ var prefix = this.prefix;
+ var keys = Object.keys(process.env);
+ if (!prefix) return keys;
+ return keys.reduce(function (accum, key) {
+ if (key.indexOf(prefix) === 0)
+ accum.push(key);
+ return accum;
+ }, []);
+};
+
+/**
+ * Get an object by key
+ *
+ * @param {String} keyPrefix
+ * @return {Object}
+ */
+
+habitat.prototype.getAsObject = function getAsObject(keyPrefix) {
+ var envkey = this.envkey(keyPrefix);
+ var env = new habitat(envkey);
+ return env.all();
+};
+
+
+
/**
* Get a key from the environment without a prefix.
*
View
@@ -1,6 +1,6 @@
{
"name": "habitat",
- "version": "0.2.1",
+ "version": "0.2.2",
"description": "Small library for managing environment variables",
"main": "index.js",
"directories": {
View
@@ -126,3 +126,35 @@ test('habitat#all', function (t) {
t.same(obj.nope, 'naw');
t.end();
});
+
+test('habitat#rawKeys', function (t) {
+ process.env['OTHERAPP_HOST'] = 'not-local-host';
+ process.env['RAWKEYS_HOST'] = 'localhost';
+ process.env['RAWKEYS_PORT'] = 3000;
+ var env = new habitat('rawkeys');
+ t.same(env.rawKeys(), ['RAWKEYS_HOST', 'RAWKEYS_PORT'])
+ t.end();
+});
+
+test('habitat#getAsObject', function (t) {
+ process.env['APP_REDIS_HOST'] = 'localhost';
+ process.env['APP_REDIS_PORT'] = 3000;
+ var env = new habitat('app');
+ var obj = env.getAsObject('redis');
+ t.same(obj.host, 'localhost');
+ t.same(obj.port, 3000);
+ t.end();
+});
+
+test('habitat#get: should try `getAsObject` if no value is found', function (t) {
+ process.env['APP_REDIS_HOST'] = 'localhost';
+ process.env['APP_REDIS_PORT'] = 3000;
+ var env = new habitat('app');
+ var obj = env.get('redis');
+ t.same(obj.host, 'localhost');
+ t.same(obj.port, 3000);
+
+ var value = env.get('nonexistent');
+ t.notOk(value, 'should not have a value');
+ t.end();
+});

0 comments on commit 4f446a3

Please sign in to comment.